Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Krinkle/79a8c9ea285e38ddc015cf785ec6aa35 to your computer and use it in GitHub Desktop.
Save Krinkle/79a8c9ea285e38ddc015cf785ec6aa35 to your computer and use it in GitHub Desktop.
An AppleScript for Safari to move all tabs in the frontmost window, from the current tab to the rightmost (last) tab, to a new window.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- Original script: John Gruber (https://daringfireball.net/linked/2023/12/05/an-applescript-for-safari-split-tabs-to-new-window)
-- Much more elegant version: Leon Cowle (https://github.com/leoncowle)
tell application "Safari"
(*
`tab` properties:
index
name
source (the full HTML of the page)
URL
visible
*)
set _old_window to window 1
make new document
set _new_window to window 1
-- save # of tabs in new window (e.g. pinned tabs)
set _new_window_count to count tabs of _new_window
-- repeat over relevant tabs in old window,
-- create them in new window, and close in old window
set _tabs to tabs of _old_window
set _tab_count to count _tabs
repeat with i from _tab_count to 1 by -1
set _t to item i of _tabs
tell _new_window to make new tab at after tab _new_window_count with properties {URL:URL of _t as string}
if visible of _t then
-- we've reached visible tab in old window
close _t
exit repeat
end if
close _t
end repeat
-- Close the blank Start Page tab (which will be the 1st tab after all the pinned tabs)
tell _new_window to close tab _new_window_count
end tell
(*
Another idea: present a list of all tabs in the front window and select the ones you want to move to a new one. `choose from list` returns a list of strings, so we could put the tab indexes in the item names.
choose from list {"1: One", "2: Two", "3: Three", "4: Four", "5: Five"} default items {"Four", "Five"} OK button name "Move to New Window" with multiple selections allowed
set _s to "3: Three"
first word of _s as integer -- 3
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment