I'm writing a program that executes do javascript
in Safari. The only problem is that I'm trying to make the app give its self permission to do it. I'm trying to locate the file that handles the Safari developer preferences so that I can do this. Does anyone have any idea where this might be or how to change these settings?

- 389
- 3
- 16
2 Answers
It's in Safari's preferences plist at ~/Library/Preferences/com.apple.Safari.plist
. The key you want is AllowJavaScriptFromAppleEvents
. You can set it using defaults
:
#to turn it on
defaults write -app Safari AllowJavaScriptFromAppleEvents 1
#to turn it off
defaults write -app Safari AllowJavaScriptFromAppleEvents 0

- 1,864
- 15
- 20
-
1Well, this does not work for Safari > 9.1.1 Only thing it allows is to later change the setting (by toggling the menu item) without entering a password (But the menu item still needs to be toggled) – user1768741 Sep 05 '16 at 07:21
-
1On Safari 9.1.3 it still asked me for the password when enabling JavaScript in addition to still having to manually enable the option. – Levi Muniz Sep 05 '16 at 07:32
-
@user1768741 I'm successfully using it on Safari 9.1.3. – Patrick Wynne Sep 05 '16 at 17:08
-
@PatrickWynne did you try it on Sierra beta / Safari 10.0? By working you mean it is actually toggling the state of this menu item? – user1768741 Sep 06 '16 at 20:04
-
@user1768741 Yep, it toggles that menu item. It also works to enable/disable that setting even without the Develop menu turned on. Just tested it again to be sure. I haven't tried it on the Sierra beta because I haven't installed that on any of my machines yet. – Patrick Wynne Sep 07 '16 at 02:37
-
@PatrickWynne you are running this on 9.1.3 on 10.11.6? Any chance you upload a screencast? – user1768741 Sep 08 '16 at 12:17
-
@user1768741 Hmm, no, I'm still on the 10.10.5. Now I'm wondering if El Cap tightened some security around this setting and that explains the differing results we're seeing. – Patrick Wynne Sep 09 '16 at 17:49
-
@PatrickWynne So how can we find out where the menu toggle is actually stored? It has to be somewhere on the system, so we should be able to modify the setting directly – Levi Muniz Sep 12 '16 at 21:13
-
@LeviMuniz Have you verified that it's not in the preferences file I mentioned earlier? Even if you can't use `defaults` to set it, it presumably is stored in the same plist file. At least, that would be my guess. – Patrick Wynne Sep 14 '16 at 04:28
The virtual keyboard thing did not work for me. As StarPlayr at apple's develepoer forum has found out the problem is in something else. For me problem occurred when i tried to do that on remote mac. For some people plugging in a keyboard and mouse to the Server allowed to turn on JavaScript Apple Events in Safari and set the password.
However, for me that wasn't an option, so the next best thing is use an accessbility scripting feature and have the machine think a user is doing the clicks, allowing you to set the password:
-- The delays can be shorter, coordinates may vary
-- Best way to get the coordinates is with Apple screen capture (command-shift-3) from upper right to lower left (the coordinates will be shown)
-- if one spends the time, the click events can be converted to Accessibility AppleScript objects by capturing them as variables, or checking the events and using the events instead of the click coordinates
tell application "System Events"
tell application "Safari"
activate
end tell
delay 1
-- click develop menu (make sure its on first)
click at {430, 12}
delay 1
-- click Allow Javascript menu from Apple Events
click at {615, 615}
delay 1
-- Click the Allow Button
click at {1010, 386}
end tell

- 95
- 1
- 14

- 11
- 1
-
Thanks for your input! Though this does not answer my original question (because I am looking for a solution that can be implemented in a BASH script), it's a good contribution for people that need a simple and quick solution. – Levi Muniz Jun 26 '19 at 23:56