AppleScript Sierra – How to Repeat Actionscript

applescriptgraphicsphotos

Could you help me fix the following script?

I have an actionscript which I use within Automator to pad any size image thrown at it into a square canvas. In other words, you need not feed it the literal dimensions of the longest side and then tell it to match the other dimension with the same pixels. (For 3000 images that would take up as much time as exporting each one manually.)

Up until Sierra, this script would work for any number of images that I threw at it, whether I told Automator to act upon selected images, images within a folder, or if I specified the images from within Automator. Now when the script begins, it opens a Finder window requesting for me to select a single item. (And choosing multiples from within this window will not solve the issue, as the script is looking to process one item per iteration.) In other words, the script runs once then ends. I'd like to know how to get it to run for number of items need, and I'd like to know how to make the script work without requesting items from a Finder window, particularly if I already used Get from within Automator to choose the items in question.

So it is clear that the script is behaving differently now, not the workflow in Automator. And to further test this, the script could just as easily be run from the AppleScript Editor, but it does the same thing.

Here's the script in question:

set this_file to choose file without invisibles
-- indicate the proportions for the pad area
set H_proportion to 1
set V_proportion to 1
try
    tell application "Image Events"
        -- start the Image Events application
        launch
        -- open the image file
        set this_image to open this_file
        -- get dimensions of the image
        copy dimensions of this_image to {W, H}
        -- calculate pad dimensions
        if H_proportion is greater than V_proportion then
            set the new_W to (H * H_proportion) / V_proportion
            set pad_dimensions to {new_W, H}
        else
            set the new_H to (W * V_proportion) / H_proportion
            set pad_dimensions to {W, new_H}
        end if
        -- perform action
        pad this_image to dimensions pad_dimensions with pad color {65535, 65535, 65535}
        -- 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

Best Answer

If I understood your question, you do not want it prompting for the files to act upon and you want it to act upon all specified files, the following will do that.

  • In Automator, create a new Workflow. (Automator > File > New > Workflow)
  • Add a Get Specified Finder Items action.

    • Add the items you want to be in the Get Specified Finder Items action. The items can be files and or folders.
  • Add a Get Folder Contents action.

    • This is to handle if an item in the Get Specified Finder Items action is a Folder.
  • Add a Run AppleScript action.
    • Add the following AppleScript code to replace the default code.
on run {input, parameters}
    set list_of_files to input
    repeat with this_file in list_of_files
        -- indicate the proportions for the pad area
        set H_proportion to 1
        set V_proportion to 1
        try
            tell application "Image Events"
                -- start the Image Events application
                launch
                -- open the image file
                set this_image to open this_file
                -- get dimensions of the image
                copy dimensions of this_image to {W, H}
                -- calculate pad dimensions
                if H_proportion is greater than V_proportion then
                    set the new_W to (H * H_proportion) / V_proportion
                    set pad_dimensions to {new_W, H}
                else
                    set the new_H to (W * V_proportion) / H_proportion
                    set pad_dimensions to {W, new_H}
                end if
                -- perform action
                pad this_image to dimensions pad_dimensions with pad color {65535, 65535, 65535}
                -- 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
    end repeat
    return input
end run