Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

42 Astoundingly Useful Scripts and Automations for the Macintosh

Work faster and more reliably. Add actions to the services menu and the menu bar, create drag-and-drop apps to make your Macintosh play music, roll dice, and talk. Create ASCII art from photos. There’s a script for that in 42 Astounding Scripts for the Macintosh.

appscript AppleScript translator

Jerry Stratton, May 22, 2007

I’m about to convert my Dining After Midnight site from FileMaker Pro to Django/SQLite. I decided to write a Python/appscript script to do the data transfer. While there aren’t that many restaurants in the list (San Diego is not a city that never sleeps), I have several other FileMaker databases I’d like to convert either to Django/SQLite or just SQLite, and this seemed like a good time to come up with an easy transfer process.

As I wrote in my earlier article about using appscript to control iTunes, the appscript help is really for Python programmers who already know appscript. If you’re comfortable with AppleScript, you will probably find ASTranslate (on the appscript download page) invaluable. It will take AppleScript “tell” blocks and convert them to appscript code.

For example:

[toggle code]

  • tell application "FileMaker Pro"
    • tell database "After Midnight"
      • show every record
    • end tell
  • end tell

Paste this into ASTranslate and it will become:

  • app(u'/Applications/Apps/FileMaker Pro 6 Folder/FileMaker Pro.app').databases['After Midnight'].records.show()

It’s still up to you to turn this into more readable Python, but that’s a much easier task than trying to guess at what appscript is expecting.

  • import appscript
  • fm = appscript.app(u'/Applications/Apps/FileMaker Pro 6 Folder/FileMaker Pro.app')
  • db = fm.databases['After Midnight']
  • records = db.records.show()

And finally, the better choice for opening FileMaker is probably to use the ID, in case you move it later:

  • fm = appscript.app(id="com.filemaker.filemakerpro")

If you’re using a more modern version of FileMaker Pro, you’ll also use a different ID.

With ASTranslate, as long as you can construct the “tell” block(s) in AppleScript, you can quickly get the necessary appscript calls.

In response to Using appscript in Python to control GUI applications: The appscript module for Python lets you control scriptable applications on the command line without having to coordinate your command-line script with your Applescript applications.

  1. <- Python Options