Skip to content

Instantly share code, notes, and snippets.

@superstrong
Created April 3, 2014 16:17
Show Gist options
  • Save superstrong/9957569 to your computer and use it in GitHub Desktop.
Save superstrong/9957569 to your computer and use it in GitHub Desktop.
[Applescript] Every 10 seconds, append the current dateTime, name of the current application, and window title to a file called "activeApps.csv" on the desktop. To use, export as an application and check "Stay open after run handler", then give the application access to Assistive Devices. TODO: change delimiter so commas in window titles don't c…
delay (2)
idle {}
on idle
my checkApp()
return 9
end idle
on checkApp()
global frontApp, frontAppName, windowTitle
set windowTitle to ""
delay (0.25)
tell application "System Events"
delay (0.25)
set frontApp to first application process whose frontmost is true
delay (0.25)
set frontAppName to name of frontApp as string
delay (0.25)
tell process frontAppName
delay (0.25)
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle" as string
end tell
end tell
end tell
set filePath to (path to desktop as text) & "activeApps.csv"
set currentApp to frontAppName & "," & windowTitle & return
set todayDate to current date
set currentTime to time string of (current date)
set timeStamp to "" & (year of todayDate) & "-" & ((month of todayDate) as integer) & "-" & (day of todayDate) & " " & currentTime
set loggedData to timeStamp & "," & currentApp
set theResult to writeTo(filePath, loggedData, text, true)
if not theResult then display dialog "There was an error writing the data!"
end checkApp
on writeTo(targetFile, theData, dataType, appendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- appendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as text
set openFile to open for access file targetFile with write permission
if appendData is false then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment