Applescript/Javascript to click submenu item of Jupiter Notebook in Safari

applescriptjavascriptsafari

When I run a.ipynb jupyter notebook file in Safari, it opens at local host http://localhost:8888/notebooks/a.ipynb#.

NOTE: The script should work for any *.ipynb file, a.ipynb is just an example. Also, the website is at the frontmost app while running the code.

How can I write an Applescript (may be using JavaScript) so that I can click the menu Run All Below button in that tab of safari submenu ?

enter image description here

Here is my attemt:

tell application "Safari"

activate

end tell


tell application "System Events"

tell process "Safari"

click menu item "Run All Below" of menu "Cell" of menu bar 1

end tell

end tell

But, this did not work. I am very new to applescript and generally only use Python.

How can I click the required menu "Run All Below" ?

Related Links:
Using Javascript | Applescript to click button in Safari
https://stackoverflow.com/questions/24917282/click-on-safari-web-page-button-using-applescript

Best Answer

An easy way to do this is to use the Automator "Watch Me Do" feature, then convert it to Applescript.

  1. Open the Automator app and create a new document.
  2. Select "Workflow" as the document type.
  3. Now make sure you have Safari open to the page you want it to.
  4. Go back to Automator and click on "Record." There should be a small black window that pops up showing Automator is recording.
  5. Go to Safari and do whatever action(s) you want to automate.
  6. Stop the recording.
  7. Go back to Automator and there should be a new action that looks somewhat like this: enter image description here
  8. You may need to adjust the playback speed using the slider to make it work properly
  9. Now you can just leave it like this and convert it to an application so you can double-click it to run, or:
  10. See here to convert it to AppleScript so you can use it in Script Editor