I was wondering if there was a way to write a Cocoa app that listened for OS system events such as an application launching, a window switches, etc, etc. My thought was to watch [NSNotficationCenter default] for messages but so far I haven't been able to find much. Does anyone have any suggestions?
Thanks!
Some messages that might interest you would be provided to [NSWorkspace notificationCenter], such as when apps launch, come to the front, or quit.
I'm not sure what you mean by "window switches". If you mean changing which window is in front without changing which app is in front, that's generally considered "none of your business" on the Mac.
Related
Now, we are developing a window application for play video like TV.
But When it's running full screen, we must block all window notifications.
so i found many ways to disable notifications.
http://www.maketecheasier.com/disable-toast-notifications-in-windows-8/
But we can't use this way, because it's for only Window App Store Applications.
We want to block all Notifications.
connected USB noti, window update noti, etc.
In addition, Our application has property of the Topmost(base on .net C#).
But it can't Blocked.
is it possible to use the user32.dll??
How to solve it, anyway is ok.
sorry for my poor English. Thanks.
Is there a way to programmatically turn on and off the Bluetooth connection on OSX in a way that will be accepted by the Mac App Store?
From my previous question, I've found about blueutil, but it uses private APIs.
It would be somewhat surprising if Apple approved an app that modified the user's antenna settings. It sounds like the kind of thing they typically frown on, no matter how you do it. But then, sometimes I get surprised.
You can definitely do it through Applescript:
tell application "System Preferences"
set current pane to pane "com.apple.preferences.Bluetooth"
tell application "System Events"
tell process "System Preferences"
set isOnCheckbox to checkbox "On" of window "Bluetooth"
if value of isOnCheckbox is 0 then
click isOnCheckbox
end if
end tell
end tell
quit
end tell
Note that this will take over System Preferences and at the end close it even if the user was running it. That's not the best user experience, and I definitely wouldn't do it without first warning the user. But of course, I wouldn't recommend modifying the bluetooth settings without warning the user.
EDIT
Because you asked, I'll take a moment to rant here....
Regarding how to learn to read and write the above, first note that it, like most AppleScript I write professionally, was cobbled together from google searches and experimentation. I'm a purist programmer at heart, and I believe in really understanding the technology you use. Even I cobble together things in AppleScript until they "kind of work."
I wish there were a really good document. Of course there's the language guide, but it's kind of like learning Cocoa from the ObjC language definition. My current recommendations are Beginning AppleScript and then AppleScript: The Definitive Guide. Neuburg in particular does not sugarcoat the language or pretend that it makes sense. Applescript, even worse than the original COBOL (ADD X TO Y GIVING Z), is very hard to write because it tries so hard to be easy. I love and respect many languages. AppleScript is crap.
It is, however, the most supported way to interact with most of the Mac system functions, so a good Mac developer needs to at least be able to get by in it. Even if you use the new ScriptingBridge via ObjC (or MacRuby), the underlying object model is still AppleScript based. In my experience, to get ScriptingBridge code to work well, you generally have to write it first in AppleScript and then translate it into Cocoa.
This worked for me in 10.15.6, I might have over complicated my solution which is running script 1 (turn bluetooth off) and then script 2 (turn bluetooth on).
Script 1. This is for turning bluetooth OFF
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
click
click menu item "Turn Bluetooth Off" of menu 1
end tell
tell window 1
click button "Turn Bluetooth Off"
end tell
end tell
Script 2. This is for turning bluetooth ON
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
click
click menu item "Turn Bluetooth On" of menu 1
end tell
end tell
So I execute one command which will run one script after the other, the sleep is for the UI to update properly.
osascript bluetooth_off.scpt && sleep 3s && osascript bluetooth_on.scpt
You can just save the command in a file and execute it using: (they have to be in the same directory).
~ bash <fileName>
Note: You will need to give access to the terminal on Preferences > Security & Privacy
If you aren't familiar with Cinch, its an application on Mac App Store that allows you to resize ANY window to half/full screen size if you drag the window to the edge of the screen. Exactly like the functionality in windows 7.
Now my question is, how is it done? I have looked all over cocoa apis looking for notifications/delegate methods for whenever a window is being dragged (ALL windows, not just windows owned by the app from which code is running from) but can't find it. Looked in Core Graphics API...Quartz Display Services....but can't find it.
Any help will be greatly appreciated as I have been looking for the past week....Thanks!
Edit: Resize the window is easy since it can be done through applescript bridge..
Are you developer behind i-Snap or some other Mac App Store clone of Cinch?
I'm the developer behind Cinch, and while I try to maintain an "abundance mentality" which basically says "There's enough out there for everyone", I've been upset by the Mac App Store lowering the barrier for entry to this market which has produced a number of half-backed competitors.
I would be thrilled to see some real innovation around the work I have done, and not just clones looking to make a quick buck.
Anyway, you want to look at the Accessibility APIs. It's a Carbon C API. This is probably your best reference: http://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/Intro.html%23//apple_ref/doc/uid/DTS10000728
I've not used the Cinch app, but if I were to do this I'd expect to be using cocoa events. (Also see here) Specifically the mouse handling events, combined with where the mouse is currently on-screen. They probably set a variable when a window is grabbed and then track the mouse pointer until it hits an edge or until they release the mouse button.
Events are very powerful and provide very low level access to what is happening, but can also be very complex. Good luck!
I'm not sure. Maybe the developers combine apple script and carbon events. You can create carbon events to know when the mouse has been clicked or dragged
I am developing an application for Mac OS , i need to display a information popup in Mac, In Windows OS context, it should be similar to the one, which used to be displayed near by tray-icon,
More real example is, assuming any messenger application is running, and someone form your contact list became online/available,then Messenger App display a Animated Popup near by tray-icon area,
the same use-case i am having ,
While googling i came to know, either i can make use of NSAlert or Growl , any other application that i should think.
There is no default alternatives to the Windows popup message on the Mac, but the de facto standard for doing this is through Growl. NSAlert popup messages are usually used to display exactly those: alerts. They are often to large and cumbersome to display small amounts of information well without distracting or interrupting the user. Growl, on the other hand, works well for things like these, and is what you should use.
Is there a way to make a "makeKeyAndOrderToFront" without beeing in the Application?
If I am in Safari and in the Menu Bar there is a MenuItem to my App, after I press this, i want to show a window of my App in the Front (makeKeyAndOrderToFront makes this just if you are in the Application).
Which way can I use? And how can I animate this Window (like Tweeties Add new Tweet -> atebits.com).
Thank you!
As you've discovered, changing the window order within your application does not have any impact on which application is active. You can bring your application to the foreground by activating it:
[NSApp activateIgnoringOtherApps:YES];
If your application had a dock icon the behavior would be similar to the user clicking on it.
Applescript would be a trivially easy way to make your app frontmost. It won't be doing any animation, though.
tell application "System Events" to set frontmost of process "My Application" to true
Perhaps the best way to accomplish what you want, if I understand the question correctly, is to have your application respond to an Apple event which you send to it after your menu item is selected which will bring your application and desired window to the front.
Provided your application has a connection to the window server (which it will if it's using AppKit or Carbon), you can use the Core Services function SetFrontProcess() to bring it to the fore. SetFrontProcessWithOptions() can be used to bring only the frontmost non-floating window of your app to the fore. These functions appear to be available to 64-bit applications, but if you're targeting 10.6+, you're likely better off using the NSRunningApplication class. Something like
[[[NSRunningApplication
runningApplicationsWithBundleIdentifier:ident] lastObject]
activateWithOptions:0]
should do the trick.