Leaving-out command-line methods (which is still a bit beyond me at the moment), any possibilities for improvements or any “gotchas” I should fix?
Premise of script: take high-res graphics from the design repository (PSD, JPEG, TIFF, EPS, and even AI) and convert them to fit within a predefined space (without padding). Script must handle any combination of files and folders dragged onto it drag-n-drop style.
Going to move this to FaceSpan for a better user input experience (less separate dialogs), but otherwise does everything I need it to that I can think of.
Anyone with PHP experience with graphics would be a plus”we still have a current issue where Photoshop-generated JPEGs sometimes confuse PHP5 (which serves the graphics). Seems PHP5 adheres strictly to the JPEG standards and doesn’t like the “extra” stuff Photoshop stores in the JPEG files (or so the web guy tells me). Annoyingly, sometimes we have to size them with this script, then make a second pass with GIMP to make sure the JPEG files are strict to standards.
--
-- DECLARE PROPERTIES
--
-- debugging on?
property g_debug : false
--basic file path and names
property g_home_folder_path : path to home folder
property g_log_file_name : "Resize Image for Web Log.txt"
--
-- UTILITY HANDLERS
--
--Log Entry Generation
-- With help from StephanK of MacScripter
-- http://bbs.applescript.net/viewtopic.php?pid=76607#p76607
--
on logMe(log_string, indent_level)
if g_debug is true then --allows turning the debugger on and off so my logMe's can be left in final version
set log_target to (g_home_folder_path & "Library:Logs:" & g_log_file_name) as text
try
set log_file_ref to open for access file log_target with write permission
repeat indent_level times
write tab to log_file_ref starting at eof
end repeat
write ((log_string as text) & return) to log_file_ref starting at eof
close access log_file_ref
return true
on error
try
close access file log_target
end try
return false
end try
end if
end logMe
--
-- MAIN SCRIPT
--
on open actionItems
-- remove folders from list
--
-- courtesy of Adam Bell of MacScripter
-- http://bbs.applescript.net/viewtopic.php?pid=77739#p77739
--
set dropped_items to {}
repeat with current_actionItem in actionItems
tell application "Finder"
if (contents of current_actionItem as text) ends with ":" then
try
set dropped_items to dropped_items & (files of (entire contents of current_actionItem) as alias list)
on error -- only one file inside (works around bug)
set dropped_items to dropped_items & (files of (entire contents of current_actionItem) as alias as list)
end try
else
set dropped_items to dropped_items & current_actionItem
end if
end tell
end repeat
--gather user-entered height, width, and JPEG quality
set h to display dialog "Height (pixels):" default answer ""
set new_height to text returned of h
set w to display dialog "Width (pixels):" default answer ""
set new_width to text returned of w
set j to display dialog "JPEG Quality:" buttons {"Medium", "High", "Maximum"} default button "Maximum"
set JPEG_quality to button returned of j
set n to display dialog "File Suffix:" default answer "_web"
set file_suffix to text returned of n
set save_location to choose folder with prompt "Choose a location to save converted files:"
--convert user answer to JPEG quality to Photoshop's numeric notation
if JPEG_quality = "Medium" then
set JPEG_quality to 5
else
if JPEG_quality = "High" then
set JPEG_quality to 8
else
if JPEG_quality = "Maximum" then
set JPEG_quality to 12
end if
end if
end if
--step through files
repeat with i from 1 to number of items of dropped_items
set parseMeString to (item i of dropped_items) as string
set parseMeStringPOSIX to POSIX path of parseMeString
--get file name without extension
tell (info for item i of dropped_items without size) to set {file_name, file_extension} to {name, name extension}
set file_name to text 1 thru -((count file_extension) + 2) of file_name
--rename file to indicate it was converted by script
set new_file_name to file_name & file_suffix & ".jpg"
try
tell application "Adobe Photoshop CS4"
activate
open file (parseMeStringPOSIX as text)
--assuming user is using pixels, convert document to same
set ruler units of settings to pixel units
--fix pixel aspect ratio
set pixel aspect ratio of current document to 1.0
tell current document
--get height and width of current document
set current_height to height
set current_width to width
--flatten image
flatten
--make sure is 72 dpi (PHP is very literal about this)
resize image resolution 72 resample method none
--change to RGB (CMYK not web compatible)
change mode to RGB
end tell
--remove extra channels
set safe_channels to {"Red", "Green", "Blue"}
set channel_names to name of channels of current document
repeat with c from 1 to number of items in channel_names
if (item c of channel_names) is not in safe_channels then
delete channel (item c of channel_names) of current document
end if
end repeat
--figure out reduction percentages
set height_ratio to new_height / current_height
set width_ratio to new_width / current_width
--make sure is not enlargement
if (height_ratio < 1) or (width_ratio < 1) then
--pick out smallest reduction and use that
if height_ratio ≤ width_ratio then
resize image current document height new_height resample method bicubic
else
if width_ratio < height_ratio then
resize image current document width new_width resample method bicubic
end if
end if
end if
(* Depricated in favor of letting user choose)
--create destination folder on Desktop
tell application "Finder"
try
make new folder at (path to desktop) with properties {name:"Images Converted for Web"}
end try
end tell
*)
--create path to destination
set new_file_location to ((save_location as Unicode text) & new_file_name)
--create reference (required for Photoshop saves)
set new_file_reference to (a reference to file new_file_location)
--save and close file
save current document appending lowercase extension as JPEG in new_file_reference with options {embed color profile:false, format options:optimized, quality:JPEG_quality}
close current document
--set completed file label to green
tell application "Finder" to set label index of (item i of dropped_items) to "6" as text
end tell
on error
activate
display alert file_name message "Unable to process this file, it will be skipped."
--set uncompleted file label to red
tell application "Finder" to set label index of (item i of dropped_items) to "2" as text
end try
end repeat
end open