Finding Z-order of processes for Playpause

I’m attempting to create a script with the same functionality as the F8 function playpause key, which can toggle the play state of the last active media app. When Quicktime and iTunes are both open, I can’t determine which one was last active. I’ve been trying to use System Events to figure it out, but I can’t so far. Any advice?

One of the nice things added in Applescript 2.0 for Leopard was the “frontmost” attribute for an application. So you can test for “frontmost” and get a true/false answer:

tell application "System Events"
	return frontmost of application "iTunes"
end tell

Does that help?

I’d been using that in the past, but it doesn’t tell me the order that apps were last open. For example, if iTunes and Quicktime are both open, but Firefox is frontmost, I want my script to send events to the media app that was open last. I’m reasonably sure this isn’t possible using applescript, but the playpause button on my keyboard does it; so it must be possible somehow.

Model: Intel iMac
AppleScript: 2.1
Operating System: Mac OS X (10.6)

Yeah, I see your problem. I thought you just wanted the front app, but if the media app you want to control ISN’T the first app, I’m not sure if there’s a way to determine that. I’ve looked through the System Events dictionary but don’t see anything helpful. I even tried getting the list of all app process names, hoping they might be in order of their Z-index, but no such luck.

You might check around for a system additon (OSAX) that might do what you want.

NSWorkspace has a notification named “NSWorkspaceDidActivateApplicationNotification.” You could write a background application that watched for “QuickTime” and “iTunes” to be activated and then wrote that to a file. Your AppleScript application could check that file to determine which was activated last.

There is also a plist file at => “~/Library/Preferences/com.apple.recentitems.plist” but it does not seem to update regularly.

Here is the relevant code if you are interested. It does not account for when either application is quit. There is a notification for that as well.


@implementation Controller

-(void)logApplicationActivity:(NSNotification *)notification
{
	NSRunningApplication *app = [[notification userInfo] objectForKey:@"NSWorkspaceApplicationKey"];
	NSString *appKey = [app bundleIdentifier];
	
	NSRange it = [appKey rangeOfString:@"com.apple.iTunes"];
	NSRange qt = [appKey rangeOfString:@"com.apple.QuickTime"];
	
	if ( (it.location != NSNotFound) || (qt.location != NSNotFound) )
	{
		NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
		[defaults setValue:appKey forKey:@"lastActivated"];
		[defaults synchronize];
	}
	
}

-(void)setUpNotification:(NSString *)notification withSelector:(SEL)method 
{
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
														   selector:method  
															   name:notification 
															 object:nil];
}

-(void)awakeFromNib 
{
    [self setUpNotification: NSWorkspaceDidActivateApplicationNotification
               withSelector:@selector(logApplicationActivity:)];
}

@end

Thanks, I’ll give it a try.