Re: "quoted form of" anomaly when pathname contains '
Re: "quoted form of" anomaly when pathname contains '
- Subject: Re: "quoted form of" anomaly when pathname contains '
- From: Neil Faiman <email@hidden>
- Date: Sat, 2 Apr 2005 16:00:06 -0500
Two separate problems here.
First, if you were typing the command directly to the shell, it would be
tr \\r \\n < filename
or, alternatively,
tr '\r' '\n' < filename
The actual arguments to the tr command are supposed to be the strings
"\r" and "\n". However, when the shell processes the command line, it
will change "\r" to "r" and "\n" to "n", so that if you typed the
command line as
tr \r \n < filename
the tr command would translate all "r"s in the input file to "n"s. When
you specify the arguments as "\\r" and "\\n", the shell replaces the
"\\"s with "\"s, so tr gets the arguments "\r" and "\n", which is what
you want.
But you're trying to specify that command line as a quoted string
literal in AppleScript. Backslash is an escape character to
AppleScript, too; so when AppleScript sees the string literal "tr \\r
\\n < ", it replaces each occurrence of "\\" with "\". We've seen above
that the shell then changes "\r" and "\n" to "r" and "n". To specify
the desired shell command string "tr \\r \\n < ", you need to escape
each backslash, thus: "tr \\\\r \\\\n < ".
----------
Ok, that's the first problem. Now for a little digression. Since what
you are trying to do is simply to read a file, change the carriage
returns to newlines, and write the result to another file, you can do
all that with the shell command, thus:
do shell script "tr \\\\r \\\\n < " & (quoted form of POSIX path of f)
& " > " & (quoted form of POSIX path of target_file)
That's it. There's no need to read the output of the tr command back
into your AppleScript and write it back out again.
----------
But, assuming that you did want to read the output of the tr command
back into your AppleScript and write it back out again, where did all
the extra zero bytes come from? That's the second problem. The result
of the "do shell script" command is a Unicode string, and when you
write a Unicode string to a file with the AppleScript "write" command,
the default behavior is to write it as UTF-16. That means that each
character is written as two bytes; for ordinary ASCII characters, the
first byte is always 0 and the second byte is the same as the one-byte
representation of the ASCII character.
Quick fix -- use the "as" modifier to tell the "write" command to write
the string as plain ascii text rather than UTF-16:
write convertedText to the open_target_file starting at eof as string
But as I said above, your best bet is just to let the tr command write
the output file directly, and never bother to bring all that text into
your AppleScript at all.
Regards,
Neil Faiman
On Apr 2, 2005, at 2:49 PM, billmonk wrote:
Well, true to form, after some sleep things look different. \-.
"quoted form of" actually seems to be fine and I was mistaken...
However my deeper problem, which should have been stated first,
remains.
set t to "some text" & return -- would actually be a text file
containing carriage returns
-- now some code lifted from TN2065:
set convertedText to do shell script "tr \\r \\n < " & quoted form of
POSIX path of f without altering line endings
-- now save it to a file, error checking removed for this example
set the open_target_file to open for access file target_file with
write permission
set eof of the open_target_file to 0
write convertedText to the open_target_file starting at eof
close access the open_target_file
The file should now should now contain "some text\n".
Instead, not only does the carriage return remain, a 0x00 has been
inserted between each character of the text.
In BBEdit, with "show invisible" turned on, it appears as:
¿s¿o¿m¿e¿ ¿t¿e¿x¿t¿¬
and in a hexdump:
0073 006f 0065 006d 0020 0074 0065 0078 0074 000d
Now, when the quoted form of this text is passed to other command line
tools, they may do weird things (or crash) because the text still
contains carriage returns rather than the desired newlines.
(That, combined with the funny look of pathnames containing an odd
number of single quotes, is what caused me to jump to the late-night
conclusion that my pathnames were being munged and causing the
problem....)
But what is the problem?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-studio mailing list
(email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden