Seit mehreren Jahren zeichne ich jede Minute auf, welches Fenster auf meinem Mac OS X-Desktop aktiv ist. Ich nutze diese Aufzeichnungen nicht zuletzt dazu, um bei ungefähr zu wissen, wie lange ich für Kundenaufträge gearbeitet habe — und dementsprechend Rechnung zu stellen.
Das AppleScript lautet folgendermassen:
tell application "System Events"
set app_name to name of item 1 of (every process whose frontmost is true)
end tell
tell application app_name
try
if (count of windows) > 0 then
set mouseToolsPath to "usr:local:bin:MouseTools"
set {mouseX, mouseY} to paragraphs of (do shell script quoted form of POSIX path of mouseToolsPath & " -location")
set title to (app_name & "&title=" & name of window 1 & "&x=" & mouseX & "&y=" & mouseY)
end if
on error
set title to app_name
end try
end tell
Damit das Script auch gleichzeitig die Position des Mauszeigers erfasst, müssen noch die MouseTools heruntergeladen und — in meinem Fall — unter /usr/local/bin/ abgelegt werden.
Um dieses Script regelmässig auszuführen, habe ich mit der kostenpflichtigen Applikation Lingon einen launchd-Job erstellt (in der Steinzeit hat man noch auf Cron-Jobs zurückgegriffen), der jede Minute ausgeführt wird. Hierzu verwende ich ein bash-Script als Wrapper, um die Informationen an ein PHP-Script auf einem im Intranet stehenden Linux-Server weiterzuleiten:
#!/bin/sh
LOGGEDIN=`who | grep "^mario" | grep console | wc -l`
WGET="/opt/local/bin/wget"
#WGET=`which wget`
if [ $LOGGEDIN -lt 1 ];
then
echo "User mario not logged in. Exiting."
exit 0
fi
if [ ! -x "$WGET" ];
then
echo "wget executable '$WGET' not found. Exiting."
exit 1
fi
APP=`/usr/bin/osascript /usr/local/bin/FrontMostApplication.scpt`
#echo $APP
URL="http://tld/save.php?app=$APP"
#echo "Accessing URL '$URL' using $WGET"
$WGET -q -O /dev/null "$URL"
exit 0
