Zatanna Magic Spell Generator

Discussion in 'Mac Programming' started by VideoBeagle, Jul 15, 2014.

  1. VideoBeagle macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #1
    [​IMG]

    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? :D)
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    AppleScript knows about words, so if you don't care about punctuation, you can do something like:

    Code:
    set sentence to text returned of (display dialog "Enter your spell:" default answer "This is a magic spell")
    
    set reversed to ""
    repeat with aWord in (words of sentence)
    	set reversed to reversed & ((reverse of characters of aWord) as text) & space
    end repeat
    
    display dialog reversed buttons {"OK"} default button 1
     
  3. VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #3
    Yeah, v.1 didn't do punctuation:

    Code:
    display dialog "Enter your spell:" default answer ""
    set answer to text returned of result
    set zanswer to ""
    set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {" "}}
    set myList to text items of answer
    set AppleScript's text item delimiters to myTID
    repeat with fword in myList
    	#set testf to fword
    	#set fword to each
    	set zword to reverse of characters in fword as string
    	set zanswer to zanswer & " " & zword
    end repeat
    set zansewer to ((characters 2 thru -1 of zanswer) as string)
    set the clipboard to zanswer
    display dialog zanswer
     
  4. JustMartin macrumors 6502a

    Joined:
    Feb 28, 2012
    Location:
    UK
    #4
    From a purely stylistic view, I would consider adding a space to the end of your string before you start. This would save you having the check for the final word (and the repeated chunk of code)
     
  5. VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #5
    Ah.. Good idea.
    I had originally had that bit in as I found if there wasn't punctuation (or a space) at the end, the last word was never added..took me a while to figure out why I was losing a word...I made it a If-then for good practice....your solution is far more elegant.

    A friend (the same one who made the challenge) gave me a python script that pops up a picture of Zatanna saying the spell. I added the code so it works...

    Code:
    do shell script "python /Users/Scav/aa2/zee.py" & " " & thespell
     

    Attached Files:

Share This Page