How do I get the icon of the user's Mac? - objective-c

Using Objective-C and Cocoa, does anyone know how to get the icon for a user's computer (the one that shows under "Devices" and "Network" in Finder)? Not the harddisk icon, the actual one for a user's device. It ranges from a MacBook icon to the Mac Pro icon to a Windows blue screen of death monitor icon.
I've tried stuff along the following lines:
NSImage *icon = [[NSWorkspace sharedWorkspace]
iconForFileType: NSFileTypeForHFSTypeCode(kComputerIcon)];
But that just returns the same icon all the time, obviously. I've also tried the iconForFile: method but I don't know of a file path to use as the parameter. Can anybody point me in the right direction?

[NSImage imageNamed: NSImageNameComputer]
This will return the icon of the current computer

Another place to look for icons:
/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources
You can create NSImage objects with the files in there like this:
[[NSImage alloc] initWithContentsOfFile:#"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/com.apple.macbook-unibody.icns"];
It's probably not recommended to hard-code the value like that, however, since Apple may change the icons' locations. There is a file called IconsCore.h that contains many other constant values such as 'kToolbarDesktopFolderIcon' which can be used as follows:
[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(kToolbarDesktopFolderIcon)];
I believe these constants only work in Snow Leopard, though.

If you are looking for any other system icons check out Apple's sample project called "IconCollection". http://developer.apple.com/mac/library/samplecode/IconCollection/listing5.html
The sample comes with a plist file that has the names and codes for quite a few system icons that can be accessed using;
OSType code = UTGetOSTypeFromString((CFStringRef)codeStr);
NSImage *picture = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(code)];
where codeStr is the string code for the icon provided in icons.plist

Related

Render an icon inside NSCell (Cocoa OSX)

I have an NSOutlineView and a stack of objects, you can imagine it is a tree of files.
So I tried to extend the NSTextFieldCell class to parse the name of the current item and render an icon for it. But I am still stuck in the icon part. I simply can't get a standard-hardcoded-image to work!
I tried many tutorials, the only one I got to work is a class called PXSourceList, but it was designed for OSX 10.7+. Also the majority of these tutorials use AppDelegate with the NSOutlineViewDataSource protocol and I also want the code to be managed elsewhere, not in the APPDelegate class.
Can someone give-me some directions on the first steps? I think a bit of enlightenment on how the general logic surrounding the icon thing would be enough. I appreciate!
I use XCode 4.2 for Snow Leopard. The project I'm on is supposed to work in OSX 10.6+, so I can't use the new Lion approach of cells using NSViews.
You can get the file icon from its path as follows;
NSImage *iconImage1 = [[NSWorkspace sharedWorkspace] iconForFile:filepath];
You need an image cell to display the icon image.

NSUserNotification with custom soundName

Did anyone manage to make a NSUserNotification soundName to work with a custom sound?
I tried with aif and caf format 44100KHz 16bit 2 second of duration. The notification is displayed at the proper time, with the right title and text, but the default sound gets played instead of my custom sound.
The sound files are correctly copied in the application bundle.
If I try this the sounds work ok:
NSSound* sound = [NSSound soundNamed:#"morse.aif"];
[sound play];
But when I use the same sound in my notification, the default notification sound gets played:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSUserNotification* notification = [[NSUserNotification alloc]init];
notification.title = #"Titolo";
notification.deliveryDate = [NSDate dateWithTimeIntervalSinceNow:10];
notification.soundName = #"morse.aif";
[[NSUserNotificationCenter defaultUserNotificationCenter]scheduleNotification:notification];
}
I tried with and without extension, but with no success.
notification.soundName = #"morse.aif";
notification.soundName = #"morse2.caf";
notification.soundName = #"morse";
none of these work.
My application is not signed and not sandboxed, but I don't think that's necessary for user notifications, and apart from the sound problem the notifications work great.
It seems to me like this issue is case-sensitivity. Using notification.soundName = #"Morse"; works perfectly for me. As you can see in the NSSound documentation for soundNamed The search folders for sounds are, in order:
~/Library/Sounds
/Library/Sounds
/Network/Library/Sounds
/System/Library/Sounds
If you look in the last one, which is probably where you're trying to pull from since they're the sounds in System Preferences, you can see their names
So keep the case of the file, and omit the extension and it should work as expected.
If you have multiple versions of your App binary notification center may be searching the wrong binary for your sound files.
If you make sure to delete any old copies of the binary it should fix the issue.
From the Apple Dev Forums: https://devforums.apple.com/message/708511
This can happen if you have multiple version of your app binary floating around. NotificationCenter only fines one of them. If it is the one without the sound then it will not work.

How to add the iOS "Open In..." feature to an app

I would like to know how to present the "Open In..." Action Sheet (iPhone) / Popover (iPad) from my app, preferably an IBAction
I would hope that it'd be similar to declaring a file type then creating the view and opening the app selected by the user, but I know it is more complicated then that.
I realize that a similar question has been asked on StackOverflow, but I cannot make sense of the answer that was accepted: How to use "open in..." feature to iOS app?, and I have found some Apple Documentation on Document Interaction Programming. But, I can't really make sense of these.
Create a UIDocumentInteractionController by using the interactionControllerWithURL: class method (pass the URL of the file you want to open in another app).
Then call either presentOpenInMenuFromRect:inView:animated: or presentOpenInMenuFromBarButtonItem:animated:. The controller takes care of presenting the popover with available apps for that file type and opening the selected app.
If you want to know when the menu was dismissed and which app was selected, you need to implement the UIDocumentInteractionControllerDelegate protocol.
omz makes some good points on how to do that in his answer, however this procedure is much easier with the introduction of new APIs in iOS 6. Here's a simple and efficient way to show the UIActionSheet Open-In-Menu in iOS 6 and up:
NSArray *dataToShare = #[contentData]; //Or whatever data you want to share - does not need to be an NSArray
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
Also, if your app is compatible with versions of iOS lower than 6.0 you may want to check if the Share Service exists:
if ([UIActivityViewController class])
Once you present the sheet, iOS will automatically handle the rest for you. It will display a beautiful uiactionsheet with icons showing each app or service the user can open / share your data with:
Note that depending on the contents of the data, iOS will show different services in the Share Sheet
EDIT: The method above shares the file content, but not the file itself. Refer to omz's answer for more on that.
I've personally never had to do this, but your answer can most certainly be found in this Apple Documentation: http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/PreviewingandOpeningItems.html#//apple_ref/doc/uid/TP40010410-SW1.

Cocoa button won't display image

Just started exploring Cocoa so pretty much a total noob.
I've written a very simple game. Set it up in Interface Builder and got it working fine.
It contains a number of buttons and I'm now trying to get the buttons to display images.
To start with I'm trying to get an image displayed on just one of the buttons which is called tile0 .
The image file (it's nothing but a green square at the moment, but I'm just trying to get that working before I attempt anything more exotic) is sitting in the same directory as the class file which controls the game.
I have the following code sitting in my wakeFromNib method:
NSString *myImageFileName = [[NSString alloc] init];
myImageFileName = #"greenImage.jpg";
NSImage *myImage = [[NSImage alloc] initByReferencingFile:myImageFileName];
[tile0 setImage: myImage];
Trouble is, the game runs fine, but the image isn't appearing on my button.
Is there someone who could kindly tell me if I'm doing something obviously wrong?
Many Thanks.
The first problem is you allocate a string but then replace it with a string constant. That isn't causing your image problem but it is a memory leak.
I've never used initByReferencingFile, I usually just use imageNamed:. There is an example of using imageNamed here.

How to get NSImage of generic folder icon on OS X 10.5 and 10.6

I'm writing a Cocoa application that displays the contents of an archive file in an NSOutlineView. I provide a custom icon for an NSBrowserCell in the outline column, and it works great, but I ran into a little snag — using [NSImage imageNamed:#"NSFolder"] returns the correct image (a generic folder icon) on 10.6, but on 10.5 it returns nil, so all the folder icons disappear. (I can't find any documentation on the NSFolder icon name to see when it was introduced — I got it working by trial and error.)
Is there a non-deprecated method for getting a generic folder icon that works on both 10.5 and 10.6?
[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)]
(I think)