« Improving TextMate’s autocompleteMark Pilgrim: On crack »

[AS] Simple XMLRPC calls

Applescript’s really nifty trick is the ability to tie applications together. It also can tie internet applications together. No, seriously. Applescript can interact with internet applications with XMLRPC/SOAP protocols. There’s a few caveats but it’s actually quite simple. An example with notes are in the extended section. First things first, we’re going to make an interface to do simple interactions with a Blogger application. If you use Wordpress, you’ve got everything set up already as it has a Blogger interface that works fairly well. I assume Blogger works just as well but I’ve never tried it. The basics should be the same, though.

Now, we’re going to create some simple script objects to help us out. This makes it easier to do everything in a nice, object-oriented way. Notably, you have to have the url to the application in a tell block. You can’t make it easy and store it in a variable. I know, it sucks, but I’ve displayed it below. The path is “http://www.fcsexample.com/xmlrpc.php” so you can search and replace that as you need.

Also, the Blogger API allows for the title and the category to be set. Because of this, there’s another script object to make it easy to have everything formatted correctly. Here are the script objects:

script Blogger
    property username : ""
    property password : ""

    on newPost(post)
        tell application "http://www.fcsexample.com/xmlrpc.php"
            return call xmlrpc { method name:"blogger.getRecentPosts", parameters:{"", "none", username, password, toString() of post, true}}
        end tell
    end newPost

    on recentPosts(x)
        try
            x
        on error e
            set x to 10
        end try
        tell application "http://www. fcsexample.com/xmlrpc.php"
            return call xmlrpc { method name: "blogger.getRecentPosts", parameters: {"", "", username , password , x} }
        end tell
    end recentPosts

    on changePost(postNumber, newPost)
        tell application "http://www. fcsexample.com/xmlrpc.php"
            return call xmlrpc { method name: "blogger.editPost", parameters: {"", (postNumber as string), username, password, toString() of newPost, true} }
        end tell
    end changePost

    on toString()
        return username & ";" & password & " @ " & "http://www. fcsexample.com/xmlrpc.php"
    end toString
end script

script Post
    property title : ""
    property category : ""
    property body : ""

    on toString()
        return "<title>" & title & "</title>" & "<category>" & category & "</category>" & body
    end toString
end script

Good stuff? Follow it fairly well? Basically, the Blogger stuff works by using call xmlrpc inside a tell block. There’s a few special method names (which is generally fairly easy to find if the API is published), and the parameters. The Post script is very simple. Here’s an example of sending a post to Blogger.

copy Blogger to b
set username of b to "Grayson"
set password of b to "some_password"

copy Post to p
set title of p to "This is a title"
set category of p to "Applescript"
set body of p to "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

b's newPost(p)

Very nice, very simple, very integrate-able with other applications. You can use this script to turn TextWrangler into a blogging client. Or you could just use another, dedicated application. But still, it’s an interesting option.

There’s also a few extra methods I tossed in there to get the most recent posts and change a post. These are handy and might work. Give it a shot.

Basically, interacting with internet applications are as easy as working with native apps on your Mac. If they provide an API, you can pretty much piece your xmlrpc call together. There is a lot of great internet applications that can add value to Mac apps.

As an added incentive, if you’ve seen the load script [AS] entry, here’s a simple way to integrate the above script objects into other scripts without a lot of copy and paste. Simply save a file containing the Blogger and Post script objects into a file called “Blogger.scpt” on the Desktop (it can be saved anywhere, but the following example assumes it’s on the Desktop). Now, load the script as usual and pull the appropriate items from it.

tell application "System Events"
    set s to file "Blogger.scpt" of desktop folder
end tell

set x to load script s
set post to post of x
copy post to p
set title of p to "hello"
set category of p to "some category"
set body of p to "This is the body"
p's toString()

Leave a Reply