
So, Zatanna, Mistress of Magic, came to me and said "Since you've been playing with reversing file names with Applescript, can you write one to help me cast my spells?"*
Well, not being one to say "no" to a witch, I figured I'd give it a try.
As we all know, Zatanna says her spells by speaking backwards...but only the words..so backwards words, forward sentences.
Code:
set alphabet to characters of "abcdefghijklmnopqestuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'-"
set theword to ""
set thespell to ""
(* Ask for spell, forward words *)
display dialog "Enter your spell:" default answer ""
set sentence to text returned of result
#set sentence to "This is a magic spell"
set slist to characters of sentence
#log slist
(*find words, revese them, add them to spell. Keep punctuation as is*)
repeat with theletter in slist
if theletter is in alphabet then
set theword to theword & theletter
else
set thespell to thespell & " " & (the reverse of characters in theword as string) & theletter
set theword to ""
end if
end repeat
(*add final word to spell if needed*)
if theword is not "" then
set thespell to thespell & " " & (the reverse of characters in theword as string)
end if
(* Remove extra spaces *)
#set thespell to ((characters 2 thru -1 of thespell) as string)
set AppleScript's text item delimiters to space
set textItems to every text item of thespell
repeat with x from 1 to count my textItems
if item x of my textItems = "" then set item x of my textItems to missing value
end repeat
set thespell to text of my textItems as string
set AppleScript's text item delimiters to ""
#return thespell
set the clipboard to thespell
display dialog thespell
(I found the script at the end to get rid of the extra spaces at macscripter.net)
Is there a better way to go about this? Someway to clean up the code?
(I know it doesn't deal with capitalization...googling looks like that's a headache for applescript).
I don't want to give someone who can turn me into a newt a bad program.
(* It was actually my friend, Andy, but isn't this story more fun?