Use AppleScript to Toggle Gatekeeper in Mountain Lion (Mac OS 10.8)

I upgraded to Mountain Lion (10.8) straight from Snow Leopard (10.6). I didn't much want to; 10.6.8 was very stable and speedy for me, but it seemed like 10.8 was stable enough, and I got used to it while recording in Graz. Generally it's been fine, maybe a little more RAM-hungry, but good.

I like the added security of Gatekeeper, and keep it set to allow applications from the App Store and identified developers. I definitely can't restrict myself only to the App Store, but want the measure of security Gatekeeper offers through signing apps. That said, every once in a while I need to toggle it briefly to "Anywhere" to run an app I've downloaded from a trusted source that is either old and no longer updated, or hasn't been signed for some reason. I'm an expert, keep backups, and am confident in my ability to unhose my machine if it gets screwed up by some rogue application, so I feel comfortable doing this.

There are a few steps to toggling Gatekeeper off, pictured here:

Step 1: Open Preferences. Step 2: Open Security & Privacy. Step 3: Click the Lock button, enter password. Step 4: Click the right radio button.  Step 5: Profit!

I do this just often enough that it felt like a good idea to write an AppleScript to handle the change for me. Here it is:

-- Open the Security & Privacy preference pane directly
tell application "System Preferences"
    set current pane to pane id ""
end tell

-- Use UIScripting to click the lock button
tell application "System Events" to set frontmost of process "System Preferences" to true
tell application "System Events"
    tell process "System Preferences"
        tell window 1
            -- click the lock
            tell button 4 to click
        end tell
    end tell
end tell

Because it involves clicking a button (the lock button), I had to resort to UIScripting via System Events. I hadn't done much of this, but got a boost from this post on the subject. To figure out what the right button was involved some trial and error, and UIElementInspector, as that article mentions. Weirdly enough, UIElementInspector, which is of course from Apple, is an example of an application that is unsigned and will require you to change the setting this script helps with.

To use it, copy that code, paste it into a new script in the AppleScript Editor, Run it to make sure it works correctly, then Save it somewhere. You can then use it via FastScripts, Keyboard Maestro, LaunchBar, or any other application that gives quick and easy access to AppleScripts. In my case, I've saved it in my LaunchBar Actions folder, naming it Gatekeeper.scpt. Now I can type Cmd-Space to activate LaunchBar, start typing Gatekeeper and hit return. The right preference pane will open up, and click the lock for me. All I need to do now is type in my password and click the right setting. Then when I've run the app I want to try, I can toggle the setting back and quit System Preferences.

Comments are closed on this post to keep spammers at bay. If you want to chime in, please email or send a tweet.