Skip to content

Instantly share code, notes, and snippets.

@agoddard
Created March 8, 2013 03:31
Show Gist options
  • Save agoddard/5114024 to your computer and use it in GitHub Desktop.
Save agoddard/5114024 to your computer and use it in GitHub Desktop.
Save URLs of all open safari tabs to TextEdit
tell application "Safari"
--Variables
set windowCount to number of windows
set docText to ""
--Repeat for Every Window
repeat with x from 1 to windowCount
set tabcount to number of tabs in window x
--Repeat for Every Tab in Current Window
repeat with y from 1 to tabcount
--Get Tab Name & URL
set tabName to name of tab y of window x
set tabURL to URL of tab y of window x
set docText to docText & "<a href=" & "\"" & tabURL & "\">" & tabName & "</a>" & linefeed as string
end repeat
end repeat
end tell
--Write Document Text
tell application "TextEdit"
activate
make new document
set the text of the front document to docText
end tell
@wstomv
Copy link

wstomv commented Dec 6, 2013

I had some problems with your script. It needed a fix (error on last window). I also added some more HTML markup. Here is my version:

tell application "Safari"
    --Variables
    set docText to ""
    set windowCount to number of windows
    --Repeat for Every Window
    repeat with x from 1 to windowCount - 1
        set docText to docText & linefeed & "<h2>Window " & x & "</h2>" & linefeed as string
        set docText to docText & "<ol>" & linefeed as string
        set tabCount to number of tabs in window x
        --Repeat for Every Tab in Current Window
        repeat with y from 1 to tabCount
            --Get Tab Name & URL
            set tabName to name of tab y of window x
            set tabURL to URL of tab y of window x as string
            set docText to docText & "  <li><a href=" & "\"" & tabURL & "\">" & tabName & "</a>" & linefeed as string
        end repeat
        set docText to docText & "</ol>" & linefeed as string
    end repeat
end tell
--Write Document Text
tell application "TextEdit"
    activate
    make new document
    set the text of the front document to docText
end tell

Thanks for sharing.

@agoddard
Copy link
Author

@wstomv thanks!

@okaypol
Copy link

okaypol commented Nov 12, 2015

    set tabCount to number of tabs in window x

applescript doesn't understand the number written in the above code, please help

@wstomv
Copy link

wstomv commented Jun 7, 2016

@okaypol
Apparently, Safari also can have some 'invisible' windows that don't respond well to 'tabs in window'.

Here is a fix:

tell application "Safari"
    --Variables
    set docText to ""
    set windowCount to count (every window where visible is true)
    log "windowCount = " & windowCount as string
    --Repeat for Every Window
    repeat with x from 1 to windowCount - 1
        set docText to docText & linefeed & "<h2>Window " & x & "</h2>" & linefeed as string
        set docText to docText & "<ol>" & linefeed as string
        set tabCount to number of tabs in window x
        --Repeat for Every Tab in Current Window
        repeat with y from 1 to tabCount
            --Get Tab Name & URL
            set tabName to name of tab y of window x
            set tabURL to URL of tab y of window x as string
            set docText to docText & "  <li><a href=" & "\"" & tabURL & "\">" & tabName & "</a>" & linefeed as string
        end repeat
        set docText to docText & "</ol>" & linefeed as string
    end repeat
end tell
--Write Document Text
tell application "TextEdit"
    activate
    make new document
    set the text of the front document to docText
end tell

It now only processes visible windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment