AppleScript to prompt for url input, open url in safari, then open bookmark with specific name

applescriptbookmarksjavascriptsafari

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

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.

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.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

Example AppleScript code:

set theURL to ""

repeat while theURL is ""
    set theURL to ¬
        the text returned of ¬
            (display dialog "Enter the URL:" default answer ¬
                "" buttons {"Cancel", "OK"} default button 2)
end repeat

tell application "Safari"
    activate
    make new document with properties {URL:theURL}
end tell

my waitForSafariPageToFinishLoading()

tell application "System Events" to ¬
    click menu item "Google" of ¬
        menu 1 of ¬
        menu bar item "Bookmarks" of ¬
        menu bar 1 of ¬
        application process "Safari"


--  # Handler #

to waitForSafariPageToFinishLoading()
    --  # Wait for page to finish loading in Safari.
    --  # This works in **macOS Catalina** and 
    --  # macOS Big Sur and may need adjusting for
    --  # other versions of macOS.
    tell application "System Events" to repeat until ¬
        exists (buttons of groups of toolbar 1 of window 1 of ¬
            process "Safari" whose name = "Reload this page")
        delay 0.5
    end repeat
end waitForSafariPageToFinishLoading

Notes:

I have a bookmark that runs some JavaScript code on the website the user has open at the time of running the bookmark.

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.