In Safari, can a set of bookmarks in a folder be opened programmatically (e.g. using AppleScript)?

Solution 1:

If you are just wanting to script the clicking go the Open in New Tabs menu item of the target folder under Bookmarks, then the following example AppleScript code will do that:

set bookmarksFolder to "Saved Tabs"

tell application "Safari" to activate

delay 1

tell application "System Events" to ¬
    click menu item "Open in New Tabs" of ¬
        menu 1 of ¬
        menu item bookmarksFolder of ¬
        menu 1 of ¬
        menu bar item "Bookmarks" of ¬
        menu bar 1 of ¬
        application process "Safari"

Just change "Saved Tabs" in the first line of code to the actual name of the target folder on the Bookmarks menu.

Here is something a bit more robust…

I have different sets of URLs that I group into different windows by themselves, and have made an AppleScript application from the code. You can use the example AppleScript code below to do the same:

set myURLs to {¬
    "https://somedomain.com", ¬
    "https://somedomain.com", ¬
    "https://somedomain.com", ¬
    "https://somedomain.com", ¬
    "https://somedomain.com", ¬
    "https://somedomain.com", ¬
    "https://somedomain.com", ¬
    "https://somedomain.com"}

tell application "Safari"
    make new document with properties {URL:first item of myURLs}
    activate
    tell window 1
        set bounds to {0, 22, 1136, 844}
        delay 1
        repeat with i from 2 to count myURLs
            set current tab to (make new tab with properties {URL:item i of myURLs})
            delay 2
        end repeat
        set current tab to fourth tab
    end tell
end tell

Obviously "https://somedomain.com" is just a place holder for the real URL. The set bounds … and set current tab … commands can be adjusted to your liking or omitted.

I called the AppleScript application My Regular Sites, gave it a nice icon through the Get Info window method, and put it in the Dock.

This make it real easy to open Safari with a window containing my regular sites.


Note: The example AppleScript code is just that and does not contain any 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.