I'm a novice, trying to create a Mac OS Automator app or AppleScript that will accept images (PSD, tiff, png, jpeg) when dragged on the app's icon, convert to a different format, and resize them. I can do in the Terminal using the code below, but trying to figure out how to build in the cd command and make it work in Automator with the Run Shell Script action and a command something like below, so it doesn't need to go through Terminal. Ideally would ask for the pixel width desired.

for i in *.psb; do sips --setProperty format png --resampleHeightWidthMax 5000 "${i}" --out "${i%psb}png"; done

How do I implement it in an Automator workflow?

  • I think your issue with the shell script is your conscientious endeavour to iterate through the files individually, and apply the sips command to them. It would be easier and quicker to pass the list of files to sips in a single input, and specify the --out argument as a path to a folder rather than filenames. That way, all the input files all output to the one folder, and the extensions for the new files are set accordingly. It would look something like this: sips --setProperty format png --resampleHeightWidthMax 5000 *.psb --out ./ – CJK Aug 19 at 6:20

Save this following AppleScript code in Script Editor as an application. Double-clicking this new app in Finder will do nothing. Because there is no on run handler in the code, the app will only process single or multiple image files dragged directly onto its icon.

When a single or multiple image files are dragged directly onto its icon, you will be given the option to choose a new image format and new size in pixels (of the longest side of the image, which will then be used to scale the image proportionately).

Be sure to grant permissions in System Preferences for your new AppleScript droplet to be allowed to control your computer.

global thisImage, theImage, saveImage, fileExtension, saveToFolder, scaledPixels

use imageEvents : application "Image Events"
use scripting additions

property fileTypes : {BMP, JPEG, JPEG2, PICT, PNG, PSD, TIFF, QuickTime Image}

on open of droppedItems
    --  Executed when files are dropped on the script
    set fileExtension to item 1 of (choose from list fileTypes ¬
        with title "Make A Choice" with prompt ¬
        "Choose A New Format For Your Image" OK button name ¬
        "OK" cancel button name "Cancel") --as text


    repeat with thisFile in droppedItems
        tell application "System Events"
            set saveToFolder to POSIX path of (container of thisFile) as POSIX file
        end tell

        tell imageEvents to set thisImage to open thisFile

        scale thisImage to size scaledPixels
    end repeat
end open

to saveScaledImage()
        tell imageEvents to save thisImage in saveToFolder as fileExtension
    end try
    delay 0.5
        tell imageEvents to close thisImage
    end try
end saveScaledImage

to setPixels()
        set scaledPixels to text returned of (display dialog ¬
            "Scale The Longest Side Of Image To How Many Pixels?  Enter Numbers Only!" default answer ¬
            "3000" buttons {"Cancel", "OK"} ¬
            default button 2 cancel button 1 with title ¬
            "Scale Your Image") as number
    on error errMsg number errNum
        if errNum is not -128 then
        end if
    end try
end setPixels

enter image description here

  • This is fantastic – David Aug 24 at 9:31
  • That's beautiful. Thank you! Is it possible to trim to the image edge and add padding of a certain pixel amount with scripting additions also, or just ImageMagick? – Sweetpea Aug 26 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.