/ freenet-preferences.applescript
freenet-preferences.applescript
  1  
  2  (* ==== Properties ==== *)
  3  
  4  property preferencesWindow : null
  5  
  6  
  7  (* ==== Event Handlers ==== *)
  8  
  9  -- This event handler is called when the "preferences" menu item is chosen.
 10  -- 
 11  on choose menu item theObject
 12  	-- Only load the preferences nib once
 13  	
 14  	if preferencesWindow is equal to null then
 15  		load nib "Preferences"
 16  		set preferencesWindow to window "Preferences"
 17  	end if
 18  	
 19  	-- Load in the preferences
 20  	loadPreferences(preferencesWindow)
 21  	
 22  	-- Show the preferences window
 23  	set visible of preferencesWindow to true
 24  end choose menu item
 25  
 26  
 27  on clicked theObject
 28  	-- Hide the preferences window
 29  	set visible of preferencesWindow to false
 30  	
 31  	--save 
 32  	if name of theObject is "Save" then storePreferences(preferencesWindow)
 33  	
 34  	if name of theObject is "newfreenetlocation" then
 35  		set newfreenetlocation to (choose folder with prompt "Please specify the location of your Freenet Folder") as string
 36  		tell preferencesWindow
 37  			set contents of text field "FreenetLocation" to newfreenetlocation
 38  			set visible of preferencesWindow to true --allows the pref window to become main again
 39  		end tell
 40  	end if
 41  	
 42  	
 43  	if name of theObject is "newentropylocation" then
 44  		set newentropylocation to (choose folder with prompt "Please specify the location of your Entropy Folder") as string
 45  		tell preferencesWindow
 46  			set contents of text field "EntropyLocation" to newentropylocation
 47  			set visible of preferencesWindow to true --allows the pref window to become main again
 48  		end tell
 49  	end if
 50  	
 51  	if name of theObject is "newsamizdatlocation" then
 52  		set newsamizdatlocation to (choose folder with prompt "Please specify the location of your Samizdat Folder") as string
 53  		tell preferencesWindow
 54  			set contents of text field "SamizdatLocation" to newsamizdatlocation
 55  			set visible of preferencesWindow to true --allows the pref window to become main again
 56  		end tell
 57  	end if
 58  	
 59  	
 60  	
 61  end clicked
 62  
 63  
 64  (* ==== Handlers ==== *)
 65  
 66  on loadPreferences(theWindow)
 67  	try
 68  		-- Read in the preferences
 69  		set theLocation to call method "defaultObjectForKey:" with parameter "location"
 70  		set entropyloc to call method "defaultObjectForKey:" with parameter "entropy"
 71  		set samizdatloc to call method "defaultObjectForKey:" with parameter "samizdat"
 72  		
 73  		-- Set the contents of the UI elements
 74  		tell theWindow
 75  			set contents of text field "FreenetLocation" to theLocation
 76  			set contents of text field "EntropyLocation" to entropyloc
 77  			set contents of text field "SamizdatLocation" to samizdatloc
 78  		end tell
 79  	on error
 80  		display dialog "Error loading Preferences."
 81  	end try
 82  end loadPreferences
 83  
 84  -- This handler will get the values from the UI elements and store those values in the  preferences file.
 85  --
 86  on storePreferences(theWindow)
 87  	-- Get the contents of the UI elements
 88  	try
 89  		tell theWindow
 90  			set theLocation to contents of text field "FreenetLocation"
 91  			set entropyloc to contents of text field "EntropyLocation"
 92  			set samizdatloc to contents of text field "SamizdatLocation"
 93  		end tell
 94  		
 95  		-- Write out the preferences
 96  		call method "setDefaultObject:forKey:" with parameters {theLocation, "location"}
 97  		call method "setDefaultObject:forKey:" with parameters {entropyloc, "entropy"}
 98  		call method "setDefaultObject:forKey:" with parameters {samizdatloc, "samizdat"}
 99  		
100  		display dialog "Preferences Saved."
101  	on error
102  		display dialog "Error saving preferences."
103  	end try
104  end storePreferences