Need help retaining selection info in AS Studio between launches

About three years ago, I wrote an app using AppleScript and XCode 3.2 that helps our company’s designers “claim” images in Extensis Portfolio. A designer selects images from a Portfolio catalog, then enters information in three fields contained in an Interface Builder window. The app then enters that information in the appropriate fields in each selected Portfolio record, which streamlines data entry and ensures that all information is entered in the appropriate format.

Now I’ve been asked to make the app retain the information a designer enters. If a designer is claiming several different images for the same publication, they don’t want to re-enter all of the claiming information each time. The designer needs the freedom to select additional images in Portfolio and then rerun the claiming script, but of course relaunching the script currently forces them to re-enter all of the claiming information.

I thought properties would be the key, but eventually learned that they don’t fill this role in AS Studio. I’ve looked at bindings, but haven’t got a handle on how to successfully implement that technique in this scenario. I really enjoy working in AppleScript but I’m still a newbie to much of this. I’m hoping that someone here can offer some suggestions or code examples that can help me make sense of the best way to accomplish retaining information from one app launch to the next, perhaps auto-entering that info into the Interface Builder window from launch to launch. Thanks!

You’re right, in applescript studio properties do not retain their value between application launches. This is where you need a preferences file (ideally located in the users’ preferences folder). Basically, when your application quits you want to save any variables to a preferences file. Then at app launch first thing to do is read in the saved preferences and restore those values to the app.

In applescript studio you can use the “user defaults” system to read/write to the preferences. At app launch call the register_Preferences() and the read_Preferences() handlers. At app quit call the save_preferences().

So any variables you want to restore in your app, set them up as properties in your code so they are global. Set them up with a value of missing value. Then make a line in each of the handlers for that property so they are registered with an initial value, written at quit, and restored at launch. Make sense? :smiley:

on register_Preferences()
	tell user defaults -- the "make new default entry" will only make the entry if the default doesn't exists
		make new default entry at end of default entries with properties {name:"myVariable", contents:"initial value"}
	end tell
end register_Preferences
on read_Preferences()
	try
		tell user defaults
			set myVariable to contents of default entry "myVariable"
		end tell
		return true
	on error errorText number errorNumber
		log "Error reading the user defaults."
		log " Error " & errorNumber & ": " & errorText
		return false
	end try
end read_Preferences
on save_Preferences()
	try
		tell user defaults
			set contents of default entry "myVariable" to myVariable
		end tell
		return true
	on error errorText number errorNumber
		log "Could not save user defaults."
		log " Error " & errorNumber & ": " & errorText
		return false
	end try
end save_Preferences

If you don’t know how to use those handlers then try these. Since they use the command line tool “defaults” they’re useful for regular scripts but they can also be used in applescript studio to create a preferences file . You can look at the man page for “defaults” if you are unsure about the inputs to the handlers.

on writeDefault(prefsFilePath, KeyName, keyType, keyValue)
	try
		set keyType to ("-" & keyType) as Unicode text
		if prefsFilePath contains "/" then
			do shell script "/usr/bin/defaults write " & quoted form of prefsFilePath & space & quoted form of KeyName & space & quoted form of keyType & space & quoted form of keyValue
		else
			do shell script "/usr/bin/defaults write " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName & space & quoted form of keyType & space & quoted form of keyValue
		end if
	on error errorText number errorNumber
		log "Error in writeDefault: " & errorNumber & ": " & errorText
		return false
	end try
	return true
end writeDefault
on readDefault(prefsFilePath, KeyName)
	try
		if prefsFilePath contains "/" then
			set theVal to do shell script "/usr/bin/defaults read " & quoted form of prefsFilePath & space & quoted form of KeyName
		else
			set theVal to do shell script "/usr/bin/defaults read " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName
		end if
	on error
		set theVal to "Does Not Exist"
	end try
	return theVal
end readDefault

Hank, thank you so much for the detailed response. Thanks in large part to the information you provided, I’ve been able to update my AppleScript Studio app and have it retain key information from one launch to the next. The designers who use the app were very pleased, which makes me very happy!

Thanks again for taking the time to explain things :smiley:

Kevin

Glad to help. :smiley: I miss applescript studio! That was a great tool. I’m glad to see someone still uses it.

Count me in as well. Best way to build lightweight applications as XML-RPC/SOAP clients or prototyping.

Prototyping tool, brilliant idea! Luckily I can have both versions on Snow Leopard! :smiley: