Remembering file location in Studio App

Hi,

I had an AppleScript file which used a Property to “remember” the alias to an EPS file which would then be imported into an InDesign page. Obviously, once the user had located the file, the AppleScript remembered where it was.

I know AppleScript Studio doesn’t have this “memory” feature, so how can I best achieve the same result?

I had thought to add the EPS file to the project (which puts it inside the package, I assume) and then work from there. But I actually can’t even see how to do that.

Any help would be greatly appreciated.

Cliff Strange
TypeShop Limited

Greetings.

You could do one of many things. If it is something that you want to distribute with your app, you could add the file to your application’s bundle. Referencing it could then be hard-coded into the app. In project builder select “Add Files…” from the Project menu and then select your file…or… locate your executable in the build directory of the project and control-click on it, select “Show Package Contents” in the popup menu, then navigate to where you want the file and insert it (probably best in the ‘Resources’ directory). Reference the file using something like…

-- change 'theFilesName' below to the actual name of the file --
set theFile to (((path to me) & ":Contents:Resources:theFilesName") as string) as file specification

You could also save it as a “preference” using the app’s user defaults (plist file). You can easily set and retrieve data stored in the UD’s, and it is a built-in function of the system so using it is reliable.

To set defaults, I use something like…


set thePref_1 to "path:to:file" as alias -- edit this so 'path:to:file' is the path to your file :-)
tell user defaults
	if default entry "MyPref1" exists then
		set contents of default entry "MyPref1" to thePref_1
	else
		make new default entry at end of default entries with properties {name:"MyPref1", contents:thePref_1}
	end if
end tell

If the pref already exists, it will rewrite it. Else it will create it.

To retrieve the value, use something like…

tell user defaults
	if default entry "MyPref1" exists then
		set MyPref1 to contents of default entry "MyPref1"
	else
		-- do something else --
	end if
end tell

This is just vague sample code, and I expect you’ll need to work it quite a bit to do what you want. This Thread has a lot of info about setting preferences, and there are others that you can find by searching this forum.

You could also go crazy and write a custom data or preferences file and place it in your app bundle, but that seems like overkill just to save one bit of info. The advantage to this though, is that it is much harder for the user to intentionally or inadvertently delete the file, losing your settings.

There is a lot of code not included here that your setup will dictate the need for. You’ll have to decide how you are going to get the data (i.e. the path to the EPS file), whether from user input, querying the disk, hard-coded, etc. You’ll also need some good error handling to make sure your file is where it should be, your preference is saved and is correct, etc. If you get nowhere, post some code and you’ll get some help, I’m sure. :slight_smile:

Good luck…
j

Hello and thanks very much.

That is a lot more information than I expected, but it is all good, and I can certainly go forward from here.

I also checked out the thread you mentioned, and it has a lot of info too.

Thanks very much for your help.

Cliff