I have a fun little problem, running on Mac OS X 10.8 Mountain Lion.

I have a script (WebKit's run-safari) which launches Safari in dev mode (it sets some environmental variables and then runs arch -x86_64 /Applications/Safari/Contents/MacOS/SafariForWebKitDevelopment).

I want to improve this script in the following ways:

  • I want to make a double-clickable, pretty icon'ed shortcut to it
  • I want it to bring Safari to the foreground when run (currently running the command leaves Terminal in the foreground, and Safari in the background)
  • I want to be able to see the stdout from the SafariForWebKitDevelopment command in a terminal window
  • If possible, I would like the Terminal window to close when Safari closes.

I've been able to get any two out of three of those by combining Automating, AppleScript and bash scripts, but not all three. Does anybody have any pointers here?

  • If you save an AppleScript file as .scpt you can simply double-click it. You can also wrap it inside an Automator application. What scripts do you have so far? – slhck Nov 6 '12 at 8:24
  • I 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 Nov 7 '12 at 12:29

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"
        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
        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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.