An AppleScript to toggle JavaScript in Safari

May 20, '04 09:15:00AM

Contributed by: phow4rd

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.

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040517165901455