First of all, I’d like to thank all the people on the forum who helped me become acquanted with applescript and IB. I’ve been working in Xcode for two weeks now, and I can see that once you understand the basics, this is a very powerful tool!
I’ve added a simple preferences system to my script, which works fine once the preferences file has been created. But when people launch the program for the first time, the preferences file has not yet been created, which leads to an error.
When the script launches, I load some variables from the preferences file as follows:
set theUsername to do shell script "defaults read com.zomplog.preferences 'username'"
To avoid an error for first time users, I need to check if the preferences file exists first. Does anyone have a clue how to do that?
Model: Imac G5
Browser: Safari 412, Xcode 2.1
Operating System: Mac OS X (10.4)
Explanation: If the ‘alias’ test fails under the ‘try’, then the file does not exist. But, be sure you have the right path. Hence, I added ‘zomplogPrefsPath’ to use in the applescript and ‘pzomplogPrefsPath’ [note the letter -p- for POSIX] to use in your do shell script or unix paths.
Note: I am no expert, but this should be easy to follow and adapt to your code.
set userhome to path to (home folder) as text
set zomplogPrefsPath to userhome & "Library:Preferences:com.zomplog.preferences" --Set the zomplogPrefsPath
set pzomplogPrefsPath to POSIX path of zomplogPrefsPath --convert AS path to unix path
try
alias zomplogPrefsPath
set theUsername to do shell script "defaults read com.zomplog.preferences 'username'"
display dialog theUsername
on error
display dialog "File does not exists"
end try
As I mentioned in the other tread that you started about the same topic, if this is a preference file for the app that is reading the file, AS Studio has commands built-in for accessing the defaults system – no need to use a shell script. Specifically, the defaults system has a command for registering defaults the first time you run it (which will create the preferences file and, if it already exists, it will do nothing–unless you register new defaults through an upgrade or something). Once registered, you use the standard get and set commands to read and write the defaults:
property default_1 : true
property default_2 : 1
property default_3 : "string"
property default_4 : {"a", "list", "of", "mixed", "values", true, 1}
property default_names : {"default_1", "default_2", "default_3", "default_4"}
on register_defaults()
set all_defaults to {default_1, default_2, default_3, default_4}
tell user defaults
repeat with i from 1 to (count default_names)
make new default entry at end of default entries with properties {name:(item i of default_names), contents:(item i of all_defaults)}
end repeat
register
end tell
end register_defaults
on read_defaults()
set all_defaults to {}
tell user defaults
repeat with i from 1 to (count default_names)
set end of all_defaults to contents of default entry (item i of default_names)
end repeat
end tell
set {default_1, default_2, default_3, default_4} to all_defaults
end read_defaults
on write_defaults()
set all_defaults to {default_1, default_2, default_3, default_4}
tell user defaults
repeat with i from 1 to (count default_names)
set contents of default entry (item i of default_names) to (item i of all_defaults)
end repeat
end tell
end write_defaults
You should put calls to these handlers in your app very early on. The “will finish launching” handler is usually the best place because it is called before anything else – especially the “awake from nib” calls so that you have the defaults ready to go when you need to adjust the interface to match the user’s preferences.