Quickly restarting apps with LaunchBar (and friends)

[Tweet]

power buttonI’ve been using this little trick frequently for a while now. I doubt “normal” people ever need to frequently restart apps, but when I start working on keybindings and other things that require fresh starts to take effect, it’s nice to be able to do so with a couple of keystrokes.

The AppleScript is far from brilliant (and utterly simple), but it looks like this:

tell application "System Events"
	set _app to the name of every process whose frontmost is true
end tell

tell application named _app to quit
delay 2 -- Props to Dr. Drang
tell application named _app to activate

-- optionally, if the apps you want to terminate don't respond well to `quit`
-- do shell script "killall \"" & _app & "\" && open -a \"" & _app & "\""

Open it in AppleScript Editor and save it as “Restart App.scpt”.

It figures out which app is in the foreground, quits it nicely with AppleScript and then uses the Unix open command to relaunch the app. If the apps you’re restarting aren’t responding quickly enough to the quit command, consider replacing the tell/quit and do shell script lines with the commented line at the end. That’ll knock ‘em down. Also, I played for a little while with using AppleScript to re-open the app, but it got frustrating enough that I skipped to just using open -a. There’s really no consequence I know of to that method, so why lose hair? Dr. Drang solved this one for me, just needed a little delay between the commands. Increase the delay if needed, the argument is an integer denoting seconds to pause.

You can launch this script from any launcher that doesn’t take the foreground (FastScripts, LaunchBar, Alfred, QuickSilver, etc.). In most of the aforementioned launchers, you can just pop them up, type ra and hit enter and your app flashes out and back in. Even if you don’t have an immediate need for this, you can have a lot of fun setting it up to run at random intervals on friend’s machines. Thanks to Autosave it’s practically harmless1.

  1. It’s not. That was my bad sense of humor talking. Do not do it. Why are you poking around on your friend’s machine anyway? Seems like you’d have better things to do with your time.