AppleScript Endec 1.0.3
AppleScript Endec 1.0.3
- Subject: AppleScript Endec 1.0.3
- From: "Jonathan 'Wolf' Rentzsch" <email@hidden>
- Date: Mon, 19 Feb 2001 11:25:38 -0600
Greetings all,
You guys finally dragged me onto this list (grin).
The AppleScript Endec home page is up:
<
http://redshed.net/applescriptEndec>
The latest revision, 1.0.3, follows.
Shane Stanley pointed out to Bill that the clipboard access comes from
the Standard Additions, not the Finder. Thus we needn't wrap the
clipboard commands in Finder tell blocks.
John Baxter griped that script didn't encode more characters, so I went
overboard and now AppleScript Endec encodes all 8 bit characters (instead
of only those kept in a list). This makes the utility more
general-purpose at the expense of speed (128 replacement iterations
instead of 7).
Now that the script has a web page, I cut down the length of the script's
header comment.
I also fixed a bug in the decoding logic.
-------------------------------------------------
(* BEGIN AppleScript Endec (Encoder/Decoder) 1.0.3
WEB PAGE
<
http://redshed.net/applescriptEndec>
PURPOSE
This public domain script quickly encodes and decodes AppleScript
scripts for transport through Internet email systems.
To break the chicken-and-the-egg scenario, this script only contains
ASCII characters and no line exceeds 70 characters, making this
script safe to send via email. Copy and paste it into Script Editor.
USE
Copy an existing script onto the clipboard and run this script
and click the "Encode" button. The newly encoded script will replace
the original script on the clipboard. Now you can paste the encoded
script into an email message.
To decode an encoded script from an email, copy the encoded script
from the email, onto the clipboard. Run this script, and click the
"Decode" button. The newly decoded script will replace the encoded
script on the clipboard.
REQUIREMENTS
The script was written and tested on Mac OS 9. It uses the Standard
Additions's clipboard commands to read from and write to the
clipboard. I believe the clipboard commands were introduced with Mac
OS 8.5. If you're stuck on an old system, you can modify this script
to use a third-party nonstandard Scripting Addition to muck with the
clipboard.
This script doesn't use any non-standard Scripting Additions.
*)
property kEscapeChar : "`"
property kHexList : "0123456789ABCDEF"
-- PLEASE CHANGE kEncoderID IF REVISED:
property kEncoderID : "-- Encoded with AppleScript Endec 1.0.3"
-- The UI for encoding/decoding the script on the clipboard.
set clipboardContents to the clipboard
if class of clipboardContents is string then
set prompt to "There's a script on the clipboard. Do you want to:
Encode the script for transport
or
Decode the script to use it?"
set bList to {"Cancel", "Decode", "Encode"}
if clipboardContents contains kEncoderID then
set defaultButton to "Decode"
else
set defaultButton to "Encode"
end if
display dialog prompt buttons bList default button defaultButton
tell me
if the button returned of the result is "Encode" then
set output to encodeScript from clipboardContents
else
set clipboardContents to checkEncoder(clipboardContents)
set output to decodeScript from clipboardContents
end if
end tell
set the clipboard to the output
end if
-- Escapes common AppleScript-isms for email transport.
to encodeScript from unencodedScript
set output to kEncoderID & return & unencodedScript
-- ee is "escaped escape".
set ee to kEscapeChar & hex2(ASCII number kEscapeChar)
set output to replaceText from kEscapeChar to ee for output
-- ltoet is "less than or equal to"
set ltoet to ASCII character 178
set output to replaceText from ltoet to "<=" for output
set gtoet to ASCII character 179
set output to replaceText from gtoet to ">=" for output
repeat with asciiNumber from 128 to 255
set asciiChar to ASCII character asciiNumber
set escapeCode to kEscapeChar & hex2(asciiNumber)
set output to replaceText from asciiChar to escapeCode for output
end repeat
-- Convert beginning-of-line tab runs into double spaces
repeat with tabDepth from 8 to 1 by -1
set textToFind to return
set rText to return
repeat tabDepth times
set textToFind to textToFind & tab
set rText to rText & " "
end repeat
set output to replaceText from textToFind to rText for output
end repeat
return output
end encodeScript
-- Decodes the hexadecimal encoded script.
to decodeScript from encodedScript
try
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {kEscapeChar}
set encodedScriptList to every text item of encodedScript
repeat with i from (count of encodedScriptList) to 2 by -1
set encodedScriptPart to item i of encodedScriptList
set decodedNumber to unhex2(text 1 thru 2 of encodedScriptPart)
set decodedChar to ASCII character decodedNumber
if length of encodedScriptPart > 2 then
set restOfPart to text 3 thru -1 of encodedScriptPart
else
set restOfPart to ""
end if
set item i of encodedScriptList to decodedChar & restOfPart
end repeat
set AppleScript's text item delimiters to {""}
set unencodedScript to encodedScriptList as text
set AppleScript's text item delimiters to oldDelimiters
return unencodedScript
on error
error "Decoding failed.
Are you sure the clipboard contains an encoded script?"
end try
end decodeScript
-- Given an 8 bit number, returns its two-character hexadecimal
-- string equivalent.
on hex2(theNumber)
set output to ""
repeat 2 times
set characterIndex to (theNumber mod 16) + 1
set output to (character characterIndex of kHexList) & output
set theNumber to theNumber div 16
end repeat
return output
end hex2
-- Given a two-character hexadecimal string, returns its equivalent
-- number.
on unhex2(theString)
set firstCharacter to character 1 of theString
set firstNumber to (offset of firstCharacter in kHexList) - 1
set secondCharacter to character 2 of theString
set secondNumber to (offset of secondCharacter in kHexList) - 1
return (firstNumber * 16) + secondNumber
end unhex2
-- replaceText from "Y" to "A" for "xxYYzz"
-- --> "xxAAzz"
to replaceText from textToFind to replacementText for sourceText
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {textToFind}
set sourceTextList to every text item of sourceText
set AppleScript's text item delimiters to {replacementText}
set theResult to sourceTextList as text
set AppleScript's text item delimiters to oldDelimiters
return theResult
end replaceText
-- Looks for (and removes) encoder ID (mark).
-- If one can't be found, warns the user.
to checkEncoder(clipboardContents)
set encoderLine to kEncoderID & return
if clipboardContents contains encoderLine then
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {encoderLine}
set output to last text item of clipboardContents
set AppleScript's text item delimiters to oldDelimiters
return output
else
set prompt1 to "This script may have been encoded using another"
set prompt2 to " utility or another version of this utility."
set prompt3 to "Check the decoded output for correct syntax."
set prompt to prompt1 & prompt2 & return & return & prompt3
set bList to {"Cancel", "OK"}
set defaultButton to "OK"
beep
display dialog prompt buttons bList default button defaultButton
return clipboardContents
end if
end checkEncoder
(* END AppleScript Endec *)
.......................................................
Jonathan 'Wolf' Rentzsch jon at redshed dot net
Red Shed Software
http://redshed.net (847) 584-7465
PGP: B2AF 1A09 F881 EBDE C9D6 C4D2 C04F A3C0 3EC5 D5F2
"The reason for the rush is the revision, but the
reason for the revision is the rush."