Saturday, 21 September 2019

Enable JavaScript From Apple Events for Safari

In macOS High Sierra, after enabling the Developer menu in Safari, clicking on "Allow JavaScript from Apple Events" does not work. We can use the below script to enable the option, once the Develop menu is enabled.
-- Enabled Apple Events in Safari if not enabled using Menu
set isEventsEnabled to false
to enableAppleEventsUsingMenu()
    tell application "Safari"
        tell application "System Events"
            tell process "Safari"
                tell menu "Develop" of menu bar item "Develop" of menu bar 1
                    click it
                    get properties of menu item "Allow JavaScript from Apple Events"
                    tell menu item "Allow JavaScript from Apple Events"
                        if value of attribute "AXMenuItemMarkChar" is not "✓" then
                            set isEventsEnabled to false
                            click
                        else
                            set isEventsEnabled to true
                            keystroke return
                        end if
                        delay 1
                    end tell
                end tell
                if isEventsEnabled is false then
                    tell front window
                        click button "Allow"
                    end tell
                end if
            end tell
        end tell
    end tell
end enableAppleEventsUsingMenu

-- Invoke the method
my enableAppleEventsUsingMenu()
Run this in the Script Editor, after launching Safari, which will then ask for confirmation. Once done, we will have a tick on that menu. This script can also be used in other automation workflows where interaction with AppleScript and JavaScript within Safari needs to be enabled beforehand.

No comments:

Post a Comment