Skip to main content

Add a script to close specific tabs

ID
11d4dd2
date
2024-04-07 18:32:19+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
931b8f4
message
Add a script to close specific tabs
changed files
3 files, 68 additions, 1 deletion

Changed files

macos/.gitattributes (290) → macos/.gitattributes (339)

diff --git a/macos/.gitattributes b/macos/.gitattributes
index 0628d3b..f0023d0 100644
--- a/macos/.gitattributes
+++ b/macos/.gitattributes
@@ -1,4 +1,5 @@
 close_tabs linguist-language=JavaScript
+close_specific_tabs linguist-language=JavaScript
 count_tabs linguist-language=JavaScript
 get_focus_mode linguist-language=JavaScript
 get_live_text linguist-language=Swift

macos/README.md (5846) → macos/README.md (6406)

diff --git a/macos/README.md b/macos/README.md
index cee628f..f8de8b7 100644
--- a/macos/README.md
+++ b/macos/README.md
@@ -25,6 +25,10 @@ scripts = [
         "description": "close ephemeral tabs in Safari – basically, anything that can be easily recreated/reopened later."
     },
     {
+        "usage": "close_specific_tabs [example.org fragment.net]",
+        "description": "close tabs in Safari based on their URL – useful for closing tabs which I can see have high activity/CPU in Activity Monitor."
+    },
+    {
         "name": "count_tabs",
         "description": "count the number of tabs I have open in Safari."
     },
@@ -98,6 +102,15 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
   </dd>
 
   <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/macos/close_specific_tabs">
+      <code>close_specific_tabs [example.org fragment.net]</code>
+    </a>
+  </dt>
+  <dd>
+    close tabs in Safari based on their URL – useful for closing tabs which I can see have high activity/CPU in Activity Monitor.
+  </dd>
+
+  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/macos/count_tabs">
       <code>count_tabs</code>
     </a>
@@ -199,4 +212,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     </p>
   </dd>
 </dl>
-<!-- [[[end]]] (checksum: 5190a14c113ded41343a7efe338cccf6) -->
+<!-- [[[end]]] (checksum: bb331272b982f8cfc69dea52a1e7f558) -->

macos/close_specific_tabs (0) → macos/close_specific_tabs (1546)

diff --git a/macos/close_specific_tabs b/macos/close_specific_tabs
new file mode 100755
index 0000000..c476159
--- /dev/null
+++ b/macos/close_specific_tabs
@@ -0,0 +1,53 @@
+#!/usr/bin/env osascript -l JavaScript
+// A script to close Safari tabs.
+//
+// This is a script I run when I see high memory usage in Activity Monitor --
+// it shows me the URL but no easy way to jump to the tab.  If I recognise
+// the URL and I know I can close it safely, this script lets me do that
+// quickly.
+
+safari = Application("Safari");
+
+// Generates all the window/tab/URLs in Safari.
+//
+// This runs in reverse window/tab index order: that is, windows are returned
+// bottom to top, and tabs from right to left.
+function* tabGenerator() {
+  window_count = safari.windows.length;
+
+  for (window_index = window_count - 1; window_index >= 0; window_index--) {
+    window = safari.windows[window_index];
+
+    tab_count = window.tabs.length;
+
+    for (tab_index = tab_count - 1; tab_index >= 0; tab_index--) {
+      tab = window.tabs[tab_index];
+
+      yield [window_index, tab_index, tab.url(), tab.name()];
+    }
+  }
+}
+
+
+function run(argv) {
+  let closedCount = 0;
+  let remainingCount = 0;
+
+  for (const [window_index, tab_index, url, title] of tabGenerator()) {
+    for (i = 0; i < argv.length; i++) {
+      const searchFragment = argv[i];
+
+      if (url.includes(searchFragment)) {
+        console.log(`${url} (${title})`);
+        safari.windows[window_index].tabs[tab_index].close();
+        closedCount += 1;
+        break;
+      } else {
+        remainingCount += 1;
+      }
+    }
+  }
+
+  console.log(`Closed ${closedCount} tab${closedCount !== 1 ? 's' : ''}; ${remainingCount} tab${remainingCount !== 1 ? 's' : ''} left open`)
+}
+