OS X programatically enumerate all metadata keys for a given folder - macos

For a given folder on a HFS+ volume on OS X, how do I enumerate all metadata keys (not just the ones used by spotlight/finder, which may be accessed via xattr or mdls) for a given folder programmatically? Either in Obj-C/Swift or Applescript/bash? I'm unable to find any info on that. Thanks!

I see that "xattr" source is available on GitHub at https://github.com/xattr/xattr/tree/v0.6.2. It seems to be a combination of C and Python rather than the languages you name but you may be able to deduce the logic.

Related

Windows short file name

How do I manually create short file names in Windows if by default short file name is not assigned?
When we run command as below c:\dir x we can see the list of short file name.
But I want to create new short file name if not exist in list.
If you are asking about Windows not generating SFNs in general, then that question belongs on SuperUser instead of here. For the record, you can turn the registry entry back on as so:
fsutil disable8dot3 set 0
If you are asking about selecting specific SFNs instead of letting Windows automatically generate them, you can do that too:
fsutil file setshortname *LFN* *SFN*
Alternately, you can do it programmatically with the SetFileShortName command.
These only work with NTFS volumes. On FAT volumes, there is no practical way to specify the SFN. You would need to directly edit the directory entry, making sure to update the LFN checksums.

OS X - how to calculate normalized file name

I need to create a mapping between file names generated on Windows and OS X. I know that OS X "converts all file names to decomposed Unicode" however, "most volume formats do not follow the exact specification for these normal forms"
So, it does not seem a simple matter of converting the Windows name to NFD using a standard UTF8 API and being sure I have the correct OS X name. Is there a way to determine what the actual OS X file name will be without actually creating the file in the file system and then scanning the directory to see what was actually created?
I think the answer is this from TechNote 1150 HFS Plus Volume Format:
Note: The Mac OS Text Encoding Converter provides several constants
that let you convert to and from the canonical, decomposed form stored
on HFS Plus volumes. When using CreateTextEncoding to create a text
encoding, you should set the TextEncodingBase to
kTextEncodingUnicodeV2_0, set the TextEncodingVariant to
kUnicodeCanonicalDecompVariant, and set the TextEncodingFormat to
kUnicode16BitFormat. Using these values ensures that the Unicode will
be in the same form as on an HFS Plus volume, even as the Unicode
standard evolves.
You're probably looking for -[NSString fileSystemRepresentation] method.
Note that there is no general solution for this task. What is a valid file name depends on filesystem of the volume you're saving on. Not every file name valid for HFS+ is valid for FAT32, for example.
For Mac's “standard” filesystem (currently HFS+), fileSystemRepresentation should give what you need; for other file systems, there is no general way. Think about ones that don't exist but will be introduced in the future, for example :)
According to your link, filesystem drivers appear to (mostly) follow one of two behaviours:
* Return all names in NFD, and convert names as appropriate.
* Don't perform any conversions.
In both these cases, if you create a file on OSX in NFD, reading it back on OSX should give you the name in NFD.
OTOH, if your filename goes from Windows → NFS → Mac and you want to do some sort of sync, you're out of luck. This is not an easy thing to do, since the underlying problem is a little philosophical: Should filenames be byte strings or Unicode strings? I believe Unix traditionally does the former, and at least in Linux, UTF-8 NFC names are merely a convention.
(It gets worse, since IIRC HFS+ is defined to use Unicode 3.something, so a naïve conversion to NFD might be wrong for characters added/changed since then unless the API you use can guarantee a specific Unicode version.)

Looking for the mac os resource definition for the 'styl' resource

I have searched the net for this antiquated piece of Mac OS 9 technology and actually have a need of it for current Mac OS X development.
I can not find a .r definition in the current set of Mac OS X SDKs.
Effectively I need to be able to analyse and create a styl resource.
The 'styl' resource contains the same structure used by TextEdit (the ancient TextEdit, not the current TextEdit.app) when copying styled text to the clipboard. It is always used in tandem with a corresponding plain text buffer. See this tech note.
This corresponds to struct StScrpRec in TextEdit.h. You can find this file at /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/TextEdit.h. Note that there are not necessarily 1601 elements in the ScrpSTTable: it's a variable length array, whose actual length is given by the scrpNStyles member of StScrpRec.

Programmatically retrieve an OS X disk partition UUID

I have a path to a partition. How can I retrieve UUID of that partition programatically without using terminal commands? An example will be more helpful.
$ diskutil info / | grep UUID
Running this from C is left as an exercise for the reader.
If you want a partition other than the root, you can specify the mount point or device name (eg. disk0s2) in place of /.
You can use the Disk Arbitration framework (Apple reference). There is also a good summary at this blog by Chris Suter.
You can get the UUID by using the kDADiskDescriptionMediaUUIDKey. Aaron Burghardt described it well in this mailing list thread. Here is a quote from that link:
Once you have the DADisk, use DADiskCopyDescription to get a dictionary of properties, in which you will
find the UUID with the key kDADiskDescriptionMediaUUIDKey (see DADisk.h for other keys that may be of
interest). Note, a DADisk is a wrapper around an IOMedia object and the description dictionary
corresponds directly to the properties in the IOMedia object. Also, CFShow() is useful for printing the
description dictionary to the console.
I think the easiest is to use polkit
Download the DiskWatcher.h and .m from
http://polkit.googlecode.com/svn/trunk/FileSystem/DiskWatcher.h
http://polkit.googlecode.com/svn/trunk/FileSystem/DiskWatcher.m
Add it to your project (It has no ARC so add -fno-objc-arc flag if you use ARC)
Add DiskArbiratation framework
You can use
+ (NSString*) diskIdentifierForPath:(NSString*)path;
NSString *UUID1 = [DiskWatcher diskIdentifierForPath:#"/Volumes/Backup900GB"];

Testing if an application can open a specific type?

On Mac OS X, how can I tell if a given application opens a specific type?
I have two UTIs that I'm interested in: public.text and public.image and I have a list of applications. I want to filter the list by those two types so I have a list of text viewers and a list of image viewers.
I've seen a related question on SO where it's possible to get a list of all the viewers for a particular document type. I could do that and get the intersection of the set, but I'd rather be able to ask each application individually.
Are there any Cocoa or Carbon calls for that?
If you want a list of applications for a given document type, use LSCopyAllRoleHandlersForContentType.
If you want a list of document types for a specific application, open the application's Info.plist file and read the CFBundleDocumentTypes key.
Launch Services is your friend. But I don't think there's any API which gets the UTIs supported by a given app, so I would suggest you to use what you already described, unfortunately.

Resources