Temporarily turn off Gatekeeper

I like GateKeeper. It keeps unsigned apps from opening. The problem is not all apps I currently want to open are signed. So, I still want the ability to choose to open unsigned apps.

This script turns off Gatekeeper, opens the currently selected item in The Finder, then turns GateKeeper back on

To use: Select the App you want to open without GateKeeper on, Trigger the script.

Note: This only needs to be invoke once, when the unsigned app is opened the first time.


tell application "System Preferences"
	open "com.apple.preference.security"
	delay 5 -- This delay to to allow for System Preference to open
	-- Without it, the authentication dialaog need to be clicked to come front.
	activate
	reveal anchor "General" of pane id "com.apple.preference.security"
end tell

tell application "System Events"
	tell process "System Preferences"
		tell application "System Events"
			click button "Click the lock to make changes." of window "Security & Privacy" of application process "System Preferences"
			delay 5 -- This delay to minimize a run-away situation.
			-- Without the delay, it is possible that the script takes off before the pref is authenicated.
			click (radio button "Anywhere" of radio group 1 of tab group 1 of window "Security & Privacy") of application process "System Preferences"
			delay 2
			keystroke space
		end tell
	end tell
end tell

tell application "Finder"
	set theSelection to get selection
	open theSelection
end tell


tell application "System Preferences"
	activate
end tell

tell application "System Events"
	tell process "System Preferences"
		tell application "System Events"
			click button "Click the lock to prevent further changes." of window "Security & Privacy" of application process "System Preferences"
		end tell
	end tell
end tell

Hi, Stephen.

I’m still on Snow Leopard myself, but I’ve no doubt your script will be useful to those with GateKeeper.

With regard to your actual code:

  1. I find that to open an anchor from an initially non-running System Preferences, it’s enough just to have the ‘reveal’ command followed by ‘activate’. On both my systems, this launches System Preferences and brings it to the front with the anchor already visible:
tell application "System Preferences"
	reveal anchor "General" of pane id "com.apple.preference.security"
	activate
end tell

Of course, this may not work on systems later than mine. :slight_smile:

  1. You’ve sort of “double-wrapped” your System Events references:

Should be:

tell application "System Events"¨
	tell application process "System Preferences"
¨		click button "Click the lock to make changes." of window "Security & Privacy"
		-- etc.
	end tell
end tell

Or:

tell application "System Events"
¨	click button "Click the lock to make changes." of window "Security & Privacy" of application process "System Preferences"
	-- etc.
end tell
  1. When putting delays into GUI scripts, the delay time can often be ensured to be adequate and simulaneously kept to a minumum by repeating a very short delay until a known and testable change occurs in the process’s GUI. In the case of clicking the “locked padlock” button, for example, you might repeat until that button ceased to exist, having been replaced with the “unlocked padlock” button:

tell application "System Preferences"
	reveal anchor "General" of pane id "com.apple.preference.security"
	set paneName to name of current pane
	activate
end tell

tell application "System Events"
	tell application process "System Preferences"
		tell window paneName
			tell button "Click the lock to make changes."
				-- If the "locked padlock" button's showing, click it and wait for it to be replaced with the "unlocked padlock" button.
				set wasLocked to (it exists)
				if (wasLocked) then
					click
					repeat while (it exists)
						delay 0.2
					end repeat
				end if
			end tell
			
			-- Something similar here, but I don't know what's supposed to happen, so I can't write it!
			click radio button "Anywhere" of radio group 1 of tab group 1 -- ie. of window paneName
			delay 2
			keystroke space
		end tell
	end tell
end tell

tell application "Finder"
	set theSelection to get selection
	open theSelection
end tell


if (wasLocked) then
	tell application "System Preferences"
		activate
	end tell
	
	tell application "System Events"
		click button "Click the lock to prevent further changes." of window paneName of application process "System Preferences"
	end tell
end if

I liked this idea so using these ideas I wrote a script for this too. As Nigel pointed out there’s some issues with Stephen Magladry’s script. Good luck.

-- this will temporarily unlock the preference to allow "applications downloaded from anywhere" to run
-- it makes the setting to "allow applications from anywhere" in system preferences
-- it opens the selected Finder files
-- it changes the preference setting back

property shortDelay : 0.2

try
	-- get the selection
	tell application "Finder" to set theSelection to get selection
	if theSelection is {} then error "Nothing is selected! Please select the application in the Finder and try again."
	
	-- open the preference pane
	activateSystemPreferences()
	tell application "System Preferences"
		tell pane id "com.apple.preference.security" to reveal anchor "General"
	end tell
	delay shortDelay
	
	tell application "System Events"
		tell process "System Preferences"
			-- check the lock button and unlock it
			set {lockButton, isLocked} to my lockButtonAndState()
			if lockButton is missing value then error "Could not find the Lock Button."
			if isLocked then
				click lockButton
				
				-- wait for the user to authenticate
				my waitWhileAuthenticatingLockState()
				
				-- check if it was unlocked
				set {lockButton, isLocked} to my lockButtonAndState()
				if isLocked is not false then error "You did not unlock the preferences!"
			end if
			
			-- make sure the "Anywhere" button is selected
			set {currentButton, anywhereButton} to my getAllowAppsButtonsState()
			if not (currentButton is equal to anywhereButton) then
				click anywhereButton
				delay shortDelay
				
				-- dismiss the warning sheet about setting it to anywhere
				if exists (sheet 1 of window 1) then
					click button "Allow From Anywhere" of sheet 1 of window 1
				end if
			end if
		end tell
	end tell
	
	-- open the selected items now that the preference allows it
	tell application "Finder" to open theSelection
	delay shortDelay
	
	-- change the preference back, relock the lock button, and quit system preferences
	activateSystemPreferences()
	tell application "System Events"
		tell process "System Preferences"
			-- put the app preference back
			if not (currentButton is equal to anywhereButton) then
				click currentButton
				delay shortDelay
			end if
			
			-- relock the lock button
			set {lockButton, isLocked} to my lockButtonAndState()
			if lockButton is not missing value and not isLocked then
				click lockButton
				delay shortDelay
			end if
		end tell
	end tell
	tell application "System Preferences" to quit
on error theError number errorNumber
	tell me
		activate
		display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
	end tell
end try


(*************** SUBROUTINES *****************)
on lockButtonAndState()
	tell application "System Events"
		tell process "System Preferences"
			set lockButton to missing value
			set isLocked to missing value
			set theButtons to buttons of window 1
			repeat with aButton in theButtons
				try
					set bName to name of aButton
					if (bName contains "make") then
						set isLocked to true
						set lockButton to contents of aButton
						exit repeat
					else if (bName contains "prevent") then
						set isLocked to false
						set lockButton to contents of aButton
						exit repeat
					end if
				end try
			end repeat
		end tell
		
		return {lockButton, isLocked}
	end tell
end lockButtonAndState

on waitWhileAuthenticatingLockState()
	set c to -1
	tell application "System Events"
		tell process "System Preferences"
			repeat
				try
					set c to count of windows
					if c is 1 then exit repeat
				end try
				delay shortDelay
			end repeat
		end tell
	end tell
	return c
end waitWhileAuthenticatingLockState

on getAllowAppsButtonsState()
	tell application "System Events"
		tell process "System Preferences"
			set currentButton to missing value
			set anywhereButton to missing value
			
			set theButtons to radio buttons of radio group 1 of tab group 1 of window "Security & Privacy"
			repeat with aButton in theButtons
				if value of aButton is 1 then set currentButton to contents of aButton
				if (title of aButton is "Anywhere") then
					set anywhereButton to contents of aButton
					exit repeat
				end if
			end repeat
		end tell
	end tell
	return {currentButton, anywhereButton}
end getAllowAppsButtonsState

on activateSystemPreferences()
	tell application "System Preferences"
		launch
		delay shortDelay
		activate
		delay shortDelay
	end tell
end activateSystemPreferences