Close Safari Tabs Except Active Mac
In this article
Want to quickly close all Safari tabs except the one you're actively viewing on your Mac? Itβs a common need when you've accumulated a lot of tabs and just want to tidy up without losing your place. The easiest way to do this involves using a combination of keyboard shortcuts and AppleScript. Here's a step-by-step guide.
What You'll Need
- A Mac computer running macOS.
- Safari browser installed and functioning.
- Basic familiarity with using the macOS Spotlight search.
Step-by-Step
- Open Spotlight Search: Press
Command + Spacebarto open Spotlight search. This is the quickest way to launch applications and run commands. - Run the AppleScript: Type
osascriptinto the Spotlight search bar and press Enter. This will launch theosascriptcommand-line tool. - Paste the Script: A terminal window will open. Paste the following AppleScript code into the terminal window:
tell application "Safari" set activeTab to active tab of window 1 repeat with i from 1 to (count tab of window 1) if tab i is not activeTab then close tab i of window 1 end if end repeat end tell - Execute the Script: Press the Enter key to execute the AppleScript. The script will run and automatically close all tabs in the current Safari window except for the tab you are currently viewing.
- Verify the Result: Check that only the active tab remains open in the Safari window.
Note: Menu labels and window titles may differ slightly depending on your macOS version, Safari version, and regional settings.
Common Mistakes
- Incorrect Script Syntax: Ensure you copy and paste the entire AppleScript code exactly as provided. Even a small typo can prevent the script from running correctly.
- Multiple Safari Windows: This script only affects the *first* Safari window. If you have multiple Safari windows open, you'll need to run the script for each window individually.
- Active Tab Not Recognized: The script identifies the "active tab" as the currently selected tab in the first window. If you're unsure which window is considered first, make sure the window you want to keep open is the foremost window.
- Spotlight Not Working: If Spotlight search isn't functioning correctly, try restarting your Mac.
Tips
- Create an Alias: For quicker access, create an alias (shortcut) to the
osascriptcommand with the script embedded. Right-click on the script in TextEdit (or your preferred text editor), choose "Create Alias," and then place the alias in your Dock or on your desktop for easy access. - Automator Service: You can create an Automator service that runs this script with a single click from the Safari menu. This provides a more integrated workflow. To do this:
- Open Automator (found in /Applications/).
- Choose "Quick Action."
- In the workflow receives section, select "Safari" from the dropdown.
- Add a "Run AppleScript" action to the workflow.
- Paste the AppleScript code into the "Run AppleScript" action.
- Save the Quick Action with a descriptive name (e.g., "Close Other Safari Tabs").
- The Quick Action will now be available in Safari's Services menu (under the Safari menu).
- Regular Cleanup: Use this technique regularly to keep your Safari tabs organized and prevent clutter.
Troubleshooting
- Script Doesn't Run: Double-check the script for any typos. Ensure you're running it through the
osascriptcommand in Terminal. - Tabs Aren't Closing: Verify that you have multiple tabs open in a Safari window. The script only closes tabs if there are more than one. Also, confirm that the active tab is correctly identified. Try clicking on the tab you want to keep open *before* running the script.
- Terminal Errors: If you see an error message in the Terminal window, it usually indicates a syntax error in the script. Carefully review the script and compare it to the provided code. Common errors include missing quotation marks or incorrect variable names.
- Safari Freezes: While rare, running scripts on Safari can occasionally cause it to freeze. If this happens, force quit Safari (
Command + Option + Esc) and relaunch it. - Permissions Issues: macOS security settings might prevent the script from controlling Safari. Check System Preferences > Security & Privacy > Privacy tab and ensure that Automator (or Terminal, if you're running the script directly) has access to control Safari.
Frequently Asked Questions
Will this script close tabs in other Safari windows?
No, this script only affects the first Safari window that is open. You'll need to run it for each window separately.
Can I modify the script to close *all* tabs?
Yes, you can. Simply remove the `if tab i is not activeTab then` condition. The modified script would be:
tell application "Safari"
repeat with i from 1 to (count tab of window 1)
close tab i of window 1
end repeat
end tell
However, be *extremely* careful when running this modified script, as it will permanently close all your tabs without warning.
Is there a way to undo closing the tabs?
Unfortunately, once the tabs are closed, they are usually gone. Safari doesn't have a built-in "undo close tab" feature for multiple tabs closed simultaneously. Regularly saving your browsing sessions or using a tab management extension can help prevent data loss.
Does this work with Safari Technology Preview?
The script should generally work with Safari Technology Preview, but there's no guarantee due to the preview nature of the browser. Changes in Safari's internal structure could break the script in future versions of the Technology Preview.
Can I use this script on iPadOS?
No, this script is designed for macOS and relies on the osascript command and AppleScript, which are not available on iPadOS.
How do I create a keyboard shortcut for this?
You can assign a keyboard shortcut to the Automator service (as described in the "Tips" section). This allows you to run the script with a single key combination.