Batch convert .rtf files to .textclippings

Dear Macscripters,

I searched the internet and this forum, but couldn’t find the solution unfortunately.

I have a folder with hundreds of .rtf-files, each containing some formatted letters and numbers.
I want to batch convert each of these files to a folder with the same amount of textclippings keeping the formatted letters and numbers.
Is this possible with Applescript? Do you have a solution?

Thank you and all the best

Model: Macbook Pro 16
AppleScript: 2.8
Browser: Safari 605.1.15
Operating System: macOS 12

Thank you Fredrik71,

that goes far beyond my horizons and skills. I have no idea what the code should look like.

Nevertheless, thank you very much.

Hello Fredrik71,

thank you very much for the script. Much appreciated. Works like a charm.
Is there a way that the contents (Rich-Text) of the textclipping can be previewed in Spotlight with Quickview?

Thank you again.

Thanks for sharing very cool script, Fredrik71!!

I refined it little and made it useful handler for me:


use framework "Foundation"
use scripting additions

set theFiles to (choose file of type {"public.rtf"} with multiple selections allowed)
createTextClippings(theFiles)

on createTextClippings(theFiles) -- theFiles is aliases list parameter
	repeat with theFile in theFiles
		-- Get the source's Posix path, NSURL, basename
		set posixPath to POSIX path of theFile
		set containerPosixPath to (((current application's NSString's stringWithString:posixPath)'s stringByDeletingLastPathComponent()) as text) & "/"
		set theURL to (current application's |NSURL|'s fileURLWithPath:posixPath)
		set basename to theURL's lastPathComponent()'s stringByDeletingPathExtension()
		-- Create the dictionary for text clipping file
		set rtfString to (current application's NSString's stringWithContentsOfURL:theURL encoding:(current application's NSUTF8StringEncoding) |error|:(missing value))
		set theObjects to current application's NSMutableDictionary's dictionary()
		(theObjects's setObject:rtfString forKey:"public.rtf")
		set newDict to current application's NSMutableDictionary's dictionary()
		(newDict's setObject:theObjects forKey:"UTI-Data")
		-- Write .textclipping file at parent directory
		set theURL to (current application's |NSURL|'s fileURLWithPath:(containerPosixPath & basename & ".textClipping"))
		(newDict's writeToURL:theURL atomically:true)
	end repeat
end createTextClippings

That’s awesome!

Thank you Fredrik71