Resize images to specific width only (AppleScript or Automator)

1.6k views Asked by At

I've been searching google.com a couple of hours trying to find a way to do this, but there is no answer. Everywhere is resize to the longest size, but nothing about resize to the specified width only.

Is there any way in Automator or AppleScript to resize images to a specified width only, instead of just the longest size? I need my output images to be a specific width ony (e.g 200px).

2

There are 2 answers

7
Craig Smith On BEST ANSWER

You can do this with plain AppleScript and a shell utility entitled sips:

    on open droppings
    repeat with everyDrop in droppings
        set originalFile to quoted form of POSIX path of (everyDrop as text)
        tell application "Finder"
            set originalName to everyDrop's name
            set imageContainer to (everyDrop's container as text)
        end tell

        set reSizedName to "200W" & originalName
        set outputPath to quoted form of POSIX path of (imageContainer & reSizedName)

        do shell script "sips --resampleWidth 200 " & originalFile & " --out " & outputPath
    end repeat
end open

on run
    display dialog "Drop some Image Files to Re-size them all to 200 pixels wide" buttons {"Aye Aye"} default button "Aye Aye"
end run

This preserves the aspect ratio of the original image, and simply re-sizes the width to 200 pixels. Hopefully you can see where you can make the changes necessary for your own workflow.

If you want to drop a folder of images as well as individual files, try this as a droplet:

on open droppings
    repeat with everyDrop in droppings
        if (info for everyDrop)'s folder is true then
            tell application "Finder" to set allImageFiles to everyDrop's every file
            repeat with eachFile in allImageFiles
                my SetWidthTo200(eachFile)
            end repeat
        else
            my SetWidthTo200(everyDrop)
        end if
    end repeat
end open

to SetWidthTo200(img)
    set originalFile to quoted form of POSIX path of (img as text)
    tell application "Finder"
        set originalName to img's name
        set imageContainer to (img's container as text)
    end tell
    set reSizedName to "200W" & originalName
    set outputPath to quoted form of POSIX path of (imageContainer & reSizedName)
    do shell script "sips --resampleWidth 200 " & originalFile & " --out " & outputPath
end SetWidthTo200


on run
    display dialog "Drop some Image Files to Re-size them all to 200 pixels wide" buttons {"Aye Aye"} default button "Aye Aye"
end run

There is still no error-checking or checking to be sure that the files are indeed image files, so keep that in mind.

1
ken On

Here's a summary from this OSX Tips using automator to resize images

  • open "Automator"
  • File > New > Service
  • Settings:
    • Service received selected: "image files"
    • in "Finder"
  • in the left pane, find for action named "Get Selected Finder Items"
  • Next look for action "Scale Images"
  • If you wish to replace original, choose "Don’t Add" when it ask "would you like to add a Copy Finder Items action", otherwise, choose add
  • In order to be able to choose the highest pixels before scaling, click on Options at the right of results, tick "show this action when the workflow run"