Skip to main content

Add my “close work tabs” script

ID
7158093
date
2022-03-19 09:41:59+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
7cf8f35
message
Add my "close work tabs" script
changed files
1 file, 64 additions

Changed files

close_work_tabs (0) → close_work_tabs (2349)

diff --git a/close_work_tabs b/close_work_tabs
new file mode 100755
index 0000000..3fb3325
--- /dev/null
+++ b/close_work_tabs
@@ -0,0 +1,64 @@
+#!/usr/bin/env osascript -l JavaScript
+// A script to close ephemeral Safari tabs.
+//
+// This is a script I run at the end of each working day, to close
+// Safari tabs I've opened that can be safely closed.
+//
+// See https://alexwlchan.net/2022/02/safari-tabs/
+
+safari = Application("Safari Technology Preview");
+
+// 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()];
+    }
+  }
+}
+
+function isSafeToClose(url) {
+
+  // Sometimes we get a `null` as the URL of a tab; I'm not sure why,
+  // so leave this tab open.
+  if (url === null) { return false; }
+
+  return (
+    url.startsWith("https://zoom.us/") ||
+    url.startsWith("https://trustnet.wellcome.org/") ||
+    url.startsWith("https://search.wellcomelibrary.org") ||
+    url.startsWith("https://logging.wellcomecollection.org") ||
+    url.startsWith("https://console.aws.amazon.com/") ||
+    url.startsWith("https://us-east-1.signin.aws.amazon.com/") ||
+    url.startsWith("https://eu-west-1.signin.aws.amazon.com/") ||
+    url.startsWith("https://eu-west-1.console.aws.amazon.com/") ||
+    url.startsWith("https://signin.aws.amazon.com/") ||
+    url.startsWith("https://github.com/wellcomecollection/") ||
+    url.startsWith("https://github.com/orgs/wellcomecollection/") ||
+    url.startsWith("https://github.com/search?type=Code&q=org:wellcomecollection") ||
+    url.startsWith("https://buildkite.com/orgs/wellcomecollection/") ||
+    url.startsWith("https://buildkite.com/wellcomecollection/") ||
+    url.startsWith("http://localhost:3000/") ||
+    url.startsWith("https://api.wellcomecollection.org/") ||
+    url.startsWith("https://www-stage.wellcomecollection.org/")
+  ) || (
+    url === 'https://github.com/'
+  );
+}
+
+for (const [window_index, tab_index, url] of tabGenerator()) {
+	if (isSafeToClose(url)) {
+		console.log(url);
+		safari.windows[window_index].tabs[tab_index].close();
+	}
+}