How to programmatically read Mac.textClipping files?

These files are created whenever you drag the text selection into the Finder. The file size is always 0 bytes . Apparently, the data is stored in the resource fork.

I tried to read the fork [1] resource, but get the error code -39 (end of file).

Here are some more details about the file:

 $>xattr test.textClipping com.apple.FinderInfo com.apple.ResourceFork 

[1] http://www.cocoadev.com/index.pl?UsingResourceForks

+4
source share
3 answers

Take a look at FSOpenFork and FSReadFork. (Apple has an example AudioCDSample code).

There is also a command line tool that can read these files ( / usr / bin / DeRez ), you can watch it under GDB, but from what I saw it uses outdated APIs (for example, FSRead instead of FSReadFork).

+1
source

The textClipping file is an old-fashioned resource fork file. You need to open it with FSOpenResourceFile and then use Get1Resource to read resources from the file. A file can contain several different types of resources for text: resources RTF (rich text), 'utxt' (UTF-8), 'utf8' (UTF-8) or 'TEXT' (ASCII), all with identifier 256. After that, as you read the resource, extract the data from Handle and do with it what you want.

+4
source

It appears that on macOS 10.12 Sierra the .textClipping file is now a property list.

The root dictionary has the key "UTI-Data". In this case, the keys: com.apple.traditional-mac-plain-text, public.utf16-plain-text and public.utf8-plain-text contain several different representations of the data.

Here is an example that will read out of the way:

 NSString *path = @"/path/to/file.textClipping"; NSData *data = [NSData dataWithContentsOfFile:path]; id plist = [NSPropertyListSerialization propertyListWithData:data options:0 format:nil error:&error]; NSString *text; if (plist && error == nil) { NSDictionary *utiData = [plist objectForKey:@"UTI-Data"]; text = [utiData objectForKey:@"public.utf8-plain-text"]; } 
0
source

All Articles