Add Studio Log to My Yahoo

Making Web Log Updater in AppleScript Studio

By: Jesse Shanks

In what I have to think is a first, this weblog is updated using an AppleScript Studio project. I have used bookmarklets with Javascript and AppleScripts to handle this type of updating before and had good facility. But, with the release of AppleScript Studio, I decided that making an application that does the work of keeping the site up would be handy to have running in my dock, especially for the times when I am not posting from a web page. Plus, it is always so much easier to learn when actually doing something that is needed than doing endless tutorials.

I have done some experimenting in the past with the "Dialog Director" scripting addition and "Dialog Studio" from macscripter.net. I have eagerly anticipated the release of AppleScript Studio to allow for more expanded scripting options. My first experiences with AppleScript Studio showed me something quickly that I later read on the Studio mailing list: Nicholas Riley said, "An issue with much of AppleScript Studio is that if you don't know what's going on with Cocoa, and want to do something complex, you can often get very confused, very quickly. I hope the documentation improves, so that AppleScript Studio can be accessible to a wider audience."

I have been working my way through Learning Cocoa from O'Reilly Books. I have also gotten much good instruction from the series by Mike Bream on O'Reilly Network. But, being severely handicapped by a lack of programming experience, I have not yet made the complete linkage between what I have learned in making AppleScripts over the years with what I am not learning in writing Cocoa applications. There is no doubt that making powerful applications is remarkably easy in Cocoa. In fact, it can really blow you away to put together a Simple Text Editor, as in one of Bream's tutorials, in an evening that really is almost as useful as the one that comes with the system!

I have to put in a complaint about the tutorial that Apple provided involving the Watson application. It is designed to interact with Mail and I just don't use Mail. It would seem that one of the other examples (or several) would have been better to provide a blow by blow account of making. But, there are many decent tutorials out there that provide crucial clues and there is also the excellent people who patiently answer question after question about complicated technologies on the mailing lists.

Beside the tutorials, I have also made several other attempts at Studio projects. I have one that is an RSS Reader that relies on the XML Tools AppleScript Scripting Addition from Late Night Software. It works quite well, although it definitely needs some work. It has the facility of downloading the newest feed to a folder into ~/Library/Preferences and the dynamically updates a button the article URL. The sites are hard-coded into a list, so one improvement will definitely be allowing for adding and deleting feeds. In the same vein, I made what I call my DVD Review Viewer. On another web site I work on, digitallyOBSESSED.com, I have been experimenting with converting the DVD Reviews that we do to XML. So, I adapted the RSS Reader to read the XML files that I create. It works quite well.

It seems that the easiest way for an AppleScripter to get going is to create the interface in Interface Builder and assign all the names in the AppleScript section of the Info Panel. Then select the appropriate handlers, which writes them into Project Builder. Then, in the script for the project, put existing Applescipt code into handlers. Then call those handlers from the handlers of the objects. Of course, this simplistic way will not always suffice, nor should it, but it is a way of looking at it.

The main function of the script is to grab info about a page in Internet Explorer, including selected text on the page to serve as a "blurb," and then send the data in a "POST" request to a PHP script that would them store the information in a MySQL table. In the bookmarklets, it was easy to use Javascript functions to store this information in variables and then build a URL string to send. Then in AppleScript, it is possible to use Internet Explorer's "do script" command in AppleScript to get access to the Javascript objects in the browser.

on getExplorer()
	tell application "Internet Explorer"
		set theT to do script "TheT=document.title;"
		set theU to do script "TheU=document.URL;"
		set theQ to do script "Q=document.getSelection();"
	end tell
	set contents of text field "title" of window "main" to theT
	set contents of text field "myurl" of window "main" to theU
	set the contents of text view "content" of scroll view ¬
"content" of window "main" to theQ end getExplorer
Of course, AppleScript can get the page title and page url without resorting to Javascript, but since I wanted to to go there to get the selected text, it seemed just fine. So, it just a matter of calling this handler from the on clicked handler designating the name of the button, in this case "update."

So, it was fairly easy to bring the same code that had worked before into AppleScript Studio behind the Cocoa interface.



This window allows for creation of three types of entries on the site. The "Update" button captures the title, url and selected text of a web page. Although, it isn't necessary, a sheet then comes down and allows for input of a "source." I will probably delete the sheet, but it does prompt to make sure a source is included. A category is selected in the table. Then the "Post" button send the assembled URL to Explorer, which then connects to the PHP script. Update: Upgraded the application to use "curl" in "do shell script" for uploading the posted data. Infinitely better.

If the "Diary Entry" button is checked, then the "source" and "url" are not necessary. This allows for a log entry that is, well, a "diary entry." To send a link to the "links" page of the site, the "Links" button can be clicked. This points to a different PHP script that sends the information into a different table, although it could be the same and use a parameter to choose the proper query to run. The "Clear" button responds to the escape key and clears all the fields.

I am still not completely happy with the interface, because I sometimes make mistakes in what I hit. So, more usage experiments will need to be done to see what causes problems. The main mistake I make is it hit the "post" button when I mean to submit a link.

Items to be upgraded:

Store the categories in a preferences file.
Add a window to allow uploading articles like this one to the web site.
File upload capability.
Storage of favorites in a drawer.
Editing of information that is stored in the database. Currently, I have to go to the browser and connect to the database to fix mistakes.

Another item to be upgraded is my own personal knowledge of Cocoa and Object-oriented programming. Hopefully, I have used my experience in AppleScript as a bridge into the Cocoa world and eventually my applications will become more Cocoa and less AppleScript.

January 8, 2002

Back

Applescript Handbook
Danny Goodman's Applescript Handbook