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

Discussion in 'Mac Programming' started by jpefjr, Dec 6, 2011.

  1. macrumors regular

    jpefjr

    Joined:
    Jul 8, 2008
    Messages:
    209
    #1
    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.
     
  2. macrumors regular

    jpefjr

    Joined:
    Jul 8, 2008
    Messages:
    209
    #2
    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() ?
     
  3. Moderator

    robbieduncan

    Staff Member

    Joined:
    Jul 24, 2002
    Messages:
    24,480
    Location:
    London
    #3
    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?
     
  4. macrumors regular

    jpefjr

    Joined:
    Jul 8, 2008
    Messages:
    209
    #4
    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()
    
     
  5. macrumors regular

    jpefjr

    Joined:
    Jul 8, 2008
    Messages:
    209
    #5
    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.
     
  6. macrumors 6502

    Joined:
    May 29, 2011
    Messages:
    276
    Location:
    Littleton, Colorado, USA
    #6
    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)
     
  7. macrumors regular

    jpefjr

    Joined:
    Jul 8, 2008
    Messages:
    209
    #7
    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()
    
     

Share This Page