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
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.
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
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.
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?
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.
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 …