Bhishan Posted October 6, 2018 Share Posted October 6, 2018 (edited) 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 October 14, 2018 by Bhishan Link to comment
CJK Posted October 6, 2018 Share Posted October 6, 2018 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 it ≠ my 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. Bhishan 1 Link to comment
Bhishan Posted October 6, 2018 Author Share Posted October 6, 2018 Thanks a lot it works for safari. But, when I changed "Safari" to "Firefox" I was having problem. How can it be made working for "Firefox" and "Googel Chrome" also? Simply changing Safari to them seems not working. Thanks a lot. Link to comment
CJK Posted October 6, 2018 Share Posted October 6, 2018 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. Bhishan 1 Link to comment
Bhishan Posted October 6, 2018 Author Share Posted October 6, 2018 (edited) 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 October 6, 2018 by Bhishan Link to comment
CJK Posted October 8, 2018 Share Posted October 8, 2018 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 id ≠ id 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. Bhishan 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now