Skip to content

Instantly share code, notes, and snippets.

@gruber
Last active May 5, 2024 16:58
Show Gist options
  • Save gruber/eb89728474ba59287df79c054e1097a5 to your computer and use it in GitHub Desktop.
Save gruber/eb89728474ba59287df79c054e1097a5 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)
Even more elegant version: https://stackoverflow.com/questions/54066100/applescript-to-split-safari-tabs-into-new-window/54089865#54089865
Worth a warning: "moving" tabs with this script doesn't actually move them like
drag-and-drop does. The tabs "moved" by this script will reload in the new window,
so you'll lose (a) the current scroll position, and, more dangerously, (b) any
text you've entered in a text field.
*)
tell application "Safari"
set original_window to front window
set current_tab_index to index of current tab of original_window
set last_tab_index to index of last tab of original_window
make new document
# Account for pinned tabs; if we assume index 1, we'll close a pinned tab:
set new_blank_tab to index of last tab of front window
move tabs current_tab_index thru last_tab_index of original_window to front window
close tab new_blank_tab of front window
end tell
@barijaona
Copy link

@VRic-on-github works fine on macOS 14.1.2 too

@VRic-on-github
Copy link

Thanks. I suppose move has added benefits too, although I didn't investigate: when moving a tab by drag-and-drop, the tab is just relocated to another window instead of loading a new one with the same URL, which may or may not result in the same page (especially if the original is days or weeks old, or contains edited text fields…)

@ChristopherA
Copy link

I'm a bit confused, as I have a script that I found that does the same thing, and is a lot smaller. Is it missing something important?

# from https://stackoverflow.com/a/55832291

tell application "Safari"
	set original_window to front window
	set tab_index to index of current tab of original_window
	set tab_limit to index of last tab of original_window
	
	make new document
	move tabs tab_index thru tab_limit of original_window to front window
	close first tab of front window
end tell

@leoncowle
Copy link

That's very elegant, ChristopherA! Here is a slightly improved one, building on your response, which additionally caters for the possibility of using pinned tabs:

tell application "Safari"
	set original_window to front window
	set tab_index to index of current tab of original_window
	set tab_limit to index of last tab of original_window
	
	make new document
	set new_blank_tab to index of last tab of front window
	move tabs tab_index thru tab_limit of original_window to front window
	close tab new_blank_tab of front window
end tell

@gruber
Copy link
Author

gruber commented Mar 1, 2024

Agreed, thanks ChristopherA. I’ll update the main gist to this. So elegant.

For what it’s worth, the Stack Overflow link where you give credit is not the source for this however — but it is a rather interesting thread regarding techniques for finding open tabs that match a string or regex pattern! The actual source for this elegant “move tabs” script is

https://stackoverflow.com/questions/54066100/applescript-to-split-safari-tabs-into-new-window/54089865#54089865

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