Saving Preferences to PLIST

In the applescript studio program I am currently writing, the program must mount an AFP volume. In my Preferences window, I have created 5 text fields: server address, username, password, sharepoint, path. I would like to save this to a plist contained within the .app and have those five items loaded into corresponding values upon launch. What’s the best way to do this? I’m sorry for the noob questions.

on should quit after last window closed theObject
end should quit after last window closed

on clicked theObject
	if name of theObject is "createuser" then
		tell window of theObject
			try
				set theUserType to title of popup button 1
				set theShortName to contents of text field "shortname" as text
			end try
		end tell
		tell application "Finder"
			try
				duplicate alias (sharepoint & ":" & ppath & ":" & theUserType & ":" & theUserType)
				set name of result to theShortName
			end try
		end tell
	else if name of theObject is "sprefs" then
		getSettings()
		writeSettings()
	end if
end clicked

on will finish launching theObject
	registerSettings()
	readSettings()
	mountServer()
end will finish launching

on should quit theObject
	tell application "Finder"
		eject disk sharepoint
	end tell
end should quit

on awake from nib theObject
	if window is "Preferences" then
		setSettings()
	end if
end awake from nib

on registerSettings()
	tell user defaults
		make new default entry at end of default entries with properties {name:"saddress", contents:saddress}
		make new default entry at end of default entries with properties {name:"username", contents:username}
		make new default entry at end of default entries with properties {name:"upassword", contents:upassword}
		make new default entry at end of default entries with properties {name:"sharepoint", contents:sharepoint}
		make new default entry at end of default entries with properties {name:"ppath", contents:ppath}
		register
	end tell
end registerSettings

on readSettings()
	tell user defaults
		set saddress to contents of default entry "saddress"
		set username to contents of default entry "username"
		set upassword to contents of default entry "upassword"
		set sharepoint to contents of default entry "sharepoint"
		set ppath to contents of default entry "ppath"
	end tell
end readSettings

on writeSettings()
	tell user defaults
		set contents of default entry "saddress" to saddress
		set contents of default entry "username" to username
		set contents of default entry "upassword" to upassword
		set contents of default entry "sharepoint" to sharepoint
		set contents of default entry "ppath" to ppath
	end tell
end writeSettings

on getSettings()
	tell window "Preferences"
		set saddress to contents of text field "saddress"
		set username to contents of text field "username"
		set upassword to contents of text field "upassword"
		set sharepoint to contents of text field "sharepoint"
		set ppath to contents of text field "ppath"
	end tell
end getSettings

on setSettings()
	tell window "Preferences"
		set contents of text field "saddress" to saddress
		set contents of text field "username" to username
		set contents of text field "upassword" to upassword
		set contents of text field "sharepoint" to sharepoint
		set contents of text field "ppath" to ppath
	end tell
end setSettings

on mountServer()
	tell application "Finder"
		try
			mount volume ("afp://" & username & ":" & upassword & "@" & saddress & "/" & sharepoint)
		end try
	end tell
end mountServer

That’s my script. When I click my “save” button, I get a “NSReceiverEvaluationScriptError: 4 (1)” error. Any ideas?

Moving to the AppleScript Studio forum.

Sorry about that. :confused:

Well I added

etc etc for all the variables to the start of the script. No change. Any thoughts?

Would it be OK if this app only ran on OS 10.3 or later?

Yep that would be fine. 10.4 and later actually!

I hate to ‘bump’ a topic, but can anyone help me here. All I need to do is save 6 text strings that the application reads and allows the user to modify by means of my preference pane. OSX10.4 and later is the target audience…It will just be an in-house application.

I’m sorry that I didn’t get back to you, pianoman. For now, I’ll point you here:

http://forums.macosxhints.com/showthread.php?p=198613#post198613

I’ll expand on that as soon as I have a chance.

That’s not a problem. A little guidance is all I asked, and you have been an incredible resource. I appreciate everything you’ve done. I’m going to read that now.

property saddress : "pdc.somewhere.org"
property username : "pdcadmin"
property upassword : "wrongpassword"
property sharepoint : "Users"
property ppath : "Profiles"

on should quit after last window closed theObject
end should quit after last window closed

on clicked theObject
	if name of theObject is "createuser" then
		mountServer()
		tell window of theObject
			try
				set theUserType to title of popup button 1
				set theShortName to contents of text field "shortname" as text
			end try
		end tell
		tell application "Finder"
			try
				duplicate alias (sharepoint & ":" & ppath & ":" & theUserType & ":" & theUserType)
				set name of result to theShortName
			end try
			setOwner()
			setPermissions()
		end tell
	else if name of theObject is "sprefs" then
		getSettings()
		writeSettings()
	end if
end clicked

on will finish launching theObject
	registerSettings()
	readSettings()
end will finish launching

on should quit theObject
	tell application "Finder"
		eject disk sharepoint
	end tell
end should quit

on awake from nib theObject
	if window is "Preferences" then
		setSettings()
	end if
end awake from nib

on launched theObject
	(*Add your script here.*)
end launched

on setPermissions()
end setPermissions

on setOwner()
end setOwner

on registerSettings()
	tell user defaults
		make new default entry at end of default entries with properties {name:"saddress", contents:saddress}
		make new default entry at end of default entries with properties {name:"username", contents:username}
		make new default entry at end of default entries with properties {name:"upassword", contents:upassword}
		make new default entry at end of default entries with properties {name:"sharepoint", contents:sharepoint}
		make new default entry at end of default entries with properties {name:"ppath", contents:ppath}
		register
	end tell
end registerSettings

on readSettings()
	tell user defaults
		set saddress to contents of default entry "saddress"
		set username to contents of default entry "username"
		set upassword to contents of default entry "upassword"
		set sharepoint to contents of default entry "sharepoint"
		set ppath to contents of default entry "ppath"
	end tell
end readSettings

on writeSettings()
	tell user defaults
		set contents of default entry "saddress" to saddress
		set contents of default entry "username" to username
		set contents of default entry "upassword" to upassword
		set contents of default entry "sharepoint" to sharepoint
		set contents of default entry "ppath" to ppath
	end tell
end writeSettings

on getSettings()
	tell window "Preferences"
		set saddress to contents of text field "saddress"
		set username to contents of text field "username"
		set upassword to contents of text field "upassword"
		set sharepoint to contents of text field "sharepoint"
		set ppath to contents of text field "ppath"
	end tell
end getSettings

on setSettings()
	tell window "Preferences"
		set contents of text field "saddress" to saddress
		set contents of text field "username" to username
		set contents of text field "upassword" to upassword
		set contents of text field "sharepoint" to sharepoint
		set contents of text field "ppath" to ppath
	end tell
end setSettings

on mountServer()
	tell application "Finder"
		try
			mount volume ("afp://" & username & ":" & upassword & "@" & saddress & "/" & sharepoint)
		end try
	end tell
end mountServer

That is my latest code. I followed the instructions as per the post you linked, so now when I open the preference pane, it shows the defaults which is a huge step in the right direction. However, when I change the contents of the field and press the “Save” button (named sprefs), I get an “NSReceiverEvaluationScriptError: 4(1)” error. Not entirely sure why. Thanks again.

I fixed the last problem, however it does not duplicate the directories now as it used to (read: not at all). Not entirely sure why. It’s probably something ridiculously stupid. I will look it over sometime over this week.