I have a bookmark that runs some JavaScript code on the website the user has open at the time of running the bookmark – I'm aware there is a way using AppleScript to execute JavaScript on a Safari page but this requires 'Allow JavaScript from Apple Events' which I'm trying to avoid.
I'd like the script to ask for the user to enter a url, it will then open a new window with this page and then it will click the bookmark which will itself run some JavaScript on the open page.
So far, by looking at existing posts here on StackExchange, I've been able to get it to open bookmarks in different tabs, but I'm struggling to amend it to open a specific named bookmark on the page the script has opened.
Would appreciate any help anyone can give!
EDIT 1: The code that I'm amending from: In Safari, can a set of bookmarks in a folder be opened programmatically (e.g. using AppleScript)?
EDIT 2: Here's my latest code revision:
@user3439894 I've changed my code to
tell application "Safari" to activate
delay 1
tell application "System Events" to ¬
click menu item "Google" of ¬
application process "Safari"
with there being a bookmark titled 'Google' – I wasn't sure whether to keep the line 'application process "Safari"' or not, but whether its kept in or removed I get "Can't get menu item 'Google' of application process 'Safari'"
Best Answer
The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
Example AppleScript code:
Notes:
The example AppleScript code does exactly what was asked on the quoted portion of the question show at the top of this answer, however, the quoted portion of the question show at the start of this Notes: section, I have no way to determine what it does or whether or not is will work as you not included it as asked in a comment under the question. So, if what's presented herein does not achieve your goal you are on your own as I will not respond further without you providing all of the requested information.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g.
delay 0.5
, with the value of the delay set appropriately.