The issue regarding the extra semicolons is that the text item delimiters are set to ‘;
’, so each time you have an as text
coercion, the text item delimiter would be inserted there. This apparently includes the following line which apparently functions with an implicit as text. Even though postpara might be empty, the delimiter will still be inserted.
set newdoc to newdoc & {prepara, postpara}
A solution would be to set the delimiters at the beginning and again at the end of the second loop Set them to ‘;
’ at the beginning, and depending upon whether the paragraph contains a semicolon or not: set them to either ‘""
’ if not, or leave them alone if so. There are probably other approaches to take but this one is simple enough.
if length of postpara is 0 then
set text item delimiters to ""
end if
So here would be the entire script. Hopefully it works as intended.
set newdoc to ""
set cr to (ASCII character 13)
tell application "TextEdit"
set txt to text of document 1
end tell
set allpara to paragraphs of txt
repeat with eachpara in allpara
set postpara to ""
repeat 1 times
set text item delimiters to ";"
if (count of eachpara) = 0 then exit repeat
set prepara to text item 1 of eachpara
repeat 1 times
if (count of prepara) = (count of eachpara) then exit repeat
set tempstr to text item 2 of eachpara
repeat with k in tempstr
set var to (ASCII number of k)
-- display dialog k & tab & var
if var > 64 and var < 91 then set var to var + 32
set postpara to postpara & (ASCII character var)
end repeat
end repeat
if length of postpara is 0 then
set text item delimiters to ""
end if
set newdoc to newdoc & {prepara, postpara}
end repeat
set newdoc to newdoc & cr as text
end repeat
-- set the clipboard to newdoc as text
newdoc
Unrelated notes:
What OS are you running? Apple introduced shell scripts to applescript in 10.1 with applescript version 1.8 (in 2001). And there is no ASObjC in the above script.
Note that if you specify an applescript version at the top of your script, eg use AppleScript version "1.8"
, then you must follow that with use scripting additions
or else the script will generate a syntax error (Expected end of line, etc. but found identifier.). FWIW, by including the use scripting additions line, I was able to run a shell script regardless of the specified version of applescript.
Regarding the ASCII commands, I’m sure that you are aware that they have been deprecated for some time and that using id
and character id
are recommended. That said, the range of letters from A-Z are contained between ASCII 65 and ASCII 90 so I modified the numbers accordingly in the script above.