Friday, January 28, 2022

#1 2021-06-29 03:40:12 pm

rickzu
Member
From:: Atlanta
Registered: 2004-08-13
Posts: 21

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.

Last edited by rickzu (2021-06-29 03:41:06 pm)

Offline

 

#2 2021-06-29 05:53:19 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6748

Re: Accessing app's plist in Applications folder via AppleScript

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2021-06-29 06:00:23 pm

rickzu
Member
From:: Atlanta
Registered: 2004-08-13
Posts: 21

Re: Accessing app's plist in Applications folder via AppleScript

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.

Offline

 

#4 2021-06-29 08:20:30 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6748

Re: Accessing app's plist in Applications folder via AppleScript

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

Applescript:

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

and:

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2021-06-29 08:58:12 pm

rickzu
Member
From:: Atlanta
Registered: 2004-08-13
Posts: 21

Re: Accessing app's plist in Applications folder via AppleScript

Thank you sir.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)