6

I'm trying to write a script that splits the current + all next tabs into a new window — which would be super useful for organization.

It is perfectly possible to get the URLs of the current and all next tabs, make a new window and load all those URLs in new tabs, and then close the tabs in the original window – but that seems very cumbersome and slow.

So, is there any way to move a set of Safari tabs to a new window without actually reloading the URLs?

Lennart Schoors
  • 401
  • 3
  • 7
  • 2
    You can move tabs with the `move` command, for example `move tabs 2 thru 5 of window 1 to window 2`. – Willeke Jan 07 '19 at 04:24

1 Answers1

6

Not sure if this is the best/right way, but got this working:

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
ticky
  • 107
  • 8
Lennart Schoors
  • 401
  • 3
  • 7
  • 1
    You do not need `set tab_limit to index of last tab of original_window`, use `-1` for the last tab, e.g.: `move tabs tab_index thru -1 of original_window to front window` – user3439894 Jan 08 '19 at 14:11
  • Also, this doesn't work with private browsing (it will split tabs from a private window into a regular window). Still figuring that out. – Lennart Schoors Jan 09 '19 at 15:04
  • If the original window is using private browsing then instead of `make new document` use `tell application "System Events" to keystroke "n" using {shift down, command down}` to open a new private browsing window. – user3439894 Jan 09 '19 at 15:18
  • Yeah, I've got that part already, but detecting if we're in private browsing isn't that simple. [There was a trick](https://stackoverflow.com/questions/42182648/detect-safari-private-browsing-in-applescript) but it supposedly no longer works. – Lennart Schoors Jan 11 '19 at 11:10
  • This script does work in Safari 15.5 on Monterey as written in this answer, but the suggestion in comment 1 from @user3439894 does not. It also will split tabs from a tab group into a new window, but I've not been able to figure out how to split a tab group into another tab group. – Christopher Allen Jun 29 '22 at 02:10