-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Fixes "unknown" being returned when some apps open #64
Open
michaeljelly
wants to merge
6
commits into
ActivityWatch:master
Choose a base branch
from
michaeljelly:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c284004
Fixes "unknown" being returned when some apps open
michaeljelly 3d242ae
re-add Firefox and remove accidental changes
michaeljelly 2ebfbbf
Apply suggestions from code review
ErikBjare f411940
Commenting fixed title code for unscriptable apps.
michaeljelly 32bfd20
Apply suggestions from code review
ErikBjare a4b343e
Fix errors for Application with no open windows
michaeljelly 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 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 |
---|---|---|
|
@@ -23,37 +23,73 @@ var url = undefined, incognito = undefined, title = undefined; | |
switch(appName) { | ||
case "Safari": | ||
// incognito is not available via safari applescript | ||
try { // see catch for details | ||
url = Application(appName).documents[0].url(); | ||
title = Application(appName).documents[0].name(); | ||
title = Application(appName).documents[0].name(); | ||
} catch { | ||
// On Mac, you can have an application open without a window. | ||
// You can also have safari open with just a Devtools window, meaning no documents | ||
// This edge-case previously caused an error meaning time wasn't tracked. | ||
// I'm not 100% that a title of "No window" is the right approach, but it is the clearest description of what's happening I can see right now. | ||
title = "No window" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a single comment is fine, but perhaps lift out the "No window" constant into a variable, and document the behavior by the declaration/first use? |
||
}; | ||
break; | ||
case "Google Chrome": | ||
case "Google Chrome": | ||
ErikBjare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case "Google Chrome Canary": | ||
case "Chromium": | ||
case "Brave Browser": | ||
const activeWindow = Application(appName).windows[0]; | ||
const activeTab = activeWindow.activeTab(); | ||
try { // see catch for details | ||
|
||
const activeWindow = Application(appName).windows[0]; | ||
const activeTab = activeWindow.activeTab(); | ||
|
||
url = activeTab.url(); | ||
title = activeTab.name(); | ||
incognito = activeWindow.mode() === 'incognito'; | ||
url = activeTab.url(); | ||
title = activeTab.name(); | ||
incognito = activeWindow.mode() === 'incognito'; | ||
} catch { | ||
// On Mac, you can have an application open without a window. | ||
// This edge-case previously caused an error meaning time wasn't tracked. | ||
// I'm not 100% that a title of "No window" is the right approach, but it is the clearest description of what's happening I can see right now. | ||
title = "No window" | ||
}; | ||
break; | ||
case "Firefox": | ||
case "Firefox Developer Edition": | ||
title = Application(appName).windows[0].name(); | ||
break; | ||
default: | ||
mainWindow = oProcess. | ||
windows(). | ||
find(w => w.attributes.byName("AXMain").value() === true) | ||
// Some applications aren't "scriptable" according to JXA/applescript, and so you can't get their attributes or properties. | ||
// This was leading the original script to fail at executing, both when setting mainWindow and later when setting title. | ||
// This meant that everything for that window was recorded as unknown. | ||
// To fix this, I first try to get the window the old way, and if this fails then to get the name of the frontmost window as the title. | ||
// This works very well, it seems to get appName and title accurately, and avoids failing to execute and getting Unknown app names. | ||
try { | ||
mainWindow = oProcess.windows().find((w, i) => w && w.attributes && w.attributes() && w.attributes.byName("AXMain") && w.attributes.byName("AXMain").value() === true) | ||
} catch { | ||
try { // see catch for details | ||
mainWindow = oProcess.windows()[0] | ||
ErikBjare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch { | ||
// On Mac, you can have an application open without a window. | ||
// This edge-case previously caused an error meaning time wasn't tracked. | ||
// I'm not 100% that a title of "No window" is the right approach, but it is the clearest description of what's happening I can see right now. | ||
title = "No window"; | ||
break; | ||
} | ||
}; | ||
|
||
|
||
// in some cases, the primary window of an application may not be found | ||
// this occurs rarely and seems to be triggered by switching to a different application | ||
michaeljelly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if(mainWindow) { | ||
title = mainWindow. | ||
attributes. | ||
// NEW: I believe the above issue is fixed by the new try-catch structure below which falls back to the name of the frontmost window as the title. | ||
try { | ||
title = mainWindow && mainWindow. | ||
attributes && mainWindow.attributes. | ||
byName("AXTitle"). | ||
value() | ||
} | ||
} catch { | ||
title = oProcess.windows[0].name() | ||
}; | ||
|
||
} | ||
|
||
// key names must match expected names in lib.py | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please try to keep whitespace consistent (2 spaces for indentation, no end-of-line spaces)