Restart Flash without quitting Safari or Chrome (using AppleScript and LaunchBar)

October 10, 2010

A few weeks ago I linked to a Macworld article that described how to restart Safari's Flash plugin without quitting the browser, and said the following:

You guys aren't already doing this? I actually take it a step further and use a LaunchBar action (that I named "kf") to kill the plugin with just a few keystrokes (i.e., I saved do shell script "killall -9 WebKitPluginHost" to kf.scpt in LaunchBar's Actions folder).

I normally would refer to my Use LaunchBar to execute, in the "background," commands via a shell piece, and tell you to alias killall -9 WebKitPluginHost to "kf," but do shell script uses the Bourne shell, which doesn't recognize aliases. Actually, you maybe could use my linked-to solution together with TextExpander (i.e., have "kf" expand to "killall -9 WebKitPluginHost").

Since making those comments I've grown the idea a bit and now my "kf" LaunchBar action checks to see which browser -- Safari or Chrome -- is having trouble with Flash, and kills the appropriate plugin. (In either case, the plugin is restarted automatically whenever you (re)load a page with Flash.)

Here's my updated AppleScript:

tell application "System Events" to set webBrowser to name of first process 
    where frontmost is true
if webBrowser is "Safari" then
    do shell script "killall -9 WebKitPluginHost"
else if webBrowser is "Google Chrome" then
    do shell script "ps ux | grep -E '[F]lash Player Plugin 
    for Chrome' | awk '{ print $2; }' | xargs kill"
end if

Because it's very likely you'll invoke the script while cursing at your unresponsive browser, the script simply checks to see which is the frontmost app, and if the frontmost app is either browser, then it kills that browser's Flash plugin. If neither browser is the frontmost app, then the script effectively does nothing. (No, I don't usually run both browsers at once, but by coding it this way I have to remember just one command — "kf" — irrespective of which browser I'm currently using.)

I had every intention of killing Chrome's Flash plugin the same way I do Safari's, but I just couldn't figure out a way to use "Shockwave Flash (Chrome Plug-In Host)" (i.e., its Process Name as reported by Activity Monitor), which is why I had to resort to the slightly(!) less elegant commands you see above.

As ever, if you've any questions or tips on how to make this faster, smaller, etc., please let me know.

You should follow me on Twitter here