Repository navigation
fix(macos): keep JXA tracking when an app has no scriptable window #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ErikBjare
merged 2 commits into
ActivityWatch:master
from
TimeToBuildBob:fix/jxa-non-scriptable-app-titles
Oct 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Control-flow regression tests for the real JXA source, not a replacement | ||
| // implementation. These stubs do not validate macOS Accessibility/JXA APIs. | ||
| const assert = require("assert"); | ||
| const fs = require("fs"); | ||
| const vm = require("vm"); | ||
| const source = fs.readFileSync(process.argv[2], "utf8").replace(/^#![^\n]*\n/, ""); | ||
|
|
||
| function fail() { | ||
| throw new Error("window disappeared or attribute unavailable"); | ||
| } | ||
|
|
||
| function windowWithTitle(title, isMain = false) { | ||
| return { | ||
| attributes: { | ||
| byName: (name) => ({ | ||
| value: () => name === "AXMain" ? isMain : title, | ||
| }), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function capture(appName, app = {}, windows = () => []) { | ||
| const proc = { displayedName: () => appName, windows }; | ||
| const result = vm.runInNewContext(source, { | ||
| Application: (name) => name === "System Events" | ||
| ? { processes: { whose: () => [proc] } } | ||
| : app, | ||
| }, { timeout: 1000 }); | ||
| return JSON.parse(result); | ||
| } | ||
|
|
||
| let cases = 0; | ||
| function check(name, actual, expected) { | ||
| assert.deepStrictEqual(actual, expected, name); | ||
| cases++; | ||
| } | ||
|
|
||
| check("Safari document", capture("Safari", { | ||
| documents: [{ url: () => "https://example.com", name: () => "Example" }], | ||
| }), { app: "Safari", title: "Example", url: "https://example.com" }); | ||
|
|
||
| check("Safari no document", capture("Safari", { documents: [] }, | ||
| () => [windowWithTitle("DevTools", true)]), | ||
| { app: "Safari", title: "DevTools" }); | ||
|
|
||
| check("Safari no windows", capture("Safari", { documents: [] }), | ||
| { app: "Safari", title: "No window" }); | ||
|
|
||
| check("Safari title read fails after URL", capture("Safari", { | ||
| documents: [{ url: () => "https://stale.example", name: fail }], | ||
| }, () => [windowWithTitle("Fallback", true)]), | ||
| { app: "Safari", title: "Fallback" }); | ||
|
|
||
| for (const appName of ["Google Chrome", "Google Chrome Canary", "Chromium", "Brave Browser"]) { | ||
| for (const mode of ["normal", "incognito"]) { | ||
| check(`${appName} ${mode}`, capture(appName, { windows: [{ | ||
| activeTab: () => ({ url: () => "https://example.com", name: () => "Example" }), | ||
| mode: () => mode, | ||
| }] }), { app: appName, title: "Example", url: "https://example.com", incognito: mode === "incognito" }); | ||
| } | ||
| check(`${appName} no windows`, capture(appName, { windows: [] }), | ||
| { app: appName, title: "No window" }); | ||
| check(`${appName} title read fails after URL`, capture(appName, { windows: [{ | ||
| activeTab: () => ({ url: () => "https://stale.example", name: fail }), | ||
| mode: () => "incognito", | ||
| }] }, () => [windowWithTitle("Fallback", true)]), | ||
| { app: appName, title: "Fallback" }); | ||
| check(`${appName} mode read fails after URL and title`, capture(appName, { windows: [{ | ||
| activeTab: () => ({ url: () => "https://stale.example", name: () => "Stale" }), | ||
| mode: fail, | ||
| }] }, () => [windowWithTitle("Fallback", true)]), | ||
| { app: appName, title: "Fallback" }); | ||
| } | ||
|
|
||
| for (const appName of ["Firefox", "Firefox Developer Edition"]) { | ||
| check(`${appName} normal`, capture(appName, { windows: [{ name: () => "Example" }] }), | ||
| { app: appName, title: "Example" }); | ||
| check(`${appName} no windows`, capture(appName, { windows: [] }), | ||
| { app: appName, title: "No window" }); | ||
| } | ||
|
|
||
| check("unscriptable app", capture("ElectronApp", {}, fail), | ||
| { app: "ElectronApp", title: "No window" }); | ||
| check("throwing attributes", capture("ElectronApp", {}, () => [{ attributes: { byName: fail } }]), | ||
| { app: "ElectronApp", title: "No window" }); | ||
| check("main window preferred", capture("Terminal", {}, () => [ | ||
| windowWithTitle("zsh"), windowWithTitle("vim", true), | ||
| ]), { app: "Terminal", title: "vim" }); | ||
| check("first window fallback", capture("Finder", {}, () => [windowWithTitle("Desktop")]), | ||
| { app: "Finder", title: "Desktop" }); | ||
| check("empty title preserved", capture("Terminal", {}, () => [windowWithTitle("", true)]), | ||
| { app: "Terminal", title: "" }); | ||
|
|
||
| console.log(`${cases} JXA control-flow cases passed`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| """Exercise JXA control flow with stubs; native macOS API checks remain manual.""" | ||
|
|
||
| from pathlib import Path | ||
| import shutil | ||
| import subprocess | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| def test_jxa_fallbacks(): | ||
| node = shutil.which("node") | ||
| if node is None: | ||
| pytest.skip("Node.js is required for the JXA control-flow harness") | ||
| root = Path(__file__).resolve().parents[1] | ||
| result = subprocess.run( | ||
| [ | ||
| node, | ||
| str(root / "tests" / "jxa_harness.js"), | ||
| str(root / "aw_watcher_window" / "printAppStatus.jxa"), | ||
| ], | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=30, | ||
| ) | ||
| assert result.returncode == 0, result.stdout + result.stderr |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.