Accessing app's plist in Applications folder via AppleScript

I’ve built an app in Xcode with AppleScript/AppleScriptObjC and have been reading/writing to a plist on my desktop with no problems.

But when I move the plist to the Applications folder in the app’s “Contents” folder I get this error: “System Events got an error: Can’t get property list file “~/Applications/my_app/Contents/prefs.plist”. (error -1728)”

Is there a way to either:

  1. read/write to my “prefs.plist” in the Applications folder using AppleScript (as I’m doing now)?, or
  2. read/write to the app’s info.plist using AS (I have little skill using AppleScriptObjC)?

Or is this simply a security access issue? I’m sure I’m missing something obvious. Thanks for any direction.

It’s a security issue. Your code shouldn’t modify the contents of the app’s bundle.

Thanks Shane. Forgive the nubie question then: how does one handle plists? If you can’t access the app’s plist or add one yourself in the bundle (as I was trying to do) then is it a matter of locating the plist in some other location? I’m concerned that I would need an installer to make sure that that plist is installed in a static location. I feel like I’m missing the obvious solution as it’s done all the time. Thanks again.

You should be handling preferences by using NSUserDefaults. For example, with a boolean value for the key SomePrefKey, you could use:

current application's NSUserDefaults's standardUserDefaults()'s setBool:true forKey:"SomePrefKey"

and:

set thePref to (current application's NSUserDefaults's standardUserDefaults()'s boolForKey:"SomePrefKey") as boolean

There are similar methods for other types, as well as the registerDefaults: method for setting default values.

If you download my PrefStorageLib from here:

https://latenightsw.com/freeware/

looking at its code should give you examples.

Thank you sir.