Detect Application Launch Event - macos

Is there a way to somehow hook into the system event of an Application Launch in Mac OS X? For example, let's say that I want a simple script or program to run each time a specific application is opened. I'm hoping that there is some sort of Cocoa API to do this, but I have not found one yet.

Ah, I figured it out on my own. There's a class called NSWorkspace that contains a NSNotificationCenter that you can add observers too. I added an observer for name "NSWorkspaceDidLaunchApplicationNotification" and this seems to do the trick.

Related

Programmatically shutdown, restart, sleep in a sandboxed cocoa application

I have a problem with the apple sandbox and the functionality of my app. The app should put my mac into sleep, or shutdown or restart it. I am using "com.apple.security.temporary-exception.apple-events" as the entitlement for sandbox with the value "com.apple.finder". I have imported ScriptingBridge and generated a Finder.h file which i have also included in my project like its proposed in https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ScriptingBridgeConcepts/UsingScriptingBridge/UsingScriptingBridge.html#//apple_ref/doc/uid/TP40006104-CH4-SW12
Then i tried to use the provided methods from Finder.h like shutDown, restart or sleep but they just don't work.
FinderApplication *theFinder = [SBApplication applicationWithBundleIdentifier:#"com.apple.finder"];
[theFinder shutDown];
Can anyone tell me how i can implement these functionalities in a sandboxed app?
Many Thanks!
Checkout https://developer.apple.com/library/mac/qa/qa1134/_index.html
This guide provides the exact code you are looking for. I have tried this and it works perfectly.

Reacting upon iTunes launch with NSDistributedNotificationCenter

I'm trying to intercept when iTunes starts up on Mac OS X, so I can relaunch my application to work around few bugs in the iTunes Framework.
What I did is to temporarly disable the sandboxing of my application and listen to all NSDistributedNotificationCenter notifications in order to examine them and pick the ones I want.
What I found is that upon start iTunes apparently sends this event:
object: com.apple.iTunes.help name: HelpBookRegistrationDidChange userInfo: (null)
which seems rather unique but also doesn't give an exact clue it is related to a startup event only at 100%.
Does anyone know if this is safe way to intercept such event?
If so, once my app is sandboxed again I need to ask temporary permission to listen to this kind of event, how can I do that in code?
Do you believe scripting bridge can help in this case?
You don't need to disable sandboxing for this, just observe NSWorkspaceDidLaunchApplicationNotification in the notification center provided by NSWorkspace, not the distributed one.
You'll get an instance of NSRunningApplication in the user info of the notification, which you can use to determine whether the launched app was iTunes (use the bundleIdentifier property).

Detecting Full screen applications on mac

I am developing a simple application in Cocoa, and I want to detect whether any application is running in full screen mode. Is this possible?
Through runningApplications API, I can get various informations but there is no specific property related to full screen mode. Does any one know how to detect it? Is there any carbon event or API for this?
I ran into this in the spring and spent forever trying to get it to work. I ended up packaging my code up into a little GitHub project, but I completely forgot to share it here.
https://github.com/shinypb/FullScreenDetector
Hope this is useful for someone.
Anyways after trying out so many options and digging into the NSWorkspace i have found way through which we can achieve this their is notification
"NSWorkspaceActiveSpaceDidChangeNotification"
Apple doc says "Posted when a Spaces change has occurred." so by using we can register for it. along with this we need to use the NSWindow's property "isOnActiveSpace" , so by this we can detect when application enters full screen mode and exits from it.
You want to key-value observe -[NSApplication currentSystemPresentationOptions]. When the active app is in full-screen mode, that property will include NSApplicationPresentationFullScreen.

Detect global paste events in cocoa

within my app I'd like to know when something is pasted from the clipboard, even if it does not happen in my application. So if a copy happens in let's say Safari and the paste is happening in let's say TextEdit, then I'd like to get a notification of that in my own application. Is this anyhow possible?
I found two articles on hooking API calls on Mac OS
Is it possible to hook API calls on Mac OS?
and
Hooking Cocoa API?
but i have no idea if this can help me.
Any help is appreciated!
Thanks a lot!
You can observe the keyboard input using Quartz Event Services or Carbon Event Manager.
Note that sandboxing your app will break this feature.

Observe other application quit or sudden termination in cocoa app

I need to create wrapper around application, so I created little cocoa app that opens application with [[NSWorkspace sharedWorkspace] launchApplication:…], can I register some event when that application quits or terminates (I certainly need to get event if app is finished good or bad way). I know that I can ask if such application is running every second, but I hope that there is a better way.
Have a look at Technical Note TN2050 "Observing Process Lifetimes Without Polling"
Particularly the NSWorkspace notifications NSWorkspaceDidLaunchApplicationNotification and NSWorkspaceDidTerminateApplicationNotification

Resources