Toggle Witch off and on via AppleScript

Recently, a few users have asked about disabling Witch when certain programs are in the foreground. Typically this comes up because of conflicts between Command-Tab or Option-Tab (the two most-common Witch activation keys) and the foreground app. For example, you can’t use Option-Tab in a Remote Desktop Client Windows window, because Witch will grab it. Or when using Fusion to run OS X in a virtual machine, you may find that Command-Tab is trapped by OS X before it gets to your virtual machine.

In those cases, it’d be nice to easily disable Witch, then quickly enable it again when you’re done with the app in question. As of today, you can’t do this within Witch, although we have plans to change that. For now, though, the best solution is to create an AppleScript that will toggle Witch off and on as needed. You can then use any program that can run AppleScripts via hot keys (such as our own Butler) to give yourself a keyboard combo that toggles Witch off and on.

Setting up the AppleScript isn’t overly complicated, though it does differ slightly depending on whether you’re using the App Store or direct version of Witch. If you’re interested in creating your own Witch toggle, read on for the how-to…

To start, launch AppleScript Editor, which you’ll find in the Applications > Utilities folder. It should open to a blank editing window; click anywhere in that window, then paste this code:

on ApplicationIsRunning(appName)
    tell application "System Events" to set appNameIsRunning to exists (processes where name is appName)
    return appNameIsRunning
end ApplicationIsRunning

if ApplicationIsRunning("witchdaemon") then
    do shell script "killall witchdaemon"
else
    do shell script "open 'REPLACE THIS BIT'"
end if

When pasting, hopefully you noticed the REPLACE THIS BIT portion of the code. That’s what you’ll do next, and it’s where the instructions differ for the direct and App Store versions of Witch.

App Store Witch

Assuming you have Witch installed in the Applications folder, highlight REPLACE THIS BIT in the AppleScript Editor, and replace it with this code:

/Applications/Witch.app/Contents/Plugins/Witch.prefPane/Contents/Helpers/witchdaemon.app

Make sure you leave both sets of quotations marks (double and single) in place. Also, if you keep Witch somewhere other than in the /Applications folder, you’ll need to replace /Applications/ with the full path to the folder that holds the Witch application.

Direct Witch

The direct version of Witch is a System Preferences panel; this means it could be installed in one of two places: either in your user’s Library/PreferncePanes folder (if you installed for just your use), or in the top-level Library/PreferencePanes folder (if you installed for all users). You can check this in Finder by using the Go > Go to Folder menu item. Activate that, then enter /Library/PreferencePanes. If you see Witch.prefPane listed here, then use the following for the REPLACE THIS BIT section:

/Library/PreferencePanes/Witch.prefPane/Contents/Helpers/witchdaemon.app

a

If you don’t see Witch listed there, that means it’s installed in your user’s Library/PreferencePanes folder. In that case, use the following for the REPLACE THIS BIT section:

/Users/your_user/Library/PreferencePanes/Witch.prefpane/Contents/Helpers/witchdaemon.app

Note that you’ll also need to replace your_user with the short name of your user, i.e. the name of your home folder.

Testing the code

Once you have all the code in, click the Compile button, and hopefully, you won’t see any error messages. If you do, compare your code to what you see above, and see if you can spot the problem area.

Assuming it compiles, the next step is to test it. Make sure that Witch is running, then click the Run button in the AppleScript Editor. You won’t see any output, but in a second, you should find that Witch no longer works. Click the Run button again, wait a second (it takes just a bit to start Witch), then hit your Witch activation keys. You should see the Witch switcher appear onscreen.

If you do, great—keep reading. If you don’t, go back and compare the code above to your code, and make sure it’s identical and that you’ve replaced the important bit to make it work.

Next steps

Now that you’ve got your toggle code working, save it somewhere (your user’s Documents folder, for instance) as a script. You can then use your favorite macro tool to execute that saved AppleScript. Or you could put it into your Dock.

If you’re a Butler user, you can create the AppleScript directly inside of Butler. Create a new AppleScript smart item, paste in your code as above, and assign it a keyboard shortcut.

Regardless of how you set it up, just run the script (via Dock or keyboard shortcut or whatever) to disable Witch, and run it again to enable Witch. (Remember it takes about a second for Witch to become active.)

As noted, this tip may be superseded by a future release of Witch…but for now, it’s the quickest way to disable and enable Witch on the fly.

Comments are closed.