Triggering scripts on change of frontmost application
  • Hi Daniel,

    Is there a way fastscripts can trigger a script when the frontmost application changes?


    I am guessing you are getting notifications from the OS to allow you to supply app specific menus, and wondered if it would be possible to trigger something off the back of these notifications?

    My best solution at the moment polls for the front most app via System Events, which is not very efficient, and in the end appears to kill System Events!

    Any suggestions?

    Thanks a lot,

    David Blishen
  • This is an interesting idea. Right now I can't think of any way to do this but I will take it as a feature request.

    What I'm envisioning is a special set of folders inside the "per-application" script folders. Any scripts inside those folders would get run on special events:

    Safari -> Activate -> SetupMySafariEnv.scpt

    Something like that. Sound like a reasonable approach? If you can share with us what specifically you're hoping to achieve, it will help me understand how to best support it in the future, and also might give me ideas about how to better workaround the problem. now.

    Daniel
  • It occurs to me that it might not be good enough if what you want to run needs to happen for *every* time the front-app changes. So ... let me know some idea of what you're thinking.
  • Hi Daniel,

    Thanks for the quick replies.

    The particular application I was thinking of was a way of logging the time spent in various applications, basically a way of measuring, across a number a people, the "true" (ie launched and frontmost) concurrent usage of the various applications we supply to users.

    I currently do this with a daemonised shell script which polls "System Events" every 30 seconds for the frontmost application. The main issue with this, is after a while the polling (not every efficient anyway) appears to kill System Events.

    So in this particular instance - making the function application specific is not ideal as I would need to make scripts for every application. What might be easier would be some system wide "event" folders that trigger the enclosed scripts when (in my example): "Frontmost application changes".

    That said - application specific folders to trigger scripts when particular applications are launched / activated / quit would also be extremely useful. We make a great deal of use of the Adobe CS3 apps supporting (on the whole) startup scripts - but wouldn't it be cool if we didn't need to rely on the vendors to support such funtionality.

    So - like a true consumer - I would say I would want this sort of functionality at both the application level, and system wide!!

    Thanks a lot,

    David
  • OK, thanks, David. Lots of food for thought here.

    It occurs to me that with your current shell script set up ... you're "oh so close" to being able to take advantage of the same app-switching notifications that FastScripts sees. Unfortunately they are implemented in a programming layer that is accessible only by Carbon (the procedural programming interface for Mac OS). But starting in Leopard, the lines are blurring quite a bit between scripting languages and the compiled languages, so that you can with a little work access the same mechanisms.

    I don't know if you guys are moved to Leopard yet, but if you are, or if you have even a single Leopard machine, you might install developer tools and then poke around in:

    /Developer/Examples/Python/PyObjC/Cocoa/AppKit/HotKeyPython/

    What that demonstrates is hooking into the same "events" system that I use to get app notifications. In this case just to register for hotkeys, but it's the same type of approach. Anyway, I figured if you're doing things as technical as shell scripts that poll System Events, you might want to at least put solutions like this on your radar.

    Daniel
  • Hello again,

    Thanks for the Leopard tips - we'll be moving over starting in Jan or Feb I guess - so not far off. Great to know stuff like this is getting easier, so I will definitely take a look. Without wanting to take up *too* much of your time are the carbon notifications you use - kEventAppLaunched and kEventAppTerminated? I wondered about trying to access these with call method from an AppleScript studio application, but I couldn't see a notification for applications becoming frontmost. Am I missing something.

    Thanks a lot,

    David
  • The notifications in question are actually of type kEventAppFrontSwitched.

    It turns out that I had actually written a fun little demo app in Objective-C that is the basic skeleton for what you're trying to do in terms of monitoring app usage:

    http://www.red-sweater.com/free/WHYDFML.zip

    That's "What Have You Done For Me Lately."

    The source is all there and you'll have to have developer tools installed to compile and run it. But it works on Tiger, too.

    You should be able to skim the source code there, particularly in TimeController.m, to get a sense for how to use the notifications. And if an Objective-C-based solution is in the realm of reality, feel free to use that code as a starting point.

    Daniel
  • Hi Daniel,

    That's really kind. Thanks for your help.

    Best wishes,

    David
Start a New Discussion

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!