Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ __pycache__
build
dist
*.swp
.mypy_cache
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
aw-watcher-window
=================
# aw-watcher-window

Cross-platform window-Watcher for Linux (X11), macOS, Windows.

[![Build Status](https://travis-ci.org/ActivityWatch/aw-watcher-window.svg?branch=master)](https://travis-ci.org/ActivityWatch/aw-watcher-window)

### Note to macOS users

To log current window title the teminal needs access to macOS accessibility API.
To log current window title the terminal needs access to macOS accessibility API.
This can be enabled in System Preferences > Security & Privacy > Accessibility, then add the Terminal to this list. If this is not enabled the watcher can only log current application, and not window title.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, the extra changes here were due to prettier, I believe. Let me know if you want them reverted. Not sure why it would remove the trailing line...

2 changes: 1 addition & 1 deletion aw-watcher-window.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ block_cipher = None
a = Analysis(['aw_watcher_window/__main__.py'],
pathex=[],
binaries=None,
datas=[("aw_watcher_window/printAppTitle.scpt", "aw_watcher_window")],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
Expand Down
6 changes: 1 addition & 5 deletions aw_watcher_window/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ def get_current_window_linux() -> Optional[dict]:

def get_current_window_macos() -> Optional[dict]:
from . import macos
info = macos.getInfo()
app = macos.getApp(info)
title = macos.getTitle(info)

return {"title": title, "appname": app}
return macos.getInfo()


def get_current_window_windows() -> Optional[dict]:
Expand Down
35 changes: 23 additions & 12 deletions aw_watcher_window/macos.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import subprocess
from subprocess import PIPE
import os
from typing import Optional
from AppKit import NSWorkspace
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID
)

def getInfo() -> Optional[dict]:
app_name = ''
title = ''

def getInfo() -> str:
cmd = ["osascript", os.path.join(os.path.dirname(os.path.realpath(__file__)), "printAppTitle.scpt")]
p = subprocess.run(cmd, stdout=PIPE)
return str(p.stdout, "utf8").strip()
app = NSWorkspace.sharedWorkspace().activeApplication()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if app:
pid = app['NSApplicationProcessIdentifier']

def getApp(info) -> str:
return info.split('","')[0][1:]
options = kCGWindowListOptionOnScreenOnly
window_list = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
for window in window_list:
if pid == window['kCGWindowOwnerPID']:
# We could use app['NSApplicationName'], but this value is more
# accurate and matches other methods (like applescript)
app_name = window['kCGWindowOwnerName']
title = window.get('kCGWindowName', u'')
break


def getTitle(info) -> str:
return info.split('","')[1][:-1]
return {"appname": app_name, "title": title}
Copy link
Member

@exoji2e exoji2e Feb 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it ever happens that the pid isn't found in window_list, but maybe it can occur because of realtime problems. We should probably move the return to the break, so we don't get a name-error here.

Binary file removed aw_watcher_window/printAppTitle.scpt
Binary file not shown.