问题
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?
回答1:
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
回答2:
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)
来源:https://stackoverflow.com/questions/3531156/how-do-i-launch-a-particular-version-of-xcode-from-applescript