Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically read Mac .textClipping files?

Tags:

cocoa

finder

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

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

Here some more details about the file:

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

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

like image 903
Mark Avatar asked May 20 '11 13:05

Mark


People also ask

How do I open a Textclip file on a Mac?

NOTE: TEXTCLIPPING files can be viewed by double clicking them in Mac OS X. When opened, their text can be manually selected, copied, and then pasted into another document.

What is .text clipping file on Mac?

textClipping is an extension used by Macintosh computers for strings of text since Mac OS 9. When a string of text is selected and dragged to the desktop or anywhere on a Macintosh computer, the computer automatically converts it into a . textClipping file.

What is Clipping in text?

Text Clipping is a process of clipping the string. In this process, we clip the whole character or only some part of it depending upon the requirement of the application. Text clipping Methods : All or None String Clipping method – In this method, if the whole string is inside the clip window then we consider it.


2 Answers

A textClipping file is an old fashioned resource fork file. You'll want to open it using FSOpenResourceFile, and then use Get1Resource to read the resources out of the file. The file might contain a few different resources types for the text: 'RTF ' (rich text), 'utxt' (UTF-8), 'utf8' (UTF-8), or 'TEXT' (ASCII) type resources, all with id 256. Once you read the resource, extract the data from the Handle and do with it what you want.

like image 83
Ken Aspeslagh Avatar answered Nov 15 '22 08:11

Ken Aspeslagh


It looks like in macOS 10.12 Sierra, a .textClipping file is now a property list.

The root dictionary has the key "UTI-Data". Within that, the keys: com.apple.traditional-mac-plain-text, public.utf16-plain-text, and public.utf8-plain-text hold a couple different representations of the data.

Here's an example that will read from a path:

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"];
}
like image 32
Dan Waylonis Avatar answered Nov 15 '22 09:11

Dan Waylonis