I Love AppleScript

Perhaps it’s all nostalgia, but I really love AppleScript.

What is AppleScript? Or should I say, what was AppleScript? A quick search finds multiple stories and threads and discussions about its demise. But it’s still there, as of today, May 16, 2020, in macOS Catalina (10.15.4). The venerable Script Editor application is still right there in Launchpad, there is a shiny Script Editor User Guide on Apple’s support site, and the AppleScript Language Guide is still online (although in a “documentation archive”).

script_editor

Ok, so what is AppleScript? My definition: a walk down memory lane. A callback to a simpler time: the late 1900s, when computers were still personal, and the term “software engineer” was not a thing. Ok, fine, wikipedia, the term started in 1965. But most people still called them “programmers” or “developers” — at least that’s what I remember.

I was working as a desktop publisher / graphic designer for a catalog retailer. I sort of fell into this mostly because I made all of the flyers for my band in college. This was well before things like streaming music, when the best form of advertising for a live show was to make a poster and staple it to telephone poles around town. Also, T-shirts.

delicious_tshirt

Because of the band, I got pretty good at Photoshop and CorelDRAW (which still exists!). I was terrible at Illustrator, but that was fine for a catalog, which was mostly photos and text. Later, Quark XPress (which also still exists!).

At the time, the concept of buying things on the web was pretty new, whereas shopping from a catalog printed on paper was a fairly established way to buy things.

crutchfield

The content production workflow was backwards from what you might imagine would happen today. We made the print catalog first, and then we copied all of the content to the web store. The creative process took several weeks, and was highly dependent on manufacturer’s new model releases, discounting, etc. Everything was timed around the new catalog hitting mailboxes. We had about 2 weeks while the catalog was off being printed and mailed to make the web store content.

Great photos are important to selling products[1]. So, we had our own photo studio where our photographer took high quality film photos. Digital cameras were still super expensive, and you could get higher quality from scanning the developed transparencies. We shipped these out to a digital shop that had high quality scanners. They would also silhouette and shadow the images since most of them were black boxes on white backgrounds.

High resolution CMYK images, optimized for print, came back from the scanning shop. I think we used Zip disks at the time. (Remember those?) We’d get a bunch of image files every day and they would get copied to our file server.

As a designer, my job was to:

  1. Place the hi-res CMYK photo into the Quark print layout for the catalog
  2. Use Photoshop to make 4 smaller RGB copies of the photo for the web

Step 2 was actually a series of smaller, tedious steps. Namely:

  1. Open the CMYK file in Photoshop
  2. Convert from CMYK to RGB
  3. Resize to Large
  4. Save as JPEG
  5. Undo twice
  6. Resize to Medium
  7. Save as JPEG
  8. Undo twice
  9. Resize to Small
  10. Save as JPEG
  11. Undo twice
  12. Resize to Thumbnail
  13. Save as JPEG
  14. Undo twice

You had to do this for each photo (every angle, every product), resulting in hundreds of clicks. It was carpel-tunnel inducing!

Enter AppleScript. A coworker made a little script to automate the repeated parts of the above process (resize, save as JPEG, undo twice).

I thought this was great, but people weren’t really using it; clearly, it needed more features for designers to bother with it. We needed to be able to process a bunch of files all at once. The script needed to do all 14 steps, for N files.

I wound up writing something like this (also on GitHub):

-- examples, replace with yours
set print_images to ¬
  {"/Users/jeffsternberg/Desktop/catalog/image_1.psd", ¬
    "/Users/jeffsternberg/Desktop/catalog/image_2.psd"}

-- main loop
repeat with print_image in print_images
  process_image(print_image)
end repeat

-- convert an image to RGB, then save 4 smaller web friendly JPEGs
on process_image(print_image)
  set image_name to remove_extension(print_image)
  tell application "Adobe Photoshop 2020"
    open file print_image
    tell current document
      change mode to RGB
      repeat with new_width in {600, 400, 200, 100}
        tell me to make_web_copy(image_name, new_width)
      end repeat
      close without saving
    end tell
  end tell
end process_image

-- resize, save as jpeg, revert
on make_web_copy(image_name, new_width)
  tell application "Adobe Photoshop 2020"
    tell current document
      set revert_state to the current history state
      resize image width new_width ¬
        resample method bicubic smoother
      save as JPEG in file (image_name & "_" & new_width) ¬
        appending lowercase extension with copying
      set the current history state to revert_state
    end tell
  end tell
end make_web_copy

-- from http://www.macosxautomation.com/applescript/sbrt/index.html
on remove_extension(this_name)
  if this_name contains "." then
    set this_name to ¬
      (the reverse of every character of this_name) as string
    set x to the offset of "." in this_name
    set this_name to (text (x + 1) thru -1 of this_name)
    set this_name to ¬
      (the reverse of every character of this_name) as string
  end if
  return this_name
end remove_extension

This may seem like nonsense to “real” programmers, but I loved this kind of code at the time, and still do.

It’s English-like. I can “tell application Photoshop” to do something, and it does it! When I want to interrupt telling Photoshop to do something, and tell my own script to do something that Photoshop doesn’t know how to do, I can “tell me to make_web_copy”. That’s just clever.

The process of writing an AppleScript is actually fun. You get immediate feedback: hit the play button and it runs. The Script Editor application hasn’t changed in 20 years, it seems, but is still really useful, with its nice syntax formatting and “Replies” panel. Web developers may recognize hints of the Console in Chrome Dev Tools here.

Plus, AppleScript has a built-in documentation concept called Dictionaries. Script Editor can open these dictionaries and display a nice little library browsing tool.

I hope this conveys some of why I love AppleScript. If you’ve made it this far, thanks for hanging with me. And tell me why you love (or hate) AppleScript on Twitter!


[1] Not sure anyone needs research on this, but here’s a paper that studies the role of images on ebay: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.648.6208&rep=rep1&type=pdf

One thought on “I Love AppleScript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s