1

I managed to make this script work but I think it can be improved further.

Can anyone advise me

  • How it can be refactored?
  • Why I cannot run close tabs whose id is not in closeTabList. (It will return can’t make {y, x} into type specifier error)

Here's the script:

tell window 1 of application "Google Chrome"
    set canCloseTab to false
    set closeTabList to {}
    
    repeat with t in tabs
        if canCloseTab then
            set end of closeTabList to id of t
        else
            if id of t = id of the active tab then
                set canCloseTab to true
            end if
        end if
    end repeat
    repeat with closeTabId in closeTabList
        close (tabs whose id is closeTabId)
    end repeat
end tell

I tried using this script for Safari but it doesn't work on Google Chrome because AppleScript cannot get the index of the tab

0

2 Answers 2

2

This following AppleScript code should accomplish what you are trying to achieve.

This will close the tabs to the right

try
    tell window 1 of application "Google Chrome" to ¬
        close (tabs -1 thru (active tab index + 1))
end try

This will close the tabs to the left

try
    tell window 1 of application "Google Chrome" to ¬
        close (tabs 1 thru (active tab index - 1))
end try
1
  • When I first saw the question, I had tried tell application "Google Chrome" to close tabs ((active tab index of front window) + 1) thru -1 of front window and for some reason it didn't work, and I focused back on what I was previous working on. Turns out the error was I had the last tab selected and obviously no try statement. I should have looked harder at the error. Anyway this should definitely be the accepted answer. +1 Sep 16, 2021 at 11:45
2

Frame challenge…
I've no idea how to fix the Applescript, sorry, but this seems something of a heavy-handed approach to a function that's already in the menu & can therefore be triggered directly by a simple key command.

enter image description here

enter image description here

enter image description here

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .