[object Object]

← back to Opened Tagger

scope to entire computer (global mdfind + noise filter); add Elsewhere group in viewer

7b12d6c7c4361ea09360e2225543a5d0398f9740 · 2026-06-24 14:23:46 -0700 · Steve

Files touched

Diff

commit 7b12d6c7c4361ea09360e2225543a5d0398f9740
Author: Steve <steve@designerwallcoverings.com>
Date:   Wed Jun 24 14:23:46 2026 -0700

    scope to entire computer (global mdfind + noise filter); add Elsewhere group in viewer
---
 tag-opened.py | 40 ++++++++++++++++++++++++----------------
 viewer.py     | 24 ++++++++++++++++++++++--
 2 files changed, 46 insertions(+), 18 deletions(-)

diff --git a/tag-opened.py b/tag-opened.py
index ecc1091..861497e 100644
--- a/tag-opened.py
+++ b/tag-opened.py
@@ -33,17 +33,17 @@ TAG = "Blue\n4"          # Finder tag name + color index (4 == blue)
 TAG_NAME = "Blue"
 XATTR = "com.apple.metadata:_kMDItemUserTags"
 
-# Default surfaces. Apps are handled separately (whole-Mac query) so launched
-# apps in /Applications, /System/Applications, etc. all get caught.
-DEFAULT_DIRS = [
-    os.path.expanduser("~/Desktop"),
-    os.path.expanduser("~/Documents"),
-    os.path.expanduser("~/Downloads"),
-    os.path.expanduser("~/Projects"),
-]
+# Whole-Mac scope: a single global Spotlight query for everything opened,
+# minus the system-noise paths below. Pass --dirs to restrict to specific
+# folders instead.
+DEFAULT_DIRS = None   # None == scope the entire computer
 
-# Folders we never want to tag inside — pure noise, never "things you read".
-EXCLUDE_SUBSTR = ("/node_modules/", "/.git/", "/Library/Caches/", "/.Trash/")
+# Paths we never tag — app-managed files a human never actually "opens".
+EXCLUDE_SUBSTR = (
+    "/node_modules/", "/.git/", "/.Trash/",
+    "/Library/", "/Caches/", "/Application Support/",
+    "/.npm/", "/.cache/", "/Pictures/Photos Library.photoslibrary/",
+)
 
 
 def mdfind(query, only_in=None):
@@ -83,15 +83,23 @@ def has_blue(tags):
 
 def gather_targets(dirs):
     seen = set()
-    # Files/folders opened at least once in each surface.
-    for d in dirs:
-        if not os.path.isdir(d):
-            continue
-        for p in mdfind("kMDItemUseCount > 0", only_in=d):
+    if dirs is None:
+        # Whole-computer scope: one global query for everything opened.
+        for p in mdfind("kMDItemUseCount > 0"):
             if any(s in p for s in EXCLUDE_SUBSTR):
                 continue
             seen.add(p)
-    # Launched apps, anywhere on the Mac.
+    else:
+        # Restricted scope: query each named surface.
+        for d in dirs:
+            if not os.path.isdir(d):
+                continue
+            for p in mdfind("kMDItemUseCount > 0", only_in=d):
+                if any(s in p for s in EXCLUDE_SUBSTR):
+                    continue
+                seen.add(p)
+    # Launched apps, anywhere on the Mac (apps live under /Library so the
+    # exclusion list would otherwise drop them — add them explicitly).
     for p in mdfind('kMDItemContentType == "com.apple.application-bundle" '
                     '&& kMDItemUseCount > 0'):
         seen.add(p)
diff --git a/viewer.py b/viewer.py
index 50e803e..7f302d9 100644
--- a/viewer.py
+++ b/viewer.py
@@ -24,13 +24,19 @@ import subprocess
 from datetime import datetime, timezone
 from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
 
+# Whole-computer scope. Known surfaces get their own group; everything else
+# opened anywhere on the Mac lands in an "Elsewhere" group, grouped by parent.
 SURFACES = {
     "Desktop": os.path.expanduser("~/Desktop"),
     "Documents": os.path.expanduser("~/Documents"),
     "Downloads": os.path.expanduser("~/Downloads"),
     "Projects": os.path.expanduser("~/Projects"),
 }
-EXCLUDE = ("/node_modules/", "/.git/", "/Library/Caches/", "/.Trash/")
+EXCLUDE = (
+    "/node_modules/", "/.git/", "/.Trash/",
+    "/Library/", "/Caches/", "/Application Support/",
+    "/.npm/", "/.cache/", "/Pictures/Photos Library.photoslibrary/",
+)
 
 
 def mdfind(query, only_in=None):
@@ -96,6 +102,20 @@ def collect():
             "surface": "Apps", "name": os.path.basename(p), "path": p,
             "opened": True, "use": use, "last": last,
         })
+    # Everything else opened anywhere on the Mac (whole-computer scope) that
+    # isn't already covered by a known surface or the apps query.
+    known = set(SURFACES.values())
+    have = {r["path"] for r in rows}
+    for p in mdfind("kMDItemUseCount > 0"):
+        if p in have or any(s in p for s in EXCLUDE) or p.endswith(".app"):
+            continue
+        if any(p.startswith(k + os.sep) for k in known):
+            continue   # already shown under its surface group
+        use, last = attrs(p)
+        rows.append({
+            "surface": "Elsewhere", "name": os.path.basename(p), "path": p,
+            "opened": use > 0, "use": use, "last": last,
+        })
     return rows
 
 
@@ -142,7 +162,7 @@ def render(rows):
     for r in rows:
         by_surface.setdefault(r["surface"], []).append(r)
     out = []
-    order = ["Desktop", "Documents", "Downloads", "Projects", "Apps"]
+    order = ["Desktop", "Documents", "Downloads", "Projects", "Apps", "Elsewhere"]
     for s in order:
         items = by_surface.get(s)
         if not items:

← bc8dd09 add light-blue-text web viewer (opened=blue, unopened=bold w  ·  back to Opened Tagger  ·  (newest)