AppleScript, mother tongue of Mac’rs

Before you were a Codewarrior addict, later Carbon libraries consumer, then Cocoa, then Obj-c, then iPhone developer, then what!

If you are a Mac user (not developer, being old or new to the business), AppleScript is your friend. In fact, any OSA-based language or “dialect” if I can call it so (such as appscript by Has or some handmade experiments by Phillip Aker). Apple Events. Application Intercommunication. Sounds like simple, but it’s the marmalade (not to talk about glue) of the daily workflow of hundreds of thousands of Mac users.

Come to mind other “experiments”, like “basic” (being Real or MS) or JavaScript for apps (such as Adobe’s apps or Air), but that is just System/App communication. It’s fine, but not in any way a checkpoint.

Although Qilania lives in a Ubuntu-driven server, many automation tasks are done in various “slave” machines, and many of them are driven by AppleScript, specially mirroring and db integration. For example, you can make tiny changes to a SWF file, and see them live in a few seconds. First, on the local server. Then, in the live server. And also changes are propagated to backup disks and so on. Magic under a double-click. Lots of technology involved: network connections, remote servers via ssh or sftp or scp, various desktop applications, etc. And one scripting language to rule them all: AppleScript.

I can ensure today, Sep 10th 2010, there is not programming language to acomplish most of these tasks we solve in a few seconds in Qilania, not in Mac nor in Win or *nix.

AppleScript, the marmalade and mother tongue of all Mac users.


AppleScript to convert HTML to PDF via Smile

This has been allways a tricky thing (convert a web page to PDF, preserving the more info possible, including text and clickable links), but since Smile 3.2 is a one-liner I thought I should share (specially when I didn’t find examples for the pageloaded event). This will save a “random” web page to your desktop, in a file called “file.pdf”. If you wanna see it in action, just remove “visible:false”:

script callback
	on pageloaded w
		save w in "~/Desktop/file.pdf"
		beep 2
		close w
	end pageloaded
end script

set webPage to ""

make new web window with properties ¬
	{path name:webPage, script:callback, visible:false}

It won’t preserve properly the formatting of certain web pages or embedded plug-ins contents, so if you need the screenshot for aesthetic purposes, Smile also provides a command called take screenshot, which will make a image of the rendered page (use it instead of “save w” as “take screenshot of w in …”). You may first resize the window to your favorite size, as the screenshot will only contain the visible area of the web window, but that is left as an exercice for the reader 😉

Very useful code for many tasks (web related), specially if Smile is your main script editor.

AppleScript to count pages of PDF documents via Skim

I found Skim to be the perfect replacement for Preview (ie, a fast app to review PDF documents with notes, etc.). And it also provides a decent AppleScript dictionary, so it’s also the perfect app to make one of those great time-savers. Follows sample code you can save as a droplet (and process a bunch of PDF files via drag & drop) or as a compiled script to be run from any script launcher:

open (get selection of application "Finder")

on open pdflst
	set cnt to 0
	repeat with i from 1 to count pdflst
		--> just in case comes from run handler...
		tell application "Finder" to set pdfFile to (pdflst's item i as alias)
		tell application "Skim"
			open pdfFile
			set cnt to cnt + (page count of (get info of document 1))
			close document 1 saving no
		end tell
	end repeat
	display dialog ("" & cnt & " pages for " & (count pdflst) & " documents...") with icon note
end open

I know it’s trivial code, but still you must spend some time to find a solution for your PDF-page-count needs, and this is a fast and free one.

Backup remote mysql databases to local MAMP via AppleScript/SSH

Requirements: access to the remote machine (usually a server) via SSH.

I backup my databases to the local installation of MAMP (stands for Mac-Apache-MySQL-PHP), but you can specify any directory you wish.

First step: do some stuff, so you can login automatically (no password prompt) to the remote machine. I followed the steps at and it worked flawlessly. In the first step exposed there, just type what the folk says, then “enter” all the time long (no need to enter data).

After you successfully copy the public key to the remote server, just copy-paste the following to your script editor, modify what you need to match your settings and hit “Run”:

--> this is the folder where the remote "mysql" dir will be copied
--> I choose MAMP's one, but you can choose any for backup purposes
set localDir to "User:Library:Application Support:appsolute:MAMP PRO:db:"

--> enter your server, the dir where your "mysql" dir is located (usually something as /var/lib)
--> and move it to whatever location is available via HTTP
--> if you don't have such service, you can use "scp" to copy the file to your machine
--> but then you must setup your machine to accept SSH connections
with timeout of 3600 seconds --> 1 hour
do shell script "ssh -l root 'cd /var/lib; tar -zcf mysql.tar.gz mysql;mv mysql.tar.gz /var/www'"
end timeout

--> download tgz file and remove pre-existing "mysql" folder in local machine (not sure yours, mine yes)
do shell script "cd " & quoted form of POSIX path of localDir & "; curl -O; rm -rf mysql;"

--> unpack
tell application "Finder" to open (localDir & "mysql.tar.gz")

--> remove tgz file from remote and local servers (I'll keep the uncompressed dir)
do shell script "ssh -l root 'rm /var/www/mysql.tar.gz'; rm " & quoted form of POSIX path of (localDir & "mysql.tar.gz")


EDIT: See this post for a different option (supports other db engines, such as InnoDB).

FLA to PNG via AppleScript plus JSFL

We use this as a droplet, so we can convert dropped FLA files to PNG in a shot. The original script is much larger, as PNG files are post-processed before using them (need some tweaks in GraphicConverter or a similar apple-scriptable app), but it is a skeleton you can use in many little automation projects involving the wonderful world of AppleScript and Flash Javascript (aka JSFL), the language used to automate and extend the Flash IDE capabilities.

AppleScript --> FLA to PNG

AppleScript --> FLA to PNG (click to enlarge)

The FLA file will be exported as PNG using the last settings you used to export a PNG file.

You can download it here.

FileMaker to MySQL via AppleScript

This is the AppleScript code we use to pass data from FileMaker to a remote MySQL database. It requires FileMaker, the MySQL binary installed in your machine and a connection to a remote MySQL-powered server.

The “code” tag is crap, so I’ll post an image of the code and a link to the code itself. You can download it here.

AppleScript --> FileMaker to MySQL

AppleScript --> FileMaker to MySQL (click to enlarge)

This way we feel comfortable working “at home” with a full-blown interface in FileMaker and we can populate changes to the server when needed.

Hello world!

Welcome to my personal blog about Qilania, the educational MMORPG for kids which will be born in 2010, where I act as product manager.

This will be a very interesting project, not only for its nature, but for the great amount of technologies used in it: SmartFox Pro Server and OpenSpace, PHP, MySQL, Flash and Flex, Java and AppleScript (in order to automate lots of tasks).

Stay tuned!