Change Icon of Running Application via NSRunningApplication? What's the setter? - objective-c

I'm new to this objective-c (1 week)
What is the setter called for the icon property via NSRunningApplication?
Where can i find this in the docs just for my future reference so everytime i need to learn a setter i dont have to ask. :)
I noticed in docs [[NSRunningApplication currentApplication] icon] is a property. Is it possible to set this icon property? Will it change the icon used in dock and the mini icon shown in the minimized window? I point out the mini icon in minimized windows in this image below:

There is no setter. The icon property of NSRunningApplication is readonly.
NSRunningApplication can sometimes refer to the current application, but it's more general than that. It can also refer to other applications on the system. It wouldn't be alright for any application to be able to set the icon of any other application, would it?
Anyway, you want to be working with the main application object of the process, which is the instance of NSApplication. You can get access to that object using [NSApplication sharedApplication]. You can also use the global variable NSApp.
NSApplication has an applicationIconImage property that's read-write. Setting that is the easiest way to change the application's icon.
Alternatively, you can obtain the application's dock tile object (an instance of NSDockTile) using the dockTile property. You can then set a custom view for the dock tile's contentView and that view can render however you want it to. (You can use an NSImageView if you just want to display an image.)
Unfortunately, neither of these approaches will change the badge on the icons of minimized windows (although the class overview docs for NSDockTile say it will). There's not a direct way to do that. You can obtain the dock tile of a minimized window and set a custom view, but then you're responsible for drawing everything about it. There's no way to just change or replace the badge part.

Related

Using multiple windows with Storyboards (Mac OS X development)

I have two window controllers (with their own view controllers) on a storyboard.
In one window, I have the main program, a basic text editor with an NSTextView. In the other window, I have a single button.
I found out how to get the window to display by linking it to a menu item. It works.
The main window is linked to my ViewController class by default. The second window is also linked to the ViewController class and has its button linked to an IBAction in the ViewController class.
I have some simple code in the IBAction that basically tells the NSTextView to change its font size to a much bigger font. I have confirmed that the code itself works when called in other methods.
The button works, BUT it is using an entirely different instance of my ViewController class. So in result: the text size doesn't change.
So my main question here is how do I get an IBAction in one window to affect an object in another window.
I hope I did an alright job at explaining myself. Keep in mind this is my first Stack Overflow question:) I tried my best to research this question but mostly found information on iOS development and using XIB files.
It sounds like you have two windows with the same controller class but want what happens in one window to affect the other window. The easiest way is going to be with notifications. When the button is clicked in one window a notification gets posted that all instances of ViewController receive and respond to by changing the font size as needed. You could also look into setting a user default when the button is clicked and using bindings to keep the text field's font size tied to the current default.

How to change NSToolBar background color?

I'm using a NSToolbar and I'm trying to change the color to a white instead of the default gray. How can I accomplish that?
Since the toolbar integrates into the window title bar, you need to draw both the title bar and the toolbar. Which basically means you need to take over drawing for the entire window.
This requires "swizzling" which, while it works well, is not supported by Apple and therefore may get your app rejected from the app store.
Basically you need to inject your own code into the NSWindow class, by using objective-c runtime functions to modify the class definition. Note that this will affect all windows in your app, so you will need to have an if statement checking if this is a window you want to modify.
Subclass NSWindow, and in the +initialize method, find the "frame view" class which is the one that does most of the window drawing, and rename it's "drawRect:" method to "originalDrawRect:". Then you define a new "drawRect:" method on the class, as a duplicate of a method in your NSWindow subclass. This method should first call originalDrawRect and then do custom drawing on top of it.
Note: you will be drawing over the top of the window title text... so'll need to change the the drawing mode to kCGBlendModeColor or something. Or else just draw the title string again. You can ask the window where the title text should be drawn.
Here's a full article with more details: http://parmanoir.com/Custom_NSThemeFrame

Transparent NSMenu

I'm trying to change the opacity of a NSMenu, I made custom NSMenuItems and changed their opacity but there is still the white color of menu behinde them, I'm looking for a way too change the color and opacity of the menu background. I was wondering if it's even possible?
Regards
I don't think there is an elegant way to do that. NSMenu isn't a subclass of NSView. The rendering view is probably a private variable of the menu object.
NSMenuItem has a view property that you can use. Try setting a custom view (if you don't already) and later find the menu's view by calling menuItem.view.superview. Then set that view's alpha property.
Alternatively you could try to find the private view property of NSMenu with a tool like class-dump. Note that using private variables is never a good idea. Especially if you're planning to release the app on the App Store.

Removing the Proxy Icon in the bar of my mac app

I was wondering how to remove the proxy icon in the bar of my mac app. I've added an image so you can see what Icon I'm talking about
Thank you in advance!
The icon is included in the titlebar of the application automatically when you've created an NSDocument based application.
You can remove the proxy icon by returning nil from the -[NSWindow representedURL] method. This could be accomplished by using a custom NSWindow subclass with the method overridden; or simply setting the property to nil at the appropriate times.
Be aware, you might loose other functionality you normally get for free by changing this behavior, such as the dirty/clean indicator for the window, or some prompting to save when closing the window.
Alternatively, if you wanted a different image, you could use:
[[NSWindow standardWindowButton:NSWindowDocumentIconButton] setImage:customImage]
Then implement -[id<NSWindowDelegate> window:shouldPopUpDocumentPathMenu:] to return NO to prevent the popup menu from appearing.
If your application isn't actually document based, or the window doesn't represent a document, consider refactoring to present this window a different way, rather than being a document window.
There is some additional information in the Cocoa window documentation.

How to check if a NSWindow is visible

Is there a way to check if a NSWindow is visible or not? I want to display a sheet controller once the first window of my app became visible (the animation on 10.7 ended and the user can see the window!). If I just show the sheet in windowDidLoad, it results in a stupid looking animation (sheet rolling out, window popping out from the back). I know that NSWindowDelegate provides two methods which are invoked when a window either became the key window or the main window, however, this doesn't have to mean that the window is already fully visible at the time. This is even more noticeable on Lion where windows tend to pop up with this stupid animation.
I would go for something like this:
if ([myWindow isVisible]) {
// Do stuff
}
Or an an observer for this key path to be notified when the change occurs.
For what it's worth, you can also bind to the window.visible property. Xcode 4 may squawk at you, saying it's not a bindable property, but it will work.
This can be useful if you are trying enable/disable show/hide NSStatusItem based on whether the window is visible, as well as other approaches.
i.e. in Interface Builder:
Bind to: App Delegate
Model Key Path: self.window.visible