Subject: | |
From: | |
Reply To: | |
Date: | Fri, 3 Oct 2008 15:27:21 -0400 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
On Oct 3, 2008, at 3:03 PM, RJay Hansen wrote:
> Okay. This one runs, and it runs extremely quickly. The file I'm
> running it on is 420K and the script finishes virtually
> instantaneously.
>
> However, opening the resulting file in Tex-Edit Plus gives very
> different results than opening it in TextEdit. Items are correctly
> italicized in TextEdit, but the paragraphs seem to be missing.
>
> In Tex-Edit Plus, all the paragraphs are there, but the supposed-to-
> be-italic words and phrases are now delimited by \i and \i0 rather
> than underscores and their style is still plain.
>
> So I tried doing a "Save As..." in TextEdit, saving as rtf, then
> opened that in Tex-Edit Plus and now the italic strings are all
> italicized, but the paragraphs are gone. Showing invisibles
> confirms, there are no paragraphs.
>
> RJay
RTF seems to only want one command per line. It also wants to have a
\ before returns that are actual returns.
Try this. I tested it against itself.
-------------------
try
set head to "{\\rtf1\\ansi\\ansicpg1252\\cocoartf949\\cocoasubrtf350
{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}
{\\colortbl;\\red255\\green255\\blue255;}
\\margl1440\\margr1440\\vieww12240\\viewh15840\\viewkind1
\\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\
\tx6480\\tx7200\\tx7920\\tx8640\\ql\\qnatural\\pardirnatural
\\f0\\fs24 \\cf0 "
set tail to "}"
tell application "Finder"
activate
set inputFile to choose file with prompt "Choose an underscore
delimited plaintext file for input."
set inputFileName to name of inputFile
set AppleScript's text item delimiters to "."
copy text item 1 of inputFileName to inputFileName
set AppleScript's text item delimiters to ""
set inputFilePath to ((container of inputFile) as alias) as text
end tell
set inputFileHandle to open for access inputFile
set outputFileHandle to open for access file (inputFilePath &
inputFileName & "_Italicized.rtf") with write permission
write head to outputFileHandle
set evenOddOpenCloseItalicsToggle to 0
repeat
try
set currentChunk to read inputFileHandle until "_"
if evenOddOpenCloseItalicsToggle is 0 then
if currentChunk is "_" then
write ((ASCII character 32) & "\\i " as text) to outputFileHandle
as text
else
write (endlines(text 1 thru -2 of currentChunk) & (ASCII
character 32) & "\\i " as text) to outputFileHandle
end if
set evenOddOpenCloseItalicsToggle to 1
else
if currentChunk is "_" then
write ((ASCII character 32) & "\\i0" as text) to outputFileHandle
else
write (endlines(text 1 thru -2 of currentChunk) & (ASCII
character 32) & "\\i0" as text) to outputFileHandle
end if
set evenOddOpenCloseItalicsToggle to 0
end if
on error e number n
if n is not -39 then display dialog "Error: " & (n as text) &
return & e
exit repeat
end try
end repeat
write tail to outputFileHandle
try
close access inputFileHandle
close access outputFileHandle
end try
on error e number n
display dialog "Error: " & (n as text) & return & e
end try
on endlines(sometext)
set tid to text item delimiters
set text item delimiters to ASCII character 10 -- in 10.5 you can use
linefeed instead
set sometext to text items of sometext
set text item delimiters to "\\" & (ASCII character 10)
set sometext to sometext as text
set text item delimiters to return
set sometext to text items of sometext
set text item delimiters to "\\" & return
set sometext to sometext as text
set text item delimiters to tid
return sometext
end endlines
|
|
|