Mac How to read the plist from an Xcode Cocoa-Applescript Application?

jpefjr

macrumors regular
Original poster
Jul 8, 2008
209
0
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:

Code:
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:

Code:
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

macrumors regular
Original poster
Jul 8, 2008
209
0
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() ?
 

jpefjr

macrumors regular
Original poster
Jul 8, 2008
209
0
Instead of trying to call a C function can you call the class method mainBundle on NSBundle then call bundlePath on the returned NSBundle instance?
Thanks - that worked. I can now get the location of the bundle with:
Code:
property tempBundle : class "NSBundle"
set bundlePathValue to tempBundle's mainBundle()'s bundlePath()
 

jpefjr

macrumors regular
Original poster
Jul 8, 2008
209
0
Any idea why when I add the following line to my Cocoa-Applescript Xcode project the OASCompile fails?

Code:
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.
 

jpefjr

macrumors regular
Original poster
Jul 8, 2008
209
0
That almost looks like AS Studio terminology. Refer to the AppleScriptObjC Release Notes for converting the method calls, for example

Code:
setObject:(id)value forKey:(NSString *)defaultName
becomes
Code:
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:

Code:
    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()
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.