How to change the icon of file in MacOS in Objective-C? - objective-c

My program for MacOS is creating the alias to another file and I want this alias to have some designated icon. How can I do it with Objective-C, preferably in Cocoa?

You can use NSWorkspace, which has a method for doing this:
//anImage is an NSImage object and
//pathToFile is a path string
[[NSWorkspace sharedWorkspace] setIcon:anImage forFile:pathToFile options:0];
This will save the icon data to the file's Resource Fork. These days Mac OS doesn't use actual resource forks, so the image is stored in an HFS extended attribute named com.apple.ResourceFork.
Extended attributes are filesystem metadata attached to a file. You can see the extended attributes attached to a file using xattr and related commands.
Note that extended attributes are only guaranteed to be reliable on HFS+ file systems, if the file is located on a file server that doesn't support file metadata or on an external drive with a non-HFS file system, the icon may not be written.

Related

How to build with a custom eglfs cursor atlas?

I'm trying to change the eglfs mouse cursor graphics for my embedded linux QT application (QT5.5). I have the new cursor atlas PNG and the new JSON descriptor file, but the documentation is rather vague:
".. a custom cursor atlas can be provided by setting the QT_QPA_EGLFS_CURSOR environment variable to the name of a JSON file. The file can also be embedded into the application via Qt's resource system."
I'd prefer to keep everything within the resource system if possible but I can't work out how to do it.. do I need a specific qrc file containing the path to the JSON file? I assume that the PNG file would also need to be added as a resource so that it gets built into the application?
If adding it via the resource system is a bad idea where's the correct place to set the QT_QPA_EGLFS_CURSOR environment variable? I'm currently specifying the platform on the command line via "-platform eglfs"; will this be ok or will I need to set the platform to eglfs in the build?
After much trial, error and digging around I have found the solution that I was looking for within the resource system.
Create a new resource file called "cursor.qrc", the contents of which needs to be two lines:
path/to/your/custom-cursor-atlas.png
cursor.json
The first line (path to your cursor atlas) must be relative to your resource directory.
You then need to put the JSON file (contents as described in the documentation) in the root of your resource directory. It must be called "cursor.json", and its image location line must must match the location in your new resource file and be of the format:
"image": ":/path/to/your/custom-cursor-atlas.png",
This will then include your cursor atlas in resources, and Qt will find it when your application starts.
Run time solution example:
export XDG_RUNTIME_DIR=~
export QT_QPA_EGLFS_CURSOR=~/cursor.json
In the cursor.json:
"image": "cursor.png",
Put your custom cursor.png atlas into your home dir (~) then run the Qt app from there.

In Obj-C, how to programmatically set the default "open with" property of a file in Mac OS X

In creating some .mov files using Cocoa (Obj-C), I'd like to set them to be opened by default by a specific program, instead of the default. This should be a file level property, I do not wish to change the default program for all files with the same extension. This is to be done from Cocoa itself, as opposed to manually in "context menu">>"Get Info">>"Open With".
There's an undocumented function call that sets this:
// undocumented function call
extern OSStatus _LSSetStrongBindingForRef(const FSRef *inItemRef,
FSRef *inAppRefOrNil);
*If you use this in your application and submit it to the AppStore it will probably get rejected.
As an intermediate between doing it by hand and doing it from Cocoa, there is an Automator action called "Set Application for Files".
I don't think there is a supported way to do it programmatically, but some people have figured out what Finder is doing: Adding a resource of type 'usro' that contains a full path to the application. See for example this discussion. Note: the Resource Manager is deprecated as of 10.8.

path of suporting files in OS X program?

I need some help for my OS X program.
I need the URL of a file inside the supporting files.
I have an array in which I save URLs from images and add them to a table view and if no images are chosen I want to add a question mark image (it is called "bild.jpg")
This bild.jpg is inside the supporting files but for later use I can't just save the name of the image because the array stores also URLs.
I need to have the URL of that image in the supporting file because it's easier to use the array for image initialization.
Is there a function to get the path or is there a standard path to the supporting files? I already search on the net but couldn't find anything that could help.
You seem to be talking about the application bundle and its resources directory rather than, say, a subdirectory in ~/Library/Application Support/..., in which case you probably want something like:
[[NSBundle mainBundle] URLForResource:#"bild" withExtension:#"jpg"]
(See the documentation for NSBundle.)

how to open an apple unsupported file in our ipad app

i need to open an Apple unsupported file in my ipad app using "Open In" feature. The file extension in ".lasso" . I said that file is unsupported by Apple, because i cant find it description in Apple System-Declared Uniform Type Identifiers.
I was try to solved that using CFBundleDocumentTypes in info.plist, but i still can not open it. i was write this in my plist :
but when i try to open a .lasso file from another apps, there always show an alert "Can not open this file in another apps".
do somebody know how to solved it?
You need to create your own UTI for your file type, something like com.yourcompany.lasso. Your Info.plist is claiming to export the public.plain-text UTI which of course already exists. You would also list the pre-existing UTIs that your new UTI conforms to. You don't show what's under the "Conforms to UTIs" key in the screenshot, but it should apparently be public.plain-text if .lasso files are indeed plain text. (If there's a specific encoding for .lasso files, such as UTF-8, you should consider something more specific, such as public.utf8-plain-text.)
Also, you should either have a custom MIME type or not use one at all. You don't want to redefine the "text" MIME type to suggest that all data streams of that type are Lasso files, do you?
And why are you using all-caps for "LASSO". I assume that, in real use, files will have an extension in lowercase like ".lasso". Right?

Objective C: Register file extension

I am creating an app which needs to be opened if a user double clicks on a file with a certain extension.. How do i register the file extension with my app? and then read the contents?.
E.G the file could have the extension words.ThisApp and it could be in XML Format.. how could I read that in objective c into an array?
I think you should read the Document-Based Applications Overview.
To register an extension to your application, bring up the Target info window (Project » Edit Current Target "My Target"... at the bottom) and open the "Properties" tab. Fill in the blanks for your document type there. For more info, read Storing document type informations in the Application's Property List, contained inside the above guide.
To read XML data, consider using a NSXMLParser (google it for examples) to drive the results into a NSMutableArray as you see fit; and to get the data into your application, consider using a NSDocument subclass, as suggested (again) in the document-based application overview.
As you might understand, this document is quite a vital read.