Allow Javascript from Apple Events will not activate

On 10.12.6, selecting Allow Javascript from Apple Events from the Develop menu does not stick.

Steps to reproduce-


  1. Develop menu > Allow Javascript from Apple Events
  2. Click "Allow" in the dialog box.

Result: Develop menu shows the setting UNchecked.

Tested with defaults read shows it is enabled (1).

Writing defaults completes without error.

Tested in different user account.

Tested with Safari beta 11.1.1, and Technology Preview

Replies

I have this issue too... (Running 10.13.5)

ps: "defaults write -app Safari AllowJavaScriptFromAppleEvents 1" does not help!


Any solutions out there?

I found the solution on Github


step 1 enable virtual keyboard for controls

If you haven't already, you'll need to open System Preferences > Keyboard > Shortcuts and change the Full Keyboard Access setting to All controls.


step 2 with virtual keyboard fully enabled

Select the "Allow JavaScript from Apple Events" from Develop menu option in Safari as normal.

When You get the prompt "Are you sure you want to allow JavaScript from Apple Events?" don't click "Allow" with mouse, but press space bar (press this button by keyboard) and then a magic window asking for a password will appear.

Thank you! You saved my bacon!

The virtual keyboard thing did not work for me. Then I realised I was accessing our file server third Remote Desktop and directly on the computer. Plugging in a keyboard and mouse to the Server allowed my to turn on JavaScript Apple Events in Safari and set the password.


However, you can't always do this, 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-4) from upper right to lower left

-- 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

Thank you! You are amazing!

the script cheat helped

Here's a more simple way of doing it - reference elements by name instead of by location:


tell application "System Events"
  tell application "Safari" to activate
  delay 1

  tell process "Safari"
  -- check if developer menu is available
  set menuItems to name of every menu bar item of menu bar 1
  if "Develop" is not in menuItems then error "You must enable developer menu" number -1024

  -- click to allow js events
  click menu item "Allow JavaScript from Apple Events" of menu 1 of menu bar item "Develop" of menu bar 1

  -- confirm allow
  if value of static text 1 of window 1 is "Are you sure you want to allow JavaScript from Apple Events?" then click button "Allow" of window 1
  end tell
end tell

The work-arounds above also work for VMware virtual machines (at least, those running High Sierrra and Safari 13), which otherwise silently abort attempts to enable either of the "Allow Javascript..." Develop Menu options at the point when they should prompt for a password.

Note the above AppleScript can be enhanced to recognize that the option is already enabled. The improved version (which I also made a little more concise by using a reference) is below.

tell application "System Events"
	tell application "Safari" to activate
	delay 1

	tell process "Safari"
		-- check if developer menu is available
		set menuItems to name of every menu bar item of menu bar 1
		if "Develop" is not in menuItems then error "You must enable developer menu" number -1024
		
		-- get a reference to the "Allow" menu item
		set mitem to a reference to menu item "Allow JavaScript from Apple Events" of menu 1 of menu bar item "Develop" of menu bar 1

		-- stop now if it's already enabled
		if (value of attribute "AXMenuItemMarkChar" of mitem) = "✓" then error "\"Allow JavaScript from Apple Events\" is already enabled" number -1024
		
		-- click to allow js events
		click mitem
		
		-- confirm allow in the pop-up dialog box
		if value of static text 1 of window 1 is "Are you sure you want to allow JavaScript from Apple Events?" then click button "Allow" of window 1
	end tell
end tell