Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to toggle JavaScript in Safari Web Browsers
Being a satisfied Safari user, I've never seen the need to disable JavaScript in my regular browsing; but this morning I came across a need to test several sites with JavaScript both enabled and disabled. Imagine my surprise when I discovered that there is no keyboard shortcut in Safari to toggle JavaScript! Further, since it's not accessible from a menu (only through the Preferences dialog), I couldn't define a shortcut through the Keyboard & Mouse System Preference pane! But surely someone, somewhere must have written an Applescript to do the job?! Well, if they did, I couldn't find it.

Only one thing left to do: write my first AppleScript. Since I'm a rank amateur, suggestions and helpful critiques are welcomed.
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 tool bar 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
      if varResult = 0 then
        set varResult to "OFF"
      else
        set varResult to "ON"
      end if
      
      -- close Preferences window
      click button 1 of window "Security"
      
      -- display confirmation dialog
      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
    tell application "System Preferences"
      activate
      set current pane to pane "com.apple.preference.universalaccess"
      beep
      -- NOTE: The following section has a hard-coded return between
      -- 'Universal Access' and 'preference pane'. Replace it with a
      -- single space.
      display dialog "GUI Scripting is not enabled." & return & return & ¬
          "Check \"Enable access for assistive devices\" in the Universal Access
          preference pane (authentication is required), then run this script again." ¬
          with icon stop buttons {"OK"} default button "OK"
    end tell
  end if
end tell
For maximum efficiency, save the script as an Application using Script Editor, and use in combination with Quicksilver / LaunchBar / Butler / Whatever. BTW, I would not have been able to write this script without the assistance of PreFab Software's UI Browser.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[12,439 views]  

An AppleScript to toggle JavaScript in Safari | 6 comments | Create New Account
Click here to return to the 'An AppleScript to toggle JavaScript in Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Modify toggling JavaScript in Safari to disable Flash content
Authored by: galaher on May 20, '04 12:51:29PM

This is great. Thank you. I read the New York Times online and dislike the Macromedia Flash advertising. I was thinking it would be nice to make something that toggled the plug-ins. I copied your script and changed it to do this. For anyone else who would like to do this, note that the hyphen in the word plug-ins is an M-dash or something and a regular hyphen will throw a scripting error. Use 'Option + Hyphen' to get the correct character.



[ Reply to This | # ]
An AppleScript to toggle JavaScript in Safari
Authored by: pendraggon87 on Jul 17, '08 09:21:38AM

How do I modify this to get the value of the Open "safe" files after downloading checkbox. I tried

get value of checkbox "Open \"safe\" files after downloading" of group 1 of group 1 of window "General" but I get an error.

Thanks!



[ Reply to This | # ]
An AppleScript to toggle JavaScript in Safari
Authored by: TiredDonkey on Jul 08, '10 07:36:05AM

Great script; thanks for making it available. I use this to disable Google Analytics (which relies on JavaScript) when I am looking around my own website so that my use of the site doesn't skew my statistics.



[ Reply to This | # ]
An AppleScript to toggle JavaScript in Safari
Authored by: hejdor34 on Aug 13, '10 04:40:55AM

Very useful! I use it often.

But how can I modify it to set 'Accept Cookies = Never' ??
It would be great, if you give me a hint :-)

Thanks.

Edited on Aug 13, '10 04:41:48AM by hejdor34



[ Reply to This | # ]
An AppleScript to toggle JavaScript in Safari
Authored by: renaultssoftware on Aug 13, '10 06:13:43AM

Use Develop » <toggle>Javascript. Enable the Dev menu in Preferences

---
http://groups.google.com/group/applescripters/
http://qgf.in/
http://www.kompilesoft.info/



[ Reply to This | # ]
Toggle Safari Cookies: Never/Only from sites I visit
Authored by: hejdor34 on Aug 17, '10 04:30:00AM
I modified the script to toggle 'Accept Cookies' from 'Never' to 'Only from sites I visit' ;-)

tell application "Safari" to activate
tell application "System Events"
	tell application process "Safari"
		click menu item "Preferences…" of menu "Safari" of ¬
			menu bar item "Safari" of menu bar 1
	end tell
	tell application "System Events"
		click button "Security" of tool bar 1 of window 1 of ¬
			application process "Safari"
		delay 1
		if the value of radio button "Never" of radio group 1 of group 1 of group 1 of window "Security" of application process "Safari" is 0 then
			click radio button "Never" of ¬
				radio group 1 of group 1 of group 1 of window "Security" of application process "Safari"
		else
			click radio button "Only from sites I visit" of ¬
				radio group 1 of group 1 of group 1 of window "Security" of application process "Safari"
		end if
		get value of radio button "Never" of ¬
			radio group 1 of group 1 of group 1 of window "Security" of application process "Safari"
		copy the result as number to varResult
		if varResult = 1 then
			set varResult to "'Never'"
		else
			set varResult to "'Only from sites I visit'"
		end if
		click button 1 of window "Security" of application process "Safari"
		display dialog ("\"Toggle Cookies On/Off \" script Confirmation:" & ¬
			return & return & "'Accept Cookies' is set now to " & varResult & ".") ¬
			buttons {"OK"} default button "OK" giving up after 3 with icon 1
	end tell
end tell
Edited on Aug 17, '10 04:35:05AM by hejdor34


[ Reply to This | # ]