OS X Droplets Using Applescript

AppleScript is a scripting language built into OS X. With it’s roots in Apple’s old Hypercard application the language syntax was meant to simplify things for the layman programmer. In practice however I’ve always found it a bit verbose and a little arcane. That said AppleScript lets you automate tasks and make quick-and-dirty custom applications. For more about AppleScript see

https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptX/AppleScriptX.html

While AppleScript lets you interact with the Finder and other OS X applications this post will focus on putting a drag-and-drop frontend on an OS X command line program. The simplest way to illustrate this technique is with an example. The droplet below takes dropped png files (or folders) and uses PNGCrush (see TechNote #3) to create new, size optimized, files (named crush_filename.png).

-- This droplet processes both files or folders of files dropped onto the applet
on open these_items
  repeat with i from 1 to the count of these_items
    set this_item to (item i of these_items)
    set the item_info to info for this_item
    if folder of the item_info is true then
      process_folder(this_item)
    else if (alias of the item_info is false) then
      process_item(this_item)
    end if
  end repeat
end open

-- this sub-routine processes folders
on process_folder(this_folder)
  set these_items to list folder this_folder without invisibles
  repeat with i from 1 to the count of these_items
    set this_item to alias ((this_folder as text) & (item i of these_items))
    set the item_info to info for this_item
    if folder of the item_info is true then
      process_folder(this_item)
    else if (alias of the item_info is false) then
      process_item(this_item)
    end if
  end repeat
end process_folder

-- this sub-routine processes files
on process_item(this_item)
  set the item_info to info for this_item
  set ext to the name extension of the item_info
  set i to length of ext
  set nam to the name of the item_info
  set j to length of nam
  set nam to text 1 thru (j - i - 1) of nam
  set dir to the POSIX path of (this_item as text)
  set k to length of dir
  set dir to text 1 thru (k - j) of dir
  set outFile to quoted form of the POSIX path of (dir & "crush_" & nam & "." & ext)  
  set theFile to quoted form of the POSIX path of (this_item as text)
  do shell script "/-PATH_TO_FILE-/pngcrush " & theFile & "  " & outFile
end process_item

 

Let’s look at the code. The first two “on” blocks (from an Apple example) dispatch dropped files and folders and call the “on process_item” sub-routine block as needed. In our example the first task for this sub-routine is to parse the input file path into directory (dir), name (nam) and extension (ext). Having these variables can come in handy in building the parameters used in your command line (e.g., outFile). Next, the AppleScript command that actually executes the command line program is “do shell script”. In essence this command takes a string and processes it with the Unix shell “/bin/sh”.

For more about “do shell script” see

http://developer.apple.com/library/mac/#technotes/tn2065/_index.html

The above snippet of code is easily adaptable and I use it as a starting place for many of my droplets. Here is another example. With a new “on process_item” sub-routine this droplet uses perl to change a text file with <cr><lf> line breaks to <cr>.

 

on process_item(this_item)
  set theFile to quoted form of the POSIX path of (this_item as text)
  set AppleScript's text item delimiters to " "
  do shell script "perl -e \"s/\15/\15\12/g;\" -pi " & theFile
end process_item

 

TechNote #4

PNGCrush – Optimizing PNG Images

PNGCrush is an open source command line utility for the optimization of PNG (Portable Network Graphics) files. While the application has a number of useful features its main utility is to reduce PNG image file size. It can be run from a Terminal window on Windows, Linux and Unix (including Mac OS X). It is available as source code, as well as a few binary executables and via MacPorts (see TechNote #1). URL:

http://pmt.sourceforge.net/pngcrush/index.html

I’ve been using PNGCrush for a number of years and routinely get file size reductions of 30%-50%. If your web site or application uses many PNG image resources then PNGCrush is a good first step in optimizing performance.

TechNote #3

Got Equations – LaTeXiT

LaTeX is a document markup language for the TeX typesetting system. LaTeX is widely used in academia for the production of technical and scientific documents. To learn more about LaTeX visit – http://www.latex-project.org

latexit

LaTeXiT is a small Mac OS X utility that allows you to quickly typeset LaTeX equations. It works great as a simple tool for converting LaTeX markup to a bitmap or vector image. These images (e.g., PDF, PNG, JPG) can then be inserted into Keynote or PowerPoint presentations, web pages, Acrobat or Word documents. To download LaTeXiT (it’s free) go to

http://pierre.chachatelier.fr/latexit/

TechNote #2

MacPorts

MacPorts is a system for compiling, installing, and managing command-line—X11 or Aqua based—open-source software on Mac OS X. There are currently +17,000 ports available to install (e.g., PNGCrush, ImageMagick). The easiest way to install MacPorts itself is by downloading an installer from the MacPorts Project web site. URL:

http://www.macports.org

Note: To install and run MacPorts on your Mac you’ll need to have Apple’s Xcode, and command-line development tools installed. Also, for ports that use the X Windows system, you’ll need X11.

Sample MacPorts commands (Terminal command line):

  • Upgrade MacPorts itself and sync the local ports tree

      $ sudo port selfupdate
  • Install a port

      $ sudo port install APP_NAME
  • Upgrade installed ports and their dependencies

      $ sudo port upgrade
TechNote #1