As suggested by slhck, I created a standalone AppleScript that accomplishes this task, then saved it in the AppleScript editor utility as a .app. It does exactly what I want.
Below, find the script. It checks if SafariForWebKitDevelopment is running, telling Terminal to run a script to launch it if need be. (This automatically creates a new window, and I set my default Terminal new window settings to close a window when its process exits.) Then it sets that process to be the top window.
I went on a wild goose chase trying to figure out how to handle the case of regular Safari running at the same time as SafariForWebKitDevelopment, and finally came to the solution you see in the code, working with processes rather than applications.
tell application "System Events"
-- Only launch development Safari if it isn't already running
if not (exists process "SafariForWebKitDevelopment") then
tell application "Terminal"
do script "run-safari; exit"
activate
end tell
end if
-- Max number of iterations of checking for process before
-- we give up and exit the script (guards against errors in
-- launching SafariForWebKitDevelopment, where the process
-- would never exist, and this would be an infinite loop)
set num_checks to 100
-- Wait until dev Safari has launched
repeat until (exists process "SafariForWebKitDevelopment")
delay 0.1
set num_checks to num_checks - 1
if num_checks < 0 then
return
end if
end repeat
-- Set dev Safari to have focus
-- 'tell application "Safari" to activate' doesn't work because AppleScript
-- has no way of discerning between multiple processes from the same .app
-- bundle, so we can't be sure if we're talking to
-- SafariForWebKitDevelopment, or an already-running normal Safari
set frontmost of (process "SafariForWebKitDevelopment") to true
end tell
If you save an AppleScript file as
.scptyou can simply double-click it. You can also wrap it inside an Automator application. What scripts do you have so far? – slhck – 2012-11-06T08:24:41.627I don't know why I didn't think of that before. I created an AppleScript to launch a new terminal window and execute the run-safari command in it. Posted it below. Thanks for the insight! – int3h – 2012-11-07T12:29:39.690