Copy Safari Front Window URLs

I was asked if there was a way copy the URLs of all the tabs in the front Safari window, so I created a simple macro to demonstrate how to do this. Since it might be generally useful, here it is.

Copy Safari Front Window URLs.kmmacros (3.0 KB)

2 Likes

Hey Folks,

Since Peter’s macro has to drive the UI to make each tab in turn the active tab, it will be a bit slow on windows with many tabs.

This task can be accomplished in a millisecond using AppleScript.

I’m putting the result on the clipboard, but you can just as easily drop it into a Keyboard Maestro variable, etc.

-Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/11/15 00:00
# dMod: 2017/11/15 00:03
# Appl: Safari
# Task: Put URLs of all tabs of the front window on the clipboard.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Put, @URLs, @All, @Tabs, @Front, @Window, @Clipboard
------------------------------------------------------------------------------

tell application "Safari"
   tell front window
      if its document exists then
         set AppleScript's text item delimiters to linefeed
         set urlList to URL of its tabs
         
         set the clipboard to (urlList as text)
         
      end if
   end tell
end tell

------------------------------------------------------------------------------
8 Likes

@peternlewis How could I adjust this KM Macro to return a result that includes the name of the Safari tab, in this format?
-[Safari tab name](url)
Thanks

See Safari tokens.

@JMichaelTX
Thank you for your help and the link to the token variable %SafariTitle% I need.
How do I combine %SafariTitle% and %SafariURL% in the macro, (image attached)
to produce -[%SafariTitle%]( %SafariURL%) in a bulleted markdown list?
Capture

Just as you wrote it. :wink:

As @JMichaelTX said, you already have the syntax you need to assemble the markdown list, so all you need to do now is use it in the Set Variable action after the Select Safari Tab action (or, even better, change Set Variable to Append to Variable, which you can now do as of KM 9 by clicking on the gear menu in the top right corner of the action):

But as an alternative, I would suggest using an AppleScript inspired by @ccstone's contribution, which can assemble and return the list much more quickly:

Markdown List All Front Safari Tabs.kmmacros (1.7 KB)

tell application "Safari"
	set TabNames to {}
	set FrontWindowTabs to every tab of front window
	repeat with t in FrontWindowTabs
		set end of TabNames to "- [" & name of t & "](" & URL of t & ")"
	end repeat
end tell

set text item delimiters to linefeed
return TabNames as text

@gglick
Thank you. the AppleScript work great.
Stan

1 Like