An AppleScript to Turn Javascript ON/OFF in Safari
I started to write this article a while back and had left in my drafts bin. I recently started using this AppleScript again, so I thought I update and post this blog entry. This technique, although not infallible, works on a surprising number of websites.
The ‘Right’ Solution: Subscribe to the publication and support the whole journalistic enterprise. See: The Truth is Paywalled But Lies are Free. When this doesn’t work for you…
A Simple Solution: Turn off Javascript in Safari (Safari Menu, Preferences, Security Panel, toggle ‘Enable Javascript’) and then the reload (Command R) the web page. Remember to toggle Javascript back on, because so much of the display of the modern web relies on it, that it really isn’t very functional without Javascript turned on. This works for casual use, but if you find yourself seeking to avoid paywalls often, you may want to go the next step.
Note: this technique will work on an iPhone as well. Go to Settings: Safari: Advanced (the bottom of the list): Javascript toggle. It isn't as convenient to get to to toggle on and off, but it seems to work.
The High Volume Solution: Script the toggling of Javascript and attach it to a hot key or use it with Spotlight.
I found an AppleScript to perform the toggling action, but it was a little bit dated. It didn’t seem to work properly on my Mac, partly because Apple had moved the GUI scripting function into the Security & Privacy System Preferences panel. I have updated the AppleScript and have added a Page Reload to speed up viewing in Safari.
Page Blocker appears with Javascript turned ON
Javascript turned OFF – No Page Blocker
Applescript for Safari to Toggle Javascript On/Off
Apologies for the somewhat messy formatting below. It will clean up when you paste it into the AppleScript Editor and click the Compile button.
--updated for Mavericks 2014-03-09 tell application "Safari" to activate tell application "System Events" if (system attribute "sysv") < 4144 or UI elements enabled then tell application process "Safari" -- open Preferences window click menu item "Preferences…" of menu ¬ "Safari" of menu bar item "Safari" of menu bar 1 -- window title varies according to type of Prefs initially selected, ¬ --so save to a variable get value of static text 1 of window 1 copy the result as string to varWindowName -- switch to Security Prefs click button "Security" of toolbar 1 of window varWindowName -- toggles JavaScript checkbox click checkbox "Enable JavaScript" of group 1 ¬ of group 1 of window "Security" -- get new state for confirmation dialog get value of checkbox "Enable Javascript" of ¬ group 1 of group 1 of window "Security" copy the result as number to varResult --toggle the Javascript setting ON or OFF — if ON, then set to OFF etc. if varResult = 0 then set varResult to "OFF" else set varResult to "ON" end if -- close Preferences window click button 1 of window "Security" --reload the page, with/without Javascript running. click menu item "Reload Page" of menu ¬ "View" of menu bar item "View" of menu bar 1 --display confirmation dialog - this is just annoying, but you can turn --it on when debugging the script. --display dialog ("\"Toggle JavaScript\" script confirmation:" --& return & return & "JavaScript is now " & varResult & ".") --buttons {"OK"} --default button "OK" giving up after 7 with icon 1 end tell else --check to see if the AppleScript Editor has permission to run in --the Security and Privacy System Preferences panel on Mavericks. tell application "System Preferences" activate set current pane to pane "com.apple.preference.security" beep -- NOTE: The following section has a hard-coded return between -- 'Universal Access' and 'preference pane'. Replace it with a -- single space. display dialog ¬ "Security & Privacy Setting Not Enabled." & return & return & ¬ ¬ "Check \"Allow apps to control your computer, specifically the ¬ 'AppleScript Editor.app'\" in the Security & Privacy preference pane ¬ (authentication is required), then run this script again." with icon stop ¬ buttons {"OK"} default button "OK" end tell end if end tell
Copy the script above and paste it into the AppleScript Editor. Save the file as an Application with the name ‘Toggle Javascript in Safari.app’ in your Applications folder for future reference.
System Preferences: Security & Privacy Panel

Security Settings to Allow the AppleScript to Run

- Unlock the Privacy setting under the Accessibility area by clicking the small lock icon in the lower left of the screen.
- Click on the Accessibility panel
- Allow the apps below to control your computer — check the little box to the left of the AppleScript application to turn on permission for the newly created AppleScript ‘Toggle Javascript in Safari.app’
Spotlight used to run the AppleScript to toggle Javascript on or off
Typing Command-Spacebar brings up the Spotlight panel to search for files or applications. Typing in ‘Toggle’ brings up the script, and then hitting ENTER causes it to run. There are other methods to do this, using macro apps like Alfred or Keyboard Maestro, but Spotlight serves my purposes and is built-in and free to use.
Hello,
Each time I run the app, I receive an error message “Toggle Javascript On/Off is not authorized to a help access (-1719)” (translated from French). With two buttons, OK and MODIFY. The MODIFY button open the editor with the app in it. OK just close the message.
Thank you
Try this: Save the AppleScript as an Application. Give it permission to run in the Security System Preference as described.
This was already done! Iâm using Catalina, if it matters. Thank you.
Martin Pineault iConsultant Développement f.i.SCIENCES inc. m: 418-808-4839 w: http://www.fisciences.com
>