Skip to content. | Skip to navigation

Personal tools
Log in
Sections
You are here: Home How To OS X Admin AppleScript Droplet with File Type Associations

AppleScript Droplet with File Type Associations

Here I describe how to create an AppleScript Droplet with full file type associations, custom file type icons, and a custom application icon.

 

Install XCode

XCode comes standard on the installation disks accompanying new Macs and is also available for free at the AppStore for OS X Lion. An install from the AppStore unfortunately takes a long time because of the download. But you only need to do it once per OS X major version.

Make the New AppleScript Application Droplet

  1. Open '/Applications/Utilities/AppleScript Editor'
  2. Enter the script. It needs to have an "on open filelist" block. Here is my script for my launchADXV droplet:
  3. set filecount to 0

    run application "XQuartz"

    on open filelist
    repeat with i in filelist
    set filecount to 1
    set filename to POSIX path of i
    set quotedname to quoted form of filename
    do shell script "/usr/local/bin/adxv " & quotedname & " &> /dev/null &"
    end repeat
    end open

    if filecount < 1 then
    do shell script "/usr/local/bin/adxv &> /dev/null &"
    end if
    Note that this example is simply an AppleScript wrapper around a shell script one-liner that calls a program with the dropped files. Additionally, all of the shell scripts run in the background, so the droplet does not wait for the shell scripts to terminate before it quits. Your droplet may have different behavior.
  4. Run the script from AppleScript Editor ("/Applications/Utilities/AppleScript Editor") to make sure that it works and debug.
  5. Save the script as an Applicaton (File → Save As...)
    1. "Save As:" should be the eventual name of your program, but with something like a "Not" in front of it. For example "Not-launchADXV". This naming will help later after the application is customized and you will rename it, which is the easiest way for the OS X launching utility to register the modified application.
    2. "File Format:" must be "Application".

 

Make the Application Icon

Icons, ugly or beautiful, heighten the usability of your droplet. They are easy to make using the tools that come standard with OS X (no extra money). Icons can be added to your app in 5 minutes. Good icons take about 7 minutes. Making icons requires XCode (available at the AppStore for free for OS X Lion). Make an icon for the app, and if desired, different icons for the documents. The following procedure is simplest, but if you know how to use a drawing program like Inkscape, Gimp, Illustrator, or Photoshop,  you can make more elaborate icons with transparency, etc.

  1. Find a picture that embodies the essence of your Application. The best part of the photograph should be roughly square. You will want to begin with an image where the good part is at least 512 x 512 pixels for best results. For my droplet, I just scanned the ADXV home page and found the large version of this image:



    Note how the good part is square.
  2. Download the image to your computer and open it with Preview ("/Applications/Preview").
  3. Hold down the [Shift] key to select the square area of the region of the interest that most embodies the essence of your droplet. While selecting, the numbers that appear next to the cursor indicate the size of the image in pixels. For best results the numbers should equal or exceed 512 x 512. If the [Shift] key is held down, the two numbers will be constrained to equal each other so that the selection is a perfect square.
  4. Once the desired area is selected, copy the selection (Edit → Copy in the Preview menu).
  5. Open Icon Composer ("/Developer/Applications/Utilities/IconComposer").
  6. Paste the copied image into all of the squares within Icon Composer (512, 256, 128, 32, & 16). Depending on your level of motivation, different resolutions can have different pictures. It's probably best to give them all a consistent appearance, but of course that's up to you.
  7. Save the icon. For the application icon, you must use the name "droplet.icns". For the document (file type) icons, you can use any name that ends with ".icns", which is the extension for OS X icons.

 

Make the Document Icons

OS X style document icons are only slightly more complicated to make than Application icons. They only require Preview and Icon Composer. Both of which come free with OS X as of Lion. I provide a Document Icon Template to make these icons much easier.

  1. Download the 512 x 512 pixel Document Icon Template and open it in Preview.
  2. Find an image where the good part (the part that embodies the essence of your document type) is at least 350 x 350 pixels.
  3. From the image in Step 2, copy an area at least 350 x 350 pixels and copy it (no need to constrain it to be square as above).
  4. If the area is much larger than 512 x 512 pixels, after copying the square region you should downsize the image to make it manageable when you paste it into the Document Icon Template.
    1. In Preview, go to File → New from Clipboard to create a new document with the selection.
    2. In Preview, go to Tools → Adjust Size
      1. Fit into: Custom pixels
      2. Width: 500 and Height: 500 pixels (from the pulldown)
      3. Make sure "Scale proportionally" is checked
      4. Make sure "Resample image" is checked
    3. In Preview, go to Edit → Select All
    4. Then copy the resized image from Edit → Copy
  5. In Preview, go to the window with the open Document Icon Template and paste the copied image from Step 3 (or Step 4).
  6. Holding [Shift] use the handles to resize the image. Holding down [Shift] prevents distortion of your image while resizing.
  7. Grab the middle of the pasted image and move it into place.
  8. In this window in Preview, add the extension name using the Annotation Tools.
    1. Go to Tools → Show Fonts
    2. Select a big font size like 64 point bold.
    3. Go to Tools → Annotate → Add Text, select an area and type in the extension into the region.
    4. Grab the edge of the text region and move it to the bottom of the document icon.
    5. If the text is not black, it's best to fix the color from Tools → Show Inspector → Annotations (the pencil icon) → Color
  9. Save the icon image as a PNG file with transparency (alpha)
    1. File → Save As...
    2. Name it something like "dropletname-extension.png" (make sure it ends with ".png")
    3. Format: PNG
    4. Make sure "Alpha" is checked.
  10. In this window in Preview, go to Edit → Select All
  11. Then Edit → Copy
  12. Then, follow Steps 5-7 from the Make the Application Icon section above.

 

Here is what the image for a document icon looks like from this process (full size):


Move the Icons into Place

  1. In Finder, navigate to the application created above in the Make the New AppleScript Application Droplet section above.
  2. Right-click on the script icon and select "Show Package Contents".
  3. In the new Finder window, navigate to "Contents/Resources".
  4. Replace the file "droplet.icns" with the application icon created in the Make the Application Icon section above.
  5. Copy the document type icons created in the Make Document Icons section into the Contents/Resources directory (the directory you just moved "droplet.icns" into).

 

Edit the Info.plist File

  1. As in the Move the Icons into Place section, navigate to the Contents directory of the application bundle and double click the "Info.plist" file. XCode should be installed, so this file will open with XCode.
  2. You will want custom values for these main Keys:
    1. Bundle identifier -- this usually takes the form of "com.example.bundlename", but it can just be "bundlename" or something unique.
    2. Bundle name
    3. Bundle creator OS Type code -- this is a unique four letter code. To make sure it is unique, you can alternate caps (ThNg) or throw in some numbers, etc. (Ir8@).
    4. Bundle versions string, short
    5. Bundle version
    6. Copyright (human-readable)

    Here is what the main keys look like for my launchADXV utility:



    Note the optional keys "Application UI Presentation Mode" and "Application is background only". I have these set to 3 ("All hidden") and YES (Boolean True), respectively, to completely suppress any appearance in the Dock, etc. Depending on how you want your script to behave, you may not set the values for these keys or set them to some other values.

  3. Expand the Document types entry in the "Info.plist" file. There should be an "Item 0" entry already created. It is essentially empty.
  4. Add as many items to the Document types entry as document types you plan to support.
  5. In each Item # entry (e.g. "Item 0"), expand the "CFBundleTypeExtensions" and "Document OS Types".
  6. Under "CFBundleTypeExtensions" add an item ("Item 0") with the extension for the file type (e.g. "osc").
  7. Change the value for the "Icon File Name" key to the name of the icon file for this file type (e.g. "ADXV-OSC.icns").
  8. In the "Document OS Types" entry, add an "Item 0" item with a value of the extension for the file type (e.g. "osc").
  9. For this "Document types" → "Item 0", add an entry keyed by "Document Type Name" and give it a value with good name (e.g. "ADXV OSC Document").
  10. Add a "Role" for this document type ("Viewer" or "Editor"). I prefer "Viewer" if my script will not change the file directly.
    Here is the part of the launchADXV "Info.plist" file with the .osc document type expanded:



  11. Save the "Info.plist" file.

 

Rename the Droplet Application

Because these last few steps have changed the droplet fundamentally, OS X considers this a new application altogether. However, because you changed the innards of the application, OS X does not know that the new application exists. To inform the OS X launcher of this "new" application, it is easiest to just duplicate the application and rename it. Without this step, file type associations will not work correctly, etc.

  1. Navigate to the application created in the Make the New AppleScript Application Droplet section.
  2. Right click on the application.
  3. Select "Duplicate".
  4. Rename the new copy. In this example, the new copy would be renamed from "Not-launchADXV copy" to "launchADXV".
  5. Move the old copy to the Trash.

 

Setup Document Type Associations

This is an end-user step, but I include it here for completeness.

  1. Navige to a document with the appropriate extension
  2. Right click the document
  3. Select "Get Info"
  4. Select the "Open with:" pull down
  5. Select "Other..."
  6. Navigate to your newly created and renovated droplet created in the Rename the Droplet Application section and click the "Add" button.
  7. Click the "Change All..." button in the Info window.

 

Here is what Finder thinks of my droplet and an associated file:

Note how the value for "Version" is 1.0 like specified in the "Info.plist" file and also that the "Kind" is properly reported for the OSC file.