Jump to content

[SOLVED] How to close all Safari windows and tabs except current tab?


Recommended Posts

I was trying to write an Alfred workflow that will close all the other Safari tabs and also closes all other open safari windows open.

There is keyboard shortcut  "cmd opt w" which will close all other tabs. But I have to manually close all other opened safaries.

 

How can this workflow be created?

 

I have shared my preliminary workflow here.

 

This is pseudo code:

 

Sometimes watching movies Safari creates a lot of unwanted tabs and windows, I want to get rid of them and open only current tab.

 

Thanks for help.

 

on alfred_script(q)
tell application "Safari"
    close (every window except this tab)
end tell
end alfred_script

 

Edited by Bhishan
Link to comment

You could try this:

 

use Safari : application "Safari"

 

property |windows| : a reference to every window of Safari

property |tabs| : a reference to every tab of |windows|

property tab : a reference to the current tab of Safari's front window

 

close (|tabs| where itmy tab)

 

I haven't really tested it though as I currently have a gajillion tabs open that I don't particularly want to close.

 

Also, don't use the Run NSAppleScript action.  Use the Run Script action and select /usr/bin/osascript (AS) as the chosen language.

Link to comment

I wish you had asked this in your initial question.  It's extra work to go back and test a script in different situations after the fact, rather than had I known and been able to do the testing all at once.


I'm too tired to go and test anything for Chrome, but it would involve changing current tab to active tab, and utilising the id property of my tab to exclude it from the enumerated list of id of |tabs|.  Play around with it and just do trial-by-error.  That's what we all do.


Regarding Firefox, it won't work.  Firefox isn't scriptable (to any useful extent).


I'll check back in a couple of days and post a working solution for Chrome if you haven't got it figured out by then.


Next time, please include all your related needs in a single post.  Obviously, very disparate issues are still best posted as separate posts.

Link to comment

Thanks, I was assuming it was almost similar. Anyway this will be a good practice for me.

Also, since Safari is working perfectly, most of the job is done. I mainly open safari and only few times open chrome and firefox.

 

Edited by Bhishan
Link to comment

Hi @Bhishan,

 

Here's the equivalent code for Chrome that closes all windows and all tabs except the currently active one:

 

use Chrome : application "Google Chrome"

 

property |windows| : a reference to every window of Chrome

property |tabs| : a reference to every tab of |windows|

property tab : a reference to the active tab of Chrome's front window

 

close (|tabs| where its idid of my tab)

 

It's very similar to the Safari code, but Chrome requires that comparing tabs is done via their id property rather than directly as object references.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...