0

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'"

10
  • Please add the code you already have.
    – nohillside
    Jul 26 at 9:07
  • @nohillside just added the link to the code I'm amending and embedded within my post
    – srb7
    Jul 26 at 9:50
  • You can change Open in New Tabs in click menu item "Open in New Tabs" of ¬ to the name of the target bookmark, or use the index value, e.g. 1 or whatever number it is in the list of the bookmarksFolder menu to have it click that bookmark. Jul 26 at 13:55
  • As to other coding issues, there are plenty of answers already out there that do what you are asking. I know because I written them numerous times. You can use the display dialog command to prompt for the URL Jul 26 at 13:56
  • @user3439894 I've updated my original post with your suggested changes but I don't think I've implemented it correctly as it's returning an error
    – srb7
    Jul 26 at 14:45
0

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.

3
  • This works pretty well! I really appreciate your time writing this up. Currently the only issue I seem to have is after the first time it is run it doesn't prompt for the url anymore, instead it remembers the previously input url.
    – srb7
    Jul 27 at 11:11
  • Ah not to worry, I've edited it to reassign the url variable to an empty string and that seems to have done it - once again, thanks!
    – srb7
    Jul 27 at 11:21
  • @srb7 I've changes the first line to set ... and that will resolve the issue. Jul 27 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .