Tell macOS that a custom file format without a .png extension is a valid png - macos

I need a custom file format for my application and I thought that I could make a superset of PNG. macOS shows previews of regular PNG files (and APNGs with a .png extension) in Finder. I want macOS to show a preview of my file format even though it doesn't have a .png extension. I need to tell macOS that files with a .px2 extension are valid PNGs that can be decoded by a regular PNG decoder.
I've been reading this page trying to find the right set of keys to use but I'm not having any luck. I thought that NSExportableTypes might be the answer but that doesn't seem to be it.
To test this, I'm changing the extension of an APNG file from .png to .px2. I realise that I could just use the .png but I think that could be a little confusing (both for the user and the OS).
There's a slight chance that what I'm trying to do is impossible!

I think you may be looking at 2 different problems: one is the OS recognizing the file type and linking it to your application, the other is being able to show the preview.
The latter is going to be highly dependent upon the way that the Finder's in-built QuickLook plugin works. You may need to just implement one of those yourself.
Debugging these kinds of issues can be a little tricky, because you need to make sure macOS has assimilated your NSExportableTypes. One quick check is to drop into Terminal and use mdls <file of your type and extension> and see what the kMDItemContentType and kMDItemContentTypeTree are for your file.
If it's not recognizing the extension at all, make sure it's been re-loaded by using lsregister which is hidden away in the LaunchServices Framework of CoreServices.
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister to get the man page
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -lint -f <path> to force reload of your application (the -lint) adds more detail on errors while interpreting the entries.
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -seed will reset the daemon and re-seed the data from the default applications and library locations.

Related

API or other method for accessing Finder icon preview images

It appears that macOS now generates preview images for certain files (notably RAW image files) that are not custom icons but something else. (You can toggle them on/off in latish model macOS via "show icon preview" in Finder window "Show View Options".)
I'd love to be able to either obtain these images or use the API that generates them but I'm obviously googling the wrong terms. The usual APIs will get me the file icon (which is usually a generic document icon) and not the preview. The tricks for getting custom icons (e.g. .DS_Store etc.) do not apply.
The tool you want is qlmanage. In particular, look at qlmanage -t (thumbnail) and qlmanage -p (preview). By default, it opens a viewer for you. If you want to generate files to process yourself, see the -o option. For example:
qlmanage -t image.png -o .
This will create a thumbnail file called ./image.png.png (it attaches .png to whatever the filename is).
If you want to build your own tools around this, see the QuickLook framework.
A little more info w.r.t. using QuickLook framework. The key method is QLThumbnailImageCreate (QuickLook previews are PDFs which are not what I want and likely not what someone doing something similar wants).
Even though the function name indicates that it creates thumbnails, it can create an image of any size (and the size parameter dictates a maximum dimension).

mac default application for any unknown extension

I have a series of files that are all text files that i want to open with text editor (let's say Sublime).
The extensions on these files follow the pattern:
file.sff123
file.sff124
file.sff125
file.sff126
and so on...
Every time i go to open one of these files, the dialog pops up to select the default application for this file type, but since they all have varying extensions, i have to navigate and select the new file type each time. I literally have 1000s of these files and would like to be able to open all of them. Further, i want to be able to open any unknown file with something like Sublime by default. 99% of the time its a random text file... the 1% that it's not i'll "open with".
Is there a way to get Mac to recognize any unknown file extension and open it with an application of my choosing by default?
I am not sure if it is possible, but the following (untested) may help you, or someone else work out how to do it.
OSX uses UTIs (Uniform Type Identifiers) to categorise filetypes and map them to applications - and this works in conjunction with Launch Services to launch the appropriate application.
I don't have one of your .sffnnn files available, but I think you need to run mdls on one and see what kMDItemContentType is. You can do that like this in Terminal:
mdls -name kMDItemContentType somefile.sff123
I am hoping you get the same content type for all your .sffnnn files - if you don't, this approach won't work.
You then need to edit your LaunchServices.plist file which, I think, is in ~/Library/Preferences/com.apple.LaunchServices.plist and make an entry corresponding to the filetype you found above that maps to the sublime application. I suggest you look in there, or the system-wide version to see how entries look.
Once you get the hang of it, you may be able to do it from the command-line with a command like:
defaults write com.apple.LaunchServices LSHandlers -array-add '{LSHandlerContentType=xxxType;LSHandlerRoleAll=xxx.yyy.sublime;}'

"Revert" command in AppleScript on OS X Lion

I'm in the middle of putting together a script that manipulates an OmniGraffle document and saves the result of each progressive manipulation as a PNG. That's all working fine, but at the end of process, I want to close the file without saving the changes.
Naturally, I reached for the "revert" command. Except that "revert" doesn't appear to be a command on Lion. I tried "close saving no," but since it was autosaving as each manipulation was made, that didn't work.
Of course, I can simply leave the document open and manually revert it. Nevertheless....
Am I missing something? Is there truly no way to programmatically roll back changes made to a file using AppleScript on Lion?
Update: it appears to be worse than I expected — on a volume without permanent version storage, at least — after many manipulations with AppleScript, "Revert Document…" was no longer even available in the File menu.
I can only think of a workaround: since you're not interested in keeping those intermediate versions anyway, you could:
make a backup copy of the file prior to opening it in OmniGraffle
do your changes + PNG exports
close w/o saving
overwrite the file with the backup copy (and delete the backup)

Cocoa: font downloaded from the web

I'm coding an app that needs a font that is not available on the system. I got the font from the web, but every time I launch the app, I get this message:
http://twitpic.com/3h4nj9
I cannot ship an app with this kind of message. I believe it's some metadata on the font file. Does anyone knows how can I solve that problem?
Thanks!
This is a security feature of the OS. You should not be trying to "solve" it.
I'd suggest obtaining a license for the font and bundling the file as a resource in your app. Or do without.
[EDIT]: I misunderstood. Since it's a file you're including in your bundle, you want to clear the com.apple.quarantine metadata from the file. Use the command
xattr -d com.apple.quarantine my_font_file.otf
You probably also want to get rid of the com.apple.metadata:kMDItemWhereFroms metadata as well.
You can just use
xattr my_font_file.otf
to get a list of all the extended attributes that the OS is keeping on the file. Delete as appropriate (with the -d flag).

Writing a simple app to convert files to pdf

I want to create an application on a Mac to convert multiple files (txt, pdf, doc, html, etc) to a single pdf file that can be printed. The real point is that if you have 50 texts you don't have to open every single file and click command-p.
I'm not quite sure whether the best way to do this is by creating a full-fledged app or an automator plugin (or something else). If I remember correctly there's a filter in mac os's terminal that can convert files to pdf (but I forgot what it's called).
So would an automator plugin do this well, or shall I make an app for this? Can you provide me advantages for each answer?
I've done cocoa touch programming before so I can write objective-c quite well.
Use appscript, either as an action in an automator script or standalone. The advantage is that it is very simple and will take you a fraction of the time to write an app.
Here is something very close to what you want. It sets up a drop-folder and each file dragged onto it is printed (you can use multiple-select to get what you want). It uses Apple Works 6 which doesn't support the file-types that you want.
To modify it to use the Preview application instead you need to change the tell command in the script and then google the dictionary for Preview to check which verb to use for printing.

Resources