PDA

View Full Version : How to read the plist from an Xcode Cocoa-Applescript Application?




jpefjr
Dec 6, 2011, 10:55 PM
I'm writing an Cocoa-Applescript Application in Xcode and would like to save / recall some Combo-box (NSComboBox) entries in the [AppName]-Info.plist file. I know I can write to a plist with the following AppleScript:

make new property list item at end of property list items of contents of plistfile with properties {kind:string, name:"stringName", value:"stringValue"}

But in order to use "plistfile" I need to set it to something like:

set plistfile to make new property list file with properties {contents:parent_dictionary, name:"/path/to/plistflle.plist"}

My question is how can I find the location of the [AppName]-Info.plist file from within AppleScript? Any help or pointers or suggestions would be most appreciated.



jpefjr
Dec 7, 2011, 10:37 AM
So it looks like the function CFBundleGetMainBundle() will return a CFBundleRef class in Obective-C. From there I send messages to get to the Info.plist file in the package Contents. Anyone know if there's a way for AppleScript to call CFBundleGetMainBundle() ?

robbieduncan
Dec 7, 2011, 11:00 AM
Instead of trying to call a C function can you call the class method mainBundle (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/occ/clm/NSBundle/mainBundle) on NSBundle then call bundlePath (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/occ/instm/NSBundle/bundlePath) on the returned NSBundle instance?

jpefjr
Dec 7, 2011, 04:15 PM
Instead of trying to call a C function can you call the class method mainBundle (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/occ/clm/NSBundle/mainBundle) on NSBundle then call bundlePath (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/occ/instm/NSBundle/bundlePath) on the returned NSBundle instance?

Thanks - that worked. I can now get the location of the bundle with:

property tempBundle : class "NSBundle"
set bundlePathValue to tempBundle's mainBundle()'s bundlePath()

jpefjr
Dec 7, 2011, 08:13 PM
Any idea why when I add the following line to my Cocoa-Applescript Xcode project the OASCompile fails?

make new default entry at end of default entries of user defaults with properties {name:"myName", contents:"myValue"}

And the error from the compile is:
"testuserdefaults/AppDelegate.applescript:15: error: Expected end of line, etc. but found identifier. (-2741)
Command /usr/bin/osacompile failed with exit code 1"

Line 15 is the "make new default entry..." line. Any idea how I could get the writing (and reading) of user defaults? I would just use the NSUserDefaults class but I can't figure out how to get AppleScript to call the "setObject:forKey:" method which require two parameters.

Red Menace
Dec 7, 2011, 08:31 PM
That almost looks like AS Studio terminology. Refer to the AppleScriptObjC Release Notes (http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html) for converting the method calls, for example

setObject:(id)value forKey:(NSString *)defaultName
becomes
setObject_forKey_(value, defaultName)

jpefjr
Dec 7, 2011, 09:18 PM
That almost looks like AS Studio terminology. Refer to the AppleScriptObjC Release Notes (http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html) for converting the method calls, for example

setObject:(id)value forKey:(NSString *)defaultName
becomes
setObject_forKey_(value, defaultName)

Thank you! That's exactly what I was looking for. Now I can write to the ~/Library/Preferences/[identifier].plist file with the following:


property myUserDef : class "NSUserDefaults"
property tempUserDef : class "NSUserDefaults"

set myUserDef to tempUserDef's standardUserDefaults()
set myRet to myUserDef's setObject_forKey_("myKey", "myValue")
set myRet2 to myUserDef's synchronize()