Skip to content

Instantly share code, notes, and snippets.

@max-arnold
Last active June 25, 2019 08:24
Show Gist options
  • Save max-arnold/b30c5e17a383199621383a41d6f4b869 to your computer and use it in GitHub Desktop.
Save max-arnold/b30c5e17a383199621383a41d6f4b869 to your computer and use it in GitHub Desktop.
Safari to Firefox tab transporter
-- Transport tabs from one browser to another. This script moves tabs from Safari to Firefox
-- config
property sourceBrowser : "Safari"
property destinationBrowser : "Firefox"
on run
assertRunning()
assertHasOpenWindow()
set sourceWindow to assertFrontmost()
set urls to getTabs(sourceWindow)
assertUrlsMoveable(urls)
openTabs(urls)
closeTabs(sourceWindow)
end run
-- warn if browser isn't running
on assertRunning()
if application sourceBrowser is not running then peaceOut(sourceBrowser & " is not running.")
end assertRunning
-- warn if browser has no open windows
on assertHasOpenWindow()
using terms from application "Safari"
tell application sourceBrowser to set openWindowCount to count windows
end using terms from
if openWindowCount <= 0 then peaceOut("There are no open windows to transport from " & sourceBrowser & ".")
end assertHasOpenWindow
-- warn if window is minimized to the Dock
on assertFrontmost()
using terms from application "Safari"
tell application sourceBrowser to set sourceWindow to front window
end using terms from
if sourceWindow is miniaturized then peaceOut("The frontmost window of " & sourceBrowser & " is ignored because it is minimized.")
return sourceWindow
end assertFrontmost
-- collect tabs from source browser, swallowing blank tabs
on getTabs(sourceWindow)
set urls to {} -- list of urls to transport
using terms from application "Safari"
tell application sourceBrowser
try
repeat with t in tabs of sourceWindow
if URL of t is not "topsites://" then copy URL of t to end of urls
end repeat
end try
end tell
end using terms from
return urls
end getTabs
-- warn if no urls can be moved (ex. topsites:// page)
on assertUrlsMoveable(urls)
if (count of urls) = 0 then peaceOut("There are no valid URLs to transport.")
end assertUrlsMoveable
-- close tabs in source browser
on closeTabs(sourceWindow)
using terms from application "Safari"
tell application sourceBrowser to close sourceWindow
end using terms from
end closeTabs
-- open tabs in destination browser
on openTabs(urls)
using terms from application "Firefox"
tell application destinationBrowser
activate
repeat with u in urls
open location u
end repeat
end tell
end using terms from
end openTabs
-- end this script
on peaceOut(errorMessage)
display alert errorMessage
error number -128
end peaceOut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment