[ANN] ScriptToEmail preview
[ANN] ScriptToEmail preview
- Subject: [ANN] ScriptToEmail preview
- From: has <email@hidden>
- Date: Fri, 28 Sep 2001 16:22:11 +0100
Hey folks,
Here's a preview of a little something I knocked up on a slow afternoon.
Use it when posting your scripts to format them so they won't get chewed by
the AS list server. (Woo-woo!)
Once I'm happy with it, I'll post it to osaxen.com or something for easy
download (and amend the infostring property accordingly). Meantime,
comments are most welcome.
has
---------
NOTES
The script works on your clipboard's contents (run it before pasting your
script's code into an email message) and does the following:
1. converts 8-bit characters to keyboard shortcut (for commonly-used
characters) or to ASCII codes (for accented/oddball characters)
2. converts tab indents to spaces for appearance's sake
3. wraps long lines, preceding wrapped text with "[NO-BREAK]" for clarity
4. tops and tails the script with rules and useful info (tba).
I'm hoping my keyboard shortcut codes are safe for non-UK keyboards: I've
accounted for pound and euro signs by using ascii codes for those.
(Anything else I've missed?)
It's not smart enough to detect if a tab at a start of a line is an indent
or within a string. Thus it'll mess up if you have a string in your script
containing a return followed by tab(s). This can't be fixed short of
partially parsing the text to detect strings and handle accordingly, but I
can't say I feel much like doing this. List it under known gotchas to watch
for when converting scripts.
I think the wraplines() handler, which tries to break lines after a word
for neatness, could be improved by using 'words 1 thru -2' rather than
hunting for a space to break on using a repeat loop. I may fiddle with this
later.
It's not hugely fast (you'll have to be patient when converting larger
scripts). Any suggestions for improving speed are welcome.
The code's hopelessly uncommented, though it's not too hard to read if
you're curious.
Uses only standard additions for compatibility.
If anyone then feels like writing a decoder script, be my guest.:)
---------------------------------
Finally, here's the script itself, and may this be all but the last time I
have to say "watch out for the hard-wrapped lines":
property conversionlist : {"[ASCII-128]", "[ASCII-129]", "[ASCII-130]",
"[ASCII-131]", "[ASCII-132]", "[ASCII-133]", "[ASCII-134]", "[ASCII-135]",
"[ASCII-136]", "[ASCII-137]", "[ASCII-138]", "[ASCII-139]", "[ASCII-140]",
"[ASCII-141]", "[ASCII-142]", "[ASCII-143]", "[ASCII-144]", "[ASCII-145]",
"[ASCII-146]", "[ASCII-147]", "[ASCII-148]", "[ASCII-149]", "[ASCII-150]",
"[ASCII-151]", "[ASCII-152]", "[ASCII-153]", "[ASCII-154]", "[ASCII-155]",
"[ASCII-156]", "[ASCII-157]", "[ASCII-158]", "[ASCII-159]", "[opt-t]",
"[opt-shift-8]", "[opt-4]", "[ASCII-163]", "[opt-6]", "[opt-8]", "[opt-7]",
"[opt-s]", "[opt-r]", "[opt-g]", "[opt-2]", "[ASCII-171]", "[ASCII-172]",
"[opt-=]", "[opt-shift-']", "[opt-shift-o]", "[opt-5]", "[opt-shift-=]",
"[opt-,]", "[opt-.]", "[opt-y]", "[opt-m]", "[opt-d]", "[opt-w]",
"[opt-shift-p]", "[opt-p]", "[opt-b]", "[opt-9]", "[opt-0]", "[opt-z]",
"[opt-']", "[opt-o]", "[opt-shift-/]", "[opt-1]", "[opt-l]", "[opt-v]",
"[opt-f]", "[opt-x]", "[opt-j]", "[opt-\\]", "[opt-shift-\\]", "[opt-;]",
"[opt-SPACE]", "[ASCII-203]", "[ASCII-204]", "[ASCII-205]",
"[opt-shift-q]", "[opt-q]", "[opt--]", "[opt-shift--]", "[opt-[]",
"[opt-shift-[]", "[opt-]]", "[opt-shift-]]", "[opt-/]", "[opt-shift-v]",
"[ASCII-216]", "[ASCII-217]", "[opt-shift-1]", "[ASCII-219]",
"[opt-shift-3]", "[opt-shift-4]", "[opt-shift-5]", "[opt-shift-6]",
"[opt-shift-7]", "[opt-shift-9]", "[opt-shift-0]", "[opt-shift-w]",
"[opt-shift-e]", "[ASCII-229]", "[ASCII-230]", "[ASCII-231]",
"[ASCII-232]", "[ASCII-233]", "[ASCII-234]", "[ASCII-235]", "[ASCII-236]",
"[ASCII-237]", "[ASCII-238]", "[ASCII-239]", "[opt-shift-k]",
"[ASCII-241]", "[ASCII-242]", "[ASCII-243]", "[ASCII-244]",
"[opt-shift-b]", "[ASCII-246]", "[ASCII-247]", "[ASCII-248]",
"[ASCII-249]", "[ASCII-250]", "[ASCII-251]", "[ASCII-252]", "[ASCII-253]",
"[ASCII-254]", "[ASCII-255]"}
property indentspaces : "
"
property formatstring :
"======================================================================"
property infostring : "[formatted using ScriptToEmail -
http://www.?????]"
property oldTIDs : text item delimiters of AppleScript
set texttoconvert to the clipboard
if class of texttoconvert is not string or texttoconvert = "" then
display dialog "Clipboard doesn't contain text." buttons {"Oops"}
default button 1
else
display dialog "Format the clipboard as email-friendly AppleScript?
(This may take a few seconds.)"
set text item delimiters of AppleScript to ""
set texttoconvert to convertcharacters(texttoconvert)
set text item delimiters of AppleScript to return
replaceindents(texttoconvert)
set the clipboard to formatstring & return & return & wraplines(result)
& return & return & formatstring & return & infostring & return
set text item delimiters of AppleScript to oldTIDs
end if
-----------------------
on convertcharacters(texttoconvert)
set newtext to {}
repeat with eachitem in texttoconvert
if (ASCII number eachitem) > 127 then
set newtext to newtext & item ((ASCII number eachitem) - 127) of
conversionlist
else
set newtext to newtext & eachitem
end if
end repeat
return newtext as string
end convertcharacters
on replaceindents(texttoconvert)
set lineslist to every text item of texttoconvert
repeat with linecount from 1 to count of lineslist
set eachline to item linecount of lineslist
ignoring white space
if eachline = "" then set eachline to ""
end ignoring
if eachline begins with tab then
repeat with x from 1 to count of eachline
if item x of eachline is not tab then exit repeat
end repeat
set eachline to (text 1 thru ((x - 1) * 3) of indentspaces) &
(text x thru -1 of eachline)
end if
set item linecount of lineslist to eachline
end repeat
return lineslist
end replaceindents
on wraplines(texttoconvert)
repeat with linecount from 1 to count of texttoconvert
set eachline to item linecount of texttoconvert
if (count of eachline) > 70 then
ignoring white space
repeat with x from 70 to 40 by -1
if item x of eachline = "" then exit repeat
end repeat
end ignoring
if x = 40 then set x to 70
set firstline to text 1 thru x of eachline
set restoftext to "[NO-BREAK]" & (text (x + 1) thru -1 of eachline)
set item linecount of texttoconvert to (firstline & return &
wraplines({restoftext}))
end if
end repeat
return texttoconvert
end wraplines