Applescript is pretty cool


Applescript is pretty cool

A friend of mine was saying that a certain person he works with was like an Eliza program because he responds to email so fast. I thought I would see how easy that was in AppleScript…

First I needed an Eliza program. I picked a webservice because I heard that AppleScript can call webservices without a problem and that would let me learn two things at once. Google gave me one right away. So I loaded up the Script Editor and started hacking.

First I cut and pasted some AppleScript Mail code to get the perform_mail_action callback information that I needed. Then I looked at the examples for how to call SOAP services. AppleScript won’t parse WSDL out of the box so I had to do that myself to come up with:

on perform_mail_action(info)
tell application "Mail"
set NewMail to |SelectedMessages| of info
repeat with CurrentMessage in NewMail
set NewMessage to reply CurrentMessage
set MessageSubject to subject of CurrentMessage as string
set MessageContent to content of CurrentMessage as string
tell application "http://www34.brinkster.com/4xws/eliza.asmx"
set MySubject to call soap {method name:"CHAT", method namespace uri:"http://xws.webservice.net/xws", SOAPAction:"http://xws.webservice.net/xws/CHAT", parameters:{|Say|:MessageSubject}}
end tell
set subject of NewMessage to "Re: " & MySubject
tell application "http://www34.brinkster.com/4xws/eliza.asmx"
set MyContent to call soap {method name:"CHAT", method namespace uri:"http://xws.webservice.net/xws", SOAPAction:"http://xws.webservice.net/xws/CHAT", parameters:{|Say|:MessageContent}}
end tell
set content of NewMessage to MyContent
send NewMessage
end repeat
end tell end perform_mail_action

If you have never seen AppleScript before you’re probably pretty surprised at the syntax. It is sort of like English and it does understand quite an array of little words for doing things. Some of them you can leave out. The only really hairy thing I ran into was that “Say” is a reserved word or something so I couldn’t use it as a parameter at first until I figured out how to quote it with the |Say|. That’s really all there is to it. I’m not sure there is any language where it is easier without a lot of setup. This is with no imports, no adding libraries, no installation, really just a little text editor that can dynamically look things up that are already on the system.