Extract Safari tab URLs

November 16, 2017    Article    150 words    1 min read

Sometimes you need to grab the list of URLs from all the currently opened Safari tabs, maybe when you want to go work on something else and keep the list so you can go back later. You can do it using Apple’s OSA and JavaScript:

const safari = Application('Safari')
const tabs = safari.windows[0].tabs
const urls = []
for (let i = 0; i < tabs.length; i++) {
    const url = tabs[i].url()
    if (url) {

This code saves the list of the tabs from the topmost window, but it’s easy to make the changes required to cycle through the windows.

Save the above code to a .js file named extract_safari_tabs.js (or whatever name you want, actually) and run it using OSA :

$ osascript -l JavaScript extract_safari_tabs.js

Keep in mind that you can pipe the output to the pasteboard too:

$ osascript -l JavaScript extract_safari_tabs.js | pbcopy