Home/Q&A/Can AppleScript run JavaScript in Safari or Chrome on a remote Mac?

Can AppleScript run JavaScript in Safari or Chrome on a remote Mac?

Updated · airankrush

Direct answer

Yes, but first you have to turn on a browser switch that is off by default, allowing JavaScript from Apple Events, and then pass the system's Automation prompt. You do both steps yourself in the remote desktop.

Basis

  • Safari's scripting dictionary has a do JavaScript command that runs a piece of script in a given tab and returns the result. By default it is refused; you need to show the developer features first and then tick “Allow JavaScript from Apple Events”.
  • The matching command in Chrome is execute javascript, and the allow option of the same name is in the Developer submenu of the View menu. Menu locations in both browsers shift between versions, so go by what you see on your machine.
  • On top of the browser's own switch, the system shows an Automation prompt the first time a script controls the browser, and you need to click Allow.
  • This approach operates on the browser window you are actually using, with its existing logins and extensions; WebDriver-style test tools start a separate controlled session instead. The two suit different jobs.

Information as of .

Boundaries: when the answer is different

Once this switch is on, any script that has Automation permission can read page content, including data on sites you are signed in to. Create a separate browser profile for automation and do not mix it with a profile that holds important accounts. Many websites restrict automated access in their terms of service and have their own detection; using scripts to circumvent a third-party platform's security mechanisms is a prohibited use and can lead to suspension of the machine and termination of service. Selectors stop working when a page is redesigned, which is normal for scripts like this, and we do not guarantee that any particular website can be automated.

How to do it

  1. Turn on the web developer features in Safari's settings, then find and tick the option that allows JavaScript from Apple Events.
  2. In Script Editor, write a one-line script that makes the current tab return document.title.
  3. Run it, and in the system prompt allow Script Editor to control Safari.
  4. Once you get the title back, swap in your real logic, and add a timeout and failure logging to each step.
  5. Turn the browser switch off again when you no longer need it.

Use case

This question belongs to the use case: iMessage, AppleScript and Shortcuts on a rented Mac mini: it runs, but Apple decides about your account

Other questions in the same use case:

Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support

Back to home · All Q&A