Getting subscripts from the content of a resource fork

I’m getting access to the content of textClipping files with the following Shell command, that I use from within Automator:

DeRez -script Roman -only utf8 MyFile.textClipping

I get this type of output:

data ‘utf8’ (256) {
$“4120 6578 7065 7269 C3AA 6E63 6961 2064” /* A experiência d /
$“6520 4D69 6C6C 6572 2065 2055 7265 792C” /
e Miller e Urey, /
$“2071 7565 2074 656D 2076 696E 646F 2061” /
que tem vindo a /
$“2073 6572 2072 6561 6C69 7A61 6461 2068” /
ser realizada h /
$“C3A1 206A C3A1 206D 6169 7320 6465 2035” /
á já mais de 5 /
$“3020 616E 6F73 2C20 7365 6D70 7265 2063” /
0 anos, sempre c /
$“6F6D 206D 6169 7320 7072 6563 6973 C3A3” /
om mais precisã /
$“6F20 6520 636F 6E64 69C3 A7C3 B565 7320” /
o e condições /
$“6D61 6973 2072 6566 696E 6164 6173 2C20” /
mais refinadas, /
$“6465 6D6F 6E73 7472 6120 7175 6520 6F20” /
demonstra que o /
$“7375 7267 696D 656E 746F 2064 6520 616D” /
surgimento de am /
$“696E 6FC3 A163 6964 6F73 2062 C3A1 7369” /
inoácidos bási /
$“636F 7320 286F 7320 626C 6F63 6F73 2062” /
cos (os blocos b /
$“C3A1 7369 636F 7320 6461 2076 6964 6129” /
ásicos da vida) */

I would like to get just the strings inbetween the /* and the */
I mean, just the:

A experiência d
e Miller e Urey,
que tem vindo a
ser realizada h
á já mais de 5
0 anos, sempre c

And concatenate all in a single string.
I tried it with:

DeRez -script Roman -only TEXT MyFile.textClipping | perl -ne ‘m|/* .* */| && print $1; END {print “\n”}’

But it is not working. :frowning:

Can someone help me?

This bit of code will process your output to the desired form:

-- cut the text before & after the desired bit
set text item delimiters to {"/* ", (" */" & return)}
set theClip to text items of theClip
set theClip2 to {}
-- fish out the desired bits from the list
repeat with i from 2 to count theClip by 2
	set end of theClip2 to contents of item i of theClip
end repeat
-- concatenate
set text item delimiters to ""
set theClip2 to theClip2 as text-- there's your result

We don’t know how you get your output, so I can’t tell you how to get it into this code.
And sometimes strange things will show up; I tested with a paragraph from the DeRez manpage, and got ‘¬’ at the line endings.

Thank you so much for the code.
My problem now is that the DeRez shell command is not returning the proper string to be interpreted.
Is there any way, in Applescript to access the content of a text resource inside a file?

With installed SatImage OSAX it’s very easy to get the plain text out of a clipping file

set theClipping to choose file of type "clpt"
set destinationFile to (path to desktop as text) & "clipping.txt"

set fileDescriptor to open for access file destinationFile with write permission
try
	-- The next two lines use the Satimage OSAX.
	set rsrcNum to beginning of (list resources "utxt" from theClipping)
	set txt to (load resource rsrcNum type "utxt" from theClipping)
	write txt & return as «class utf8» to fileDescriptor
	close access fileDescriptor
on error
	try
		close access file destinationFile
	end try
end try

Thank you so much. I will look into it.

THANK YOU!!!
It worked!! :smiley:
I had to change the code a bit but it worked perfectly.

You’re welcome

Since you’re using the Satimage.osax anyway, you might as well use it more.


set destinationFile to (path to desktop as text) & "myClippingText.txt"

tell application "Finder" to set finderSelectionList to selection as alias list

if length of finderSelectionList = 1 then
	set theClipping to item 1 of finderSelectionList
	
	-- The next two lines use the Satimage OSAX.
	set clippingText to (load resource 256 type "utxt" from theClipping)
	writetext clippingText & return to file destinationFile with append
	
end if


Chris


{ MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.11.2 }
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

I’m using this Applescript inside an Automator Service and Folder Action, to automatically convert textClipping files to TextEdit files.
Since textClipping files store the text inside resources, if I copy then to a FAT16, FAT32 or NTSF pen, only the data fork of the files is copied. So, I get zero Kb files.
Also, the resource fork seems not to “survive” when carried through some types of networks.
So, since I use textClipping files so much, I need to make them compatible to whatever storage or networking method I may end up using.
This is working just fine, now :slight_smile:

I’m using macOS Mojave and can’t seem to get this to work… can you please share how you did it? I"m basically trying to do the same exact thing you did… use the AppleScript inside Automator and Folder Action to automatically convert textClippings to Text Edit files.

You may try :

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set hfsFile to choose file "Choose a textclipping file" of type {"com.apple.finder.textclipping"}
set |⌘| to a reference to current application
set theData to |⌘|'s NSData's dataWithContentsOfURL:(hfsFile as «class furl») -- edited according to Shane's comment
set theDict to |⌘|'s NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value)
set theString to (theDict's valueForKeyPath:"UTI-Data.public.utf8-plain-text")

set hfsPath to POSIX path of (path to desktop) & "someText.txt"
(theString's writeToURL:(hfsPath as «class furl») atomically:true encoding:(|⌘|'s NSUTF8StringEncoding) |error|:(missing value))

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 20 février 2020 15:38:19

I’m a bit surprised if that works – dataWithContentsOfFile: should take a POSIX path, not a URL. I can’t help but think your avoidance of POSI paths is introducing a bit of unnecessary complexity.

Hello Shane

I executed the script before posting and it really worked.

After reading your message I searched for “dataWithContentsOf” and discovered “dataWithContentsOfURL” which I now use but it makes no practical difference to the script behavior.

StefanK already wrote that my avoidance of POSIX induce a bit of complexity but it’s a deliberate choice.
In fact, many times, “set theURL to hfsPath as «class furl»” is sufficient but in the posted script it wasn’t.

For those attached to them, here is the same script using POSIX paths.

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set theFile to choose file "Choose a textclipping file" of type {"com.apple.finder.textclipping"} as text
set |⌘| to a reference to current application
set theData to |⌘|'s NSData's dataWithContentsOfFile:(POSIX path of theFile)
set theDict to |⌘|'s NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value)
set theString to (theDict's valueForKeyPath:"UTI-Data.public.utf8-plain-text")

set hfsPath to (path to desktop as text) & "someText.txt"
(theString's writeToFile:(POSIX path of hfsPath) atomically:true encoding:(|⌘|'s NSUTF8StringEncoding) |error|:(missing value))

I’m just wondering if I am specifying the encoding uselessly but,
(theString’s writeToFile:(POSIX path of hfsPath) atomically:true) is described as “Deprecated”.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 21 février 2020 10:48:21

No, you’re definitely doing the right thing – without it, the encoding will vary with language “and potentially other settings”.

Nowadays Apple highly recommends (in Swift all path manipulation APIs have been removed from the String struct) to use always the URL related API to interact with the file system

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set filePath to POSIX path of (choose file "Choose a textclipping file" of type {"com.apple.finder.textclipping"})
set |⌘| to a reference to current application
set fileURL to |⌘|'s NSURL's fileURLWithPath:filePath
set theData to |⌘|'s NSData's dataWithContentsOfURL:fileURL
set theDict to |⌘|'s NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value)
set theString to (theDict's valueForKeyPath:"UTI-Data.public.utf8-plain-text")

set hfsPath to POSIX path of (path to desktop) & "someText.txt"
set destinationURL to |⌘|'s NSURL's fileURLWithPath:hfsPath
(theString's writeToURL:destinationURL atomically:true encoding:(|⌘|'s NSUTF8StringEncoding) |error|:(missing value))

In fact, I just discovered that “dataWithContentsOfURL” doesn’t require the use of:
set theURL to (|⌘|'s NSArray’s arrayWithObject:(theFile as «class furl»))'s firstObject() to defined the triggered URL.

set theData to |⌘|'s NSData’s dataWithContentsOfURL:(theFile as «class furl»)

behaves flawlessly.

So, I may work without using POSIX Paths and without using the ‘complicated’ instruction.
I edited my original message (#11) accordingly.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 21 février 2020 15:54:15

Yvan, where does your PosixPathophobia come from?

The entire file system is based on POSIX paths.

I know that Unix is based upon POSIX paths but I never liked them.
I used them when there was no alternative.
Now that I may work without them, I drop them exactly as I drop the Finder when I don’t manipulate Finder’s windows or Finder’s selection.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 21 février 2020 17:17:11