views:

46

answers:

2

I have two versions of Xcode installed, Xcode 3.2.3 and the Xcode4 developer preview. How do I ensure from Applescript that the 3.2.3 version is picked?

+2  A: 

Instead of simply referencing Xcode by its name, i.e.:

tell application "Xcode"
    ...
end tell

you can also reference a particular version of an application by its full POSIX path, i.e.:

tell application "/Developer/Applications/Xcode 3.2.3.app"
    ...
end tell

Also see the AppleScript language guide section on the application class.

A more complex solution involves searching the Launch Services database for all the versions of an application that are installed on the system. You can then programmatically pick the one with the required version:

property pLSRegisterPath : "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
property pAppBundleID : "com.apple.Xcode"
property pAppRequiredVersion : "3.2.3"

set theAppPaths to every paragraph of (do shell script pLSRegisterPath & " -dump | grep --before-context=2 \"" & pAppBundleID & "\" | grep --only-matching \"/.*\\.app\"")

set xcodeApp to missing value
repeat with theAppPath in theAppPaths
    try
        if (version of application theAppPath) = pAppRequiredVersion then
            set xcodeApp to application theAppPath
            exit repeat
        end if
    end try
end repeat

if xcodeApp = missing value then
    error "Needed application version not installed."
end if

using terms from application "Xcode"
    tell xcodeApp
        activate
    end tell
end using terms from
sakra
Oh, yea I used to do that before. But now I want an alternative without the need to specify said path. I wonder how the script picks one among the two .app bundles.
Plumenator
@Plumenator I have update my answer with an alternate solution that explicitly searches for the required version of Xcode.
sakra
Wow, this looks just right! Lemme go to work and try this out in the morning. Btw, is the "using terms" block needed? Also how do you know this stuff? The launch services part esp.?
Plumenator
The "using terms" block is needed to make Xcode's AppleScript vocabulary available during compile time. The nested "tell xcodeApp" statement ensures that the correct version of Xcode is used during run time.Even if you are doing high level programming AppleScript it pays to know some lower level technologies like the Launch Services Framework. A good starting point is the Mac OS X Technology Overview:http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/OSX_Technology_Overview/MacOSXOverview/MacOSXOverview.html
sakra
+1  A: 

You can launch an application by its id like this. Maybe the two version will have different id's.

tell application id "com.apple.AddressBook"
    -- do something
end tell

You can get an application's id with this...

tell application "Finder" to return id of (choose file)
regulus6633
They do indeed have different bundle identifiers, Xcode 3 is "com.apple.Xcode" and Xcode 4 is "com.apple.dt.Xcode". I'm not sure if it'll stay that way when Xcode 4 goes final, but you can use it for the time being at least.
Brian Webster
I thought they might. I've seen demos of v4 and it looks so different I was sure it would have different preferences, which means it had to have a different id. The id and the pref file name are the same.
regulus6633