There used to be the setting com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptEnabled
, but this seems to have disappeared sometime prior to Safari 17. As a substitute, we can use AppleScript to do some GUI scripting. It's far from ideal, but it works.
Here's a link to the Shortcut; you can change the keyboard binding as desired by clicking on "Quick Actions" and changing the "Run with" binding:
Here's also a copyable version of the AppleScript:
tell application "System Events"
tell application process "Safari"
set frontmost to true
keystroke "," using command down
tell window 1
click button "Security" of toolbar 1 of it
click checkbox 1 of group 1 of group 1 of it
keystroke "w" using command down
end tell
end tell
end tell
You can also use separate the Shortcuts for enabling and disabling Javascript if you want. Here's the corresponding AppleScript; remove the not
as appropriate:
tell application "System Events"
tell application process "Safari"
set frontmost to true
keystroke "," using command down
tell window 1
click button "Security" of toolbar 1 of it
set javascriptBox to checkbox 1 of group 1 of group 1 of it
tell javascriptBox
if not (its value as boolean) then click javascriptBox
end tell
keystroke "w" using command down
end tell
end tell
end tell
Note that you may have to grant siriactionsd
and Script Editor permission to control your computer in System Settings → Privacy and Security → Accessibility:
WebKitJavaScriptEnabled
andWebKitPreferences.javaScriptEnabled
). Changing these from the command line almost works, but you seem to need to restart Safari for the change to be taken into account. Close, but no cigar.