gui scripting – urgh

I like RapidWeaver and really like the idea of static pages for my website (it’s not as if I update it that often !) but I also have one or two posts in WordPress that I don’t want to loose. Currently, there is no AppleScript or Automator support in RW and the plugin dev kit is not yet out[1] (it’s promised for v3.5, but the current application release is v3.5 Public Beta 1) so I thought I’d try a little GUI Scripting.

Hooboy. It’s harder than AppleScript, but then I find AppleScript hard anyway as I’m a person who never managed to complete an Infocom text adventure. Yes, of course it’s plain English, but after trying the fifteenth variant of get (pickup, take, hold, grab, steal, nick…) and still getting “I don’t understand” I just went off to do something easier and more obvious. Like 6502 assembler and Forth.

AppleScript is like that: maybe if I spoke a more US variant of English it’d be easier, but GUI Scripting takes it to a new level as it’s necessary to switch from ‘plain’ English to a more computer-oriented variant as there is no easy way to describe UI elements without using the names that have been given to them. Take the following, for example:

tell application "Finder"
	activate
end tell

tell application "System Events"
	tell process "RapidWeaver"
		activate
		
	set projectName to "test.rw3"
		set postTitle to "This is my post title"
		set postCategory to "Photography"
		set postBody to "This is my post body (ie: the content)"
		set postDate to "1970/01/02 11:22:33 +0000"
		set postPermalink to "this-is-my-post-title"
		
	delay 4
		
	click button 1 of splitter group 1 of group 1 of splitter group 1 of splitter group 1
			of window projectName
		set tf to text field 2 of splitter group 1 of group 1 of splitter group 1 of
			splitter group 1 of window projectName
		set value of tf to postTitle
		
	set tf to text area 1 of scroll area 1 of tab group 1 of splitter group 1 of group 1
			of splitter group 1 of splitter group 1 of window projectName
		set value of tf to postBody
		
	click checkbox "Custom Permalink" of tab group 1 of splitter group 1 of group 1
			of splitter group 1 of splitter group 1 of window projectName
		set tf to text field 1 of tab group 1 of splitter group 1 of group 1 of
			splitter group 1 of splitter group 1 of window projectName
		set value of tf to postPermalink
		
	set pb to pop up button 1 of splitter group 1 of group 1 of splitter group 1
			of splitter group 1 of window projectName
		tell pb
			click
			keystroke down
		end tell
		
	set df to UI element 1 of splitter group 1 of group 1 of splitter group 1
			of splitter group 1 of window projectName
		set value of df to postDate
	end tell
end tell

That ought to be enough to create a new blog entry, fill in the title, custom permalink, body, category and date. What it does do is create a new item, set the custom permalink flag, occasionally fill in the custom permalink text itself, and throw the date into the search field. This is even with the help of the 30 day trial of UI Browser – in fact, without that trial I don’t think I’d even have got this far.

The result ? Well, I think I’m going to give up on this totally unless anyone else has some better tips for getting this sort of thing automated and wait for the dev kit and write a plugin to grab the posts directly from the WordPress database and leave it running as a non-public backend (or possibly translate the posts into a format that the normal blog plugin can use: it depends on how much sample code comes with the dev. kit).

[1] Ahem. The SDK is now out

2 Comments

  1. Dave June 6, 2006

    It would seem to me – and I’m no Apple scripting guru or anything – that you’ve got a race condition with the page load and the script’s delay instruction. Is it possible to ask the script to wait for the last control you need to manipulate to load before you start fettling with the other controls?

    I know, I know; more eggs Grandma?

  2. ian June 6, 2006

    Close, but not quite in this case: the app itself is what I’m waiting for just to ensure the system has activated it fully before continuing (and I have run without any delay command with the same results). The remaining commands are supposed to be the same as keypresses/mouseclicks in the application itself. The actual blog page is kind of a secondary thing, and I haven’t tried to build that here: this is just a form filling exercise.

    That fails.

    Still, now I have the SDK, and have absolutely no clue about Cocoa/Objective-C so that’s fun, as I’ve also decided that I should be using the XML-RPC interface to pull the WordPress posts out as it’s more flexible than a pure MySQL query when it comes to other blog types. The deeper joy is that the example code I have doesn’t build in this version of the Objective-C compiler.

    I knew there was a reason that interpreted languages were a good thing ! 😉

Comments are Disabled