0
votes

I'm using Automator for the first time, trying to manipulate (resize and pad) many images at once. I've created a workflow that consists of several actions and an AppleScript at the end to perform some basic If/Then logic (since my images are in different orientations, and need to be padded accordingly).

Here is my workflow (screencap):

enter image description here

And, my crappy AppleScript:

on run {input, parameters}

set this_image to input
set target_H to 600
try
    tell application "Image Events"
        -- start the Image Events application
        launch

        -- extract the properties record
        set the props_rec to the properties of this_image
        -- get dimensions of the image
        copy (dimensions of props_rec) to {W, H}
        -- determine the shortest side and then
        -- calculate the new length for the longer side
        if W is greater than H then
            set pad_dimensions to {W, 600}
            set the scale_dimension to target_H
        end if
        -- perform action
        pad this_image to dimensions pad_dimensions with pad color {255, 255, 255}
        -- save the changes
        save this_image with icon
        -- perform action
        scale this_image to size scale_dimension
        -- save the changes
        save this_image with icon
        -- purge the open image data
        close this_image
    end tell
on error error_message
    display dialog error_message
end try

return input
end run

I just want to continue making changes to the same image I started with in the automator workflow, but can't seem to keep going when I switch to AppleScript. Thanks in advance for your time/help!

1
What happens if you try to run your workflow? Does the script run? Does anything happen at all? Do you get an error message? - Mark

1 Answers

0
votes

I only looked at the first 2 lines of your code and spotted a common error. Notice your first action "Ask for Finder items". Notice "items", it's plural. Even though you select only 1 image file you will get back a list which can hold many image files. So your list will hold only one item but it's in a list. That list is passed to "Copy Finder Items" which is plural too so you're good. The stuff from the first action will be handled properly by the second action... and so on until it hits your applescript.

Now in your applescript a list of items is passed as "input". So your second line of code doesn't get an image, it gets a list. If you want the image from the list you have to get item 1 from the list...

set this_image to item 1 of input

Now you have the image and Image Events can do its thing. I didn't look at your Image Events code but I think you'll have to open this_image first and get properties and resize the opened image. So you probably have to do something like this...

tell application "Image Events"
   -- start the Image Events application
   launch

   -- open the image
   set openedImage to open this_image

   -- extract the properties record
   set the props_rec to the properties of openedImage

And so on. You'll be manipulating openedImage, not this_image, which means you'll need code to save openedImage when you're through.

Good luck.