What’s this all about then?
rb-appscript is a powerful way to automate your Mac. It's similar to Applescript but it's built in Ruby, which means you get to leverage that language's powerful syntax and libraries. Giving you more time to make toasted sandwiches. You want long-winded explanations? We got those.
Please note that these scripts are by no means bulletproof, and are intended to provide examples of application scripting. I take no resposibility for any loss of data or mishaps; you download and run everything at your own risk. That said, I use all of these scripts in a production environment every day (well – more accurately, I use some of these scripts in a production environment most days).
Photoshop: Export Layer Comps
This script will save all your layer comps separately, in a format of your choosing. “But,” I hear you cry, “Photoshop already comes with a script that already does that already”. Yes, but this one uses the magnificent “Save for Web” command, which performs superior squishing and optimising.
The source code is rubbing its sleepy eyes and yawning, just over there.
#!/usr/bin/env ruby # rb-appscript script by Ian Haigh (http://ianhaigh.com/) # Mon Dec 14 10:32:32 EST 2009 # This is a quick & easy way to export all your layer comps to individual # files. One advantage of this technique is that it uses the "Save for Web…" # command, which (in my experience) generates smaller, higher quality web # graphics. require 'rubygems' require 'optparse' require 'appscript' require "osax" sa = OSAX.osax('StandardAdditions') ps = Appscript.app('Adobe Photoshop CS4.app') finder = Appscript.app("Finder.app") begin comps = ps.current_document.layer_comps.get rescue Exception => e sa.display_alert("Please open a document with at least two layer comps.", :as => :critical ) exit end # Save them into the same directory. # (This could be modified so the user can choose where to save the files) begin dir = File.dirname(ps.current_document.file_path.get.to_s) rescue Appscript::CommandError => e sa.display_alert("Please ensure the document is saved before running this script.") exit end format = sa.choose_from_list( %w{ jpg png psd }, :with_title => "Export layer comps", :with_prompt => "Choose a format", :default_items => ["jpg"], :multiple_selections_allowed => false, :empty_selection_allowed => false )[0] # loop through each comp, applying and saving it as we go comps.each do |c| ps.apply(c) file_name = File.basename(ps.current_document.name.get, ".*") + "_" + c.name.get case format when "jpg" ps.export(ps.current_document, :in => "#{dir}/#{file_name}.jpg", :as => :save_for_web, :with_options => {:web_format => :JPEG, :quality => 90}) when "png" ps.export(ps.current_document, :in => "#{dir}/#{file_name}.png", :as => :save_for_web, :with_options => {:web_format => :PNG, :png_eight => true}) when "psd" ps.save( ps.current_document, :in => "#{dir}/#{file_name}.psd", :as => :Photoshop_format, :appending => :lowercase_extension, :copying => true, :with_options => { :save_layers => false } ) end end finder.open(MacTypes::Alias.path(dir)) finder.activate