Skip to main content

Move my “list Safari tabs” script to JXA

ID
3e52e1a
date
2024-02-10 21:26:30+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
586f48a
message
Move my "list Safari tabs" script to JXA
changed files
3 files, 42 additions, 1 deletion

Changed files

.gitattributes (204) → macos/.gitattributes (250)

diff --git a/.gitattributes b/macos/.gitattributes
similarity index 90%
rename from .gitattributes
rename to macos/.gitattributes
index a3d8869..18ce58d 100644
--- a/.gitattributes
+++ b/macos/.gitattributes
@@ -2,4 +2,5 @@ close_tabs linguist-language=JavaScript
 get_focus_mode linguist-language=JavaScript
 get_live_text linguist-language=Swift
 get_photo_sizes linguist-language=Swift
+list_safari_tabs linguist-language=JavaScript
 set_accent_colour linguist-language=Swift

macos/README.md (5190) → macos/README.md (5528)

diff --git a/macos/README.md b/macos/README.md
index 851ae10..1d304b8 100644
--- a/macos/README.md
+++ b/macos/README.md
@@ -45,6 +45,10 @@ scripts = [
         "description": "get OCR'd text for a single image using Live Text"
     },
     {
+        "name": "list_safari_tabs",
+        "description": "print the URL of every tab I have open in Safari"
+    },
+    {
         "name": "obnote",
         "description": "print the path to the note I currently have open in [Obsidian](https://obsidian.md/), if any."
     },
@@ -135,6 +139,15 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
   </dd>
 
   <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/macos/list_safari_tabs">
+      <code>list_safari_tabs</code>
+    </a>
+  </dt>
+  <dd>
+    print the URL of every tab I have open in Safari
+  </dd>
+
+  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/macos/obnote">
       <code>obnote</code>
     </a>
@@ -173,4 +186,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     </p>
   </dd>
 </dl>
-<!-- [[[end]]] (checksum: 08c7ada61eb1b97fe419046dcccb8b1f) -->
+<!-- [[[end]]] (checksum: c269506c592ac8bcea9ed47784bf7616) -->

macos/list_safari_tabs (0) → macos/list_safari_tabs (717)

diff --git a/macos/list_safari_tabs b/macos/list_safari_tabs
new file mode 100755
index 0000000..ea9bf42
--- /dev/null
+++ b/macos/list_safari_tabs
@@ -0,0 +1,27 @@
+#!/usr/bin/env osascript -l JavaScript
+// A script to list all my open Safari tabs.
+
+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 tab.url();
+    }
+  }
+}
+
+for (const url of tabGenerator()) {
+  console.log(url);
+}