Last active
May 5, 2024 16:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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