PDA

View Full Version : launchApplication is not activated as well?




cowgp
Apr 14, 2008, 02:39 PM
What should have been a rather simple task seems to not be...

in my Cocoa app I reach a point where after doing a version check on iTunes, I launch it and need it to be brought to the foreground... in 10.4 simply doing this statement does this correctly.

[[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];

however in 10.5.2, the application requested (iTunes) is launched effectively at the bottom of the stack (if you immediately invoke app swither, iTunes is the last in the row).

I cant seem to find any API call for activating or "focusing" another app other than your own... any ideas on what the API call would be?

thanks



robbieduncan
Apr 14, 2008, 02:44 PM
Use Applescript:


tell application "iTunes" to activate


This will force the app to the "top" even switching spaces if required. You can call if via any of the standard methods (NSAppleScript (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSAppleScript_Class/Reference/Reference.html) for example).

I would note that they API you used never promised to bring the app to the front/focus it, simply to launch it. Perhaps it used to work that way, but that was just "luck".

kainjow
Apr 14, 2008, 03:24 PM
A more direct approach would be to use the Carbon function SetFrontProcess(). Go here (http://www.cocoadev.com/index.pl?HowToBringOtherApplicationToFront) for more info.

Also you could use the NDProcess (http://homepage.mac.com/nathan_day/pages/source.xml#ndprocess) class to do this, which is just a wrapper around the Carbon process manager functions.

cowgp
Apr 14, 2008, 03:27 PM
Wonderfully simple help... thanks! it worked perfectly.

You are correct, the launchApplication API never said it would focus the app, but I couldn't find any API for doing it and it was "doing" it... so...

anyways. thanks again for the help and quick response.

HiRez
Apr 14, 2008, 05:44 PM
Strangely, one of the options for the NSWorkspace method launchAppWithBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifier: is NSWorkspaceLaunchWithoutActivation, which will, according to the docs, "Launch the application but do not bring it into the foreground." Which to me would seem to imply that the default is for the application to be brought to the foreground (since there is no other option to explicitly bring the app to the front). Then again, maybe that method isn't calling launchApplication: directly and works differently, activating by default.