MacOS – AppleScript – more than title

applescriptmacos

I have a AppleScript that runs from a cron job. The script is:

tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
    set window_name to name of front window
end if
end tell

(which I got from https://stackoverflow.com/questions/480866/get-the-title-of-the-current-active-window-document-in-mac-os-x in an example of one of many instances where SO has had the answer to a query ready for me)

I use the script to populate a file like this

l

2013-08-07_20:55:04convert - Linux Command - Unix Command
2013-08-07_20:56:05matlab graph colors.... - Stack Overflow
2013-08-07_20:57:06matlab graph colors.... - Stack Overflow
2013-08-07_20:58:07matlab graph colors.... - Stack Overflow
2013-08-07_20:59:08matlab graph colors.... - Stack Overflow
2013-08-07_21:00:10printing - Save MATLAB figure with different background color - Stack Overflow
2013-08-07_21:01:11matlab graph colors.... - Stack Overflow
2013-08-07_21:02:12Changing matlab b - Stack Overflow
2013-08-07_21:03:162013-08-07_21:04:17/Users/josephreddington/Downloads

to keep track of my time. It works well but I have issues like – all I'm getting from the Apple Script is the title of the window i.e.

matlab graph colors.... - Stack Overflow

Whereas what I'd like is a little more information, like active application (Chrome) in this instance – can someone point me in the direction of either a) how to modify the script so that I get the application name as well as the window title, or b) where I find a handy API that tells me how to do a)

Cheers,

Best Answer

tell application "System Events" to tell (process 1 where frontmost is true)
    set o to name
    try
        set o to o & ":" & name of window 1
    end try
    o
end tell