Detect global paste events in cocoa - 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.

Related

Electron prompt support

I am looking for a native (OS X) prompt module when working on Electron, something like the window popped up when I click 'Join Other Network...':
This offers the ability to get inputs and process the data returned. So Dialog in Electron may not work here (at least I did not figure it out). I found this issue, but I cannot understand it.
Right now my approach is to open up a browserWindow to to ipc with main thread, but the most critical issue is that, it does not feel like native!
Any help is appreciated!
Electron currently does not have this functionality built in. As far as I know this UI is fully custom, the OS API does not have a direct way to create this UI. For this reason it is likely Electron will never have this functionality.
Your only options are to mimic the native UI in CSS or create a node module that accesses the OS APIs to build your desired UI.

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.

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.

OSX Carbon: Quartz event taps to get keyboard input

I want to get Keyboard Input on OSX using c++ without using Cocoa, deprecated Carbon UPP handlers and if possible without using IOHID since that's alot of extra work.
I allready implemented a simple mouse class using quartz event taps and it works like a charm and now I'd like to use them to implement a keyboard class. Anyways as the reference states under CGEventTapCreate:
http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html
you can only access key events if one of the following is true:
The current process is running as the root user.
Access for assistive devices is enabled. In Mac OS X v10.4, you can
enable this feature using System Preferences, Universal Access
panel, Keyboard view.
that is a very serious limitation since I also want my application to work without any weird settings. Is there any way to work around this? If not, is there any alternative to using Taps in Carbon?
Thanks!
The easiest way is to use the semi-deprecated Carbon function RegisterEventHotKey, see this SO Q&A, for example.
If not, you need to live with that restriction. The restriction is there to prevent a bad person to install keylogger behind the scenes. You need to ask the user to open the preferences, type the admin password etc.
You could try to use AXMakeProcessTrusted. This is supposed to be the same as Access for assistive devices on a per process basis.

Detect Application Launch Event

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.

Resources