7 Replies Latest reply: Oct 13, 2010 7:57 PM by red_menace
osimp Level 1 Level 1 (120 points)
Since Property List Editor.app isn't scriptable, it seems that GUI scripting or Terminal are the alternatives for working with Info.plist files. I thought I might have a valid GUI approach, but haven't had any luck with it.

My current interest is in getting/setting the value of the CFBundleExecutable. The GUI code I've been trying to work with involves the following:


tell application "System Events"
tell process "Property List Editor"
tell window 1
tell splitter group 1
tell scroll area 1
tell outline 1
tell outline row 4
tell text field 1
?
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell


Suggestions?

2.7G5 AND OTHERS, Mac OS X (10.6.3)
  • Tony T1 Level 6 Level 6 (8,660 points)
    Have you considered using "defaults" via a do shell script?

    See the man page for info
  • osimp Level 1 Level 1 (120 points)
    Yes, but there I'm also treading on shaky ground.

    Would you have a brief example of setting the value of the executable?

    Thanks
  • Tony T1 Level 6 Level 6 (8,660 points)
    osimp wrote:
    Would you have a brief example of setting the value of the executable?


    Don't know. You could ask in the Apple UNIX forum how to do this in Terminal, (then you could put the command in an Applescript do shell script command).
  • osimp Level 1 Level 1 (120 points)
    Okay, I'll do that. In the meantime, does anyone in this forum want to venture any ideas on the GUI approach?
  • red_menace Level 6 Level 6 (14,735 points)
    You can use System Evens to directly manipulate the plist file, for example:
    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    font-weight: normal;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px;
    color: #000000;
    background-color: #DAFFB6;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    tell application "System Events"
    get value of property list item "CFBundleExecutable" of property list file "/path/to/your/Info.plist" -- read
    set value of property list item "CFBundleExecutable" of property list file "/path/to/your/Info.plist" to "whatever" -- write
    end tell</pre>
  • osimp Level 1 Level 1 (120 points)
    Almost needless to say, I like it.

    In part this relates to a previous subject on which you provided answers that work: changing the name of an applet.
    (I needed this to allow do shell script "killall " & "(name of applet)".

    Although changing names in the applet package (and zipping/unzipping the applet file) has always worked for me, there are some downsides: changing even a space in the parent script requires going through the entire procedure again. If you have, as I do, a couple dozen short applets that all may need editing at the same time, this becomes quite a chore.

    So . . . I've just about completed a small AS-based utility that goes through the whole procedure in a few seconds. All that is required is entering the path to the applet and the desired new name.

    You may already have done this (or maybe you don't see a need for it) but, if not, I'll be happy to post the utility (when it's done) if you wish.
  • red_menace Level 6 Level 6 (14,735 points)
    There was something similar a while back in a mailing list topic about creating multiple script bundles for parallel processing. Note that if you have already tweaked your Info.plist file or bundle resources, you can edit and save the script without changing anything else by using just the "Save" command (or you can edit the script directly from the bundle resources).

    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    font-weight: normal;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px; height: 340px;
    color: #000000;
    background-color: #DAFFB6;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    on run -- example
    set theFile to (choose file with prompt "Select an application bundle to modify:")
    set theName to text returned of (display dialog "Set a name for the bundle:" default answer "testing")
    setBundleStuff(theFile, theName)
    end run


    on setBundleStuff(someFile, someName)
    (*
    rewrite the Info.plist of the designated AppleScript application bundle
    parameters - someFile [mixed]: the path to the script application to modify
    someName [text]: a new name for the bundle executable
    returns [boolean]: true if successful, false otherwise
    *)
    try
    set {someFile, success} to {(POSIX path of someFile), false}
    set plistPath to someFile & "Contents/Info.plist" -- path to the bundle's Info.plist
    set execPath to someFile & "Contents/MacOS/" -- path to the executable's folder
    set resourcesPath to someFile & "Contents/Resources/" -- path to the script resource's folder

    tell application "System Events"
    set plistContents to (value of property list file plistPath) -- read entire plist
    set oldExecName to |CFBundleExecutable| of plistContents
    set newExecName to (words of someName) as text -- strip spaces

    set |CFBundleExecutable| of plistContents to newExecName -- update CFBundleExecutable key
    -- set |CFBundleName| of plistContents to someName -- update CFBundleName key
    -- change other keys as desired

    -- set plistContents to plistContents & {|LSUIElement|:true} -- add key to not show in the Dock
    -- add other keys as desired

    -- rename files
    set name of file (execPath & oldExecName) to newExecName -- executable file
    set name of file (resourcesPath & oldExecName & ".rsrc") to (newExecName & ".rsrc")

    set value of property list file plistPath to plistContents -- update
    end tell
    set success to true
    end try
    return success
    end setBundleStuff
    </pre>