Cocoa analogs to application start and stop paradigms

Now that I’ve been using ASOC for a while, I want to move some of my application startup and shutdown code into Cocoa, as part of a longer-term effort to get rid of some of my Applescript code, altogether.

I’m wondering if anyone can point me to documentation which will explain what the Cocoa analogs are to the following Applescript paradigms …

Start an application:

tell application "TheApplication" to activate

Stop an application:

tell application "TheApplication" to quit

Wait for an application to fully open:

tell application "System Events"
    set theProc to a reference to process "TheProcess"
    repeat until exists theProc
    end repeat
end tell

Wait for an application to fully shut down:

tell application "System Events"
    set theProc to a reference to process "TheProcess"
    repeat while exists theProc
    end repeat
end tell

Thanks in advance.

Have a look at NSRunningApplication. It has methods to activate and terminate applications, as well as a couple of properties, finishedLaunching and terminated, to check whether an app has finished launching or closing. The method to activate an application only works on apps that are already running, so to launch an application you can use NSWorkspace’s launchApplication: method.

Ric

Hi,

Cocoa applications with fully operational AppKit are normally launched by the user like Apple’s iApps.
If you want to launch an app programmatically, use launchApplication: or
launchAppWithBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifier: of NSWorkspace

To make an Cocoa app frontmost, use activateIgnoringOtherApps: of NSApplication or
activateWithOptions: of NSRunningApplication

For the own app

[NSApp terminate:self];

For other running apps terminate of NSRunningApplication

To wait for an application being launched or terminated subscribe to
NSWorkspaceDidLaunchApplicationNotification or NSWorkspaceDidTerminateApplicationNotification
of NSWorkspace

Thank you very much!

I have another related question. I want to be able to reposition named Terminal windows in cocoa in an analogous way to this Applescript procedure:


on positionTerminal_(titleString, xCoord, yCoord)
        tell application "System Events"
            set foundIt to false
            repeat until foundIt
                set winList to windows of application process "Terminal"
                repeat with win in winList
                    if title of win contains titleString then
                        set position of win to {xCoord, yCoord}
                        set foundIt to true
                    end if
                end repeat
                delay 0.2
            end repeat
        end tell
    end positionTerminal_

I only have to do this to Terminal windows, so a general solution is not necessary.

Thanks in advance for any suggestions or pointers to docs.

PS: Why does my indentation not appear within the Applescript tags? My code is properly indented, and if I edit this message, I indeed see the indentation.

My suggestion would be to stick to AS. Speed is irrelevant, and it’s going to be a lot more complicated in Cocoa.

Thank you very much.

I’m indeed not looking for speed here. I’m happy to keep this in AS, but now I’m curious as to how to do this in a non-AS way. I don’t mind complexity.

I know how to loop though a specified set of application windows in cocoa, and the only thing I don’t know how to do in that environment is to reposition a given window, i.e.

set position of win to {xCoord, yCoord}

Where should I look to get some info about how to do this in cocoa?

Thanks.

You do it with your app’s own windows using setFrameOrigin: or setFrameTopLeftPoint:. but how you do it to other apps’ windows, I don’t have a clue. I suspect some Carbon API, but I’m really only guessing.

OK. Thanks. I’ll continue to investigate this in my spare time, and I’ll report back when I find something.

I finally figured out the answer to my latest question. The following Objective C code positions a named window of a named application to the given coordinates …

Well done. Don’t forget that you have to do your own manual memory management with CF objects.

Thank you!

And I presume you’re talking about my calling CFRelease in the appropriate places. I’ll add that to the code and repost it a little later.

Here’s the new-and-improved version with CFRelease() …