Programmatically shutdown, restart, sleep in a sandboxed cocoa application - macos

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.

Related

User preferences are not saved from XPC service

In my main app bundle I have supporting XPC service included. App is sandboxed and everything works quite fine, except that when I call [[NSUserDefaults standardUserDefault] setObject:forKey:] method and than - synchronize method from the XPC service app, preferences are not written and data cannot be retrieved next time I need it.
I didn't find anything related to this problem in Apple's documentation, except that the sandboxed app cannot access preferences of other apps. That's all right, I don't need it. XPC service has its own container in ~/Library/Containers, where it should be able to store its own data, I'd suppose. But obviously it's not the case for some reason.
I probably missed something, but cannot find what. Is there anything special which needs to be done (adding some entitlement or so) in order to make this work?
Thanks for any tips.
I believe you'll need to use Group Containers to share the preferences and I have achieved something similar (a non-UI LSUIElement app sharing preferences with its conventional Preferences app countpart) using RMSharedPreferences.

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.

Communicate between helper app and main app in sandbox

I have a status bar application from which I can launch a helper application for preferences. All preferences are stored in NSUserDefaults in the helper app and are read using addSuiteNamed: in the main app.
I would like to notify the main app when preferences are changed in the helper app. What is the best way to do this?
I have tried using NSDistributedNotificationCenter but this is too slow and not very reliable.
RMSharedPreferences by Realmac Software seems to be what I was looking for. It allows communicating between multiple apps and it is very easy to setup and seems to be way more reliable than NSDistributedNotificationCenter.

Can Mac App Store Sandboxed apps use CGEventPost?

For an app I am working on I am using CGEventPost to post some key events and I noticed that it stopped working with sandboxing enabled, even I turned on the "Enable Access for Assistive Devices" system preference.
Is there an entitlement to allow the use of CGEventPost? Or, is there another way to post key events with apps submitted to the Mac App Store?
Can Mac App Store Sandboxed apps use CGEventPost?
No.
Is there an entitlement for CGEventPost? Or is there any way to post key events with apps submitted to the Mac App Store?
No.
If you look at the developer forums, Apple has made it very clear that they will never enable an entitlement for this, because it allows a blatantly obvious way for any app to escape the sandbox.
If you have some more specific need that you can justify (not "post some key events" but "get iPhoto do to XXX, which as far as I know can only be done by posting event YYY at it"), file a bug report, and post a thread on the dev forums.
This will do two things:
Someone at Apple may suggest a workaround--possibly a private one that nobody else can tell you and/or one that isn't normally allowed on the App Store but will be accepted if you've been told to use it.
It increases the chances that Apple will invent some better way to do what you want in the future.
Old question, but accepted answer is wrong. There are apps in Mac App Store that are using CGEventPost to simulate events. For example apps "Remote Mouse" and "Mobile Mouse Server" do that. They can send input to any other app and based on symbols in those apps they are clearly using CGEventPost-function. I would like to know how are they doing it.

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.

Resources