1

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
0

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
  • @James Levine, My directions and Workflow along with the modified code work as advertised in macOS Sierra as I tested it under macOS Sierra. Whether I add a single graphics image file or multiple graphics image files and or a Folder full of graphics image files they are properly processed when the Workflow is run and there is no prompting by Finder as I'm using a Get Specified Finder Items action not a Get Selected Finder Items action. Which means you have to specify the target(s) before you run the Workflow not select them as part of the running Workflow. – user3439894 Oct 27 '16 at 14:45
  • @James Levine, You can also Drag & Drop the target(s) from Finder into the Get Specified Finder Items action, not necessarily have to use the Add button. Thus allowing adding multiple targets simultaneously. Then you run the Workflow and it does process the files for me under macOS Sierra just fine and without error. – user3439894 Oct 27 '16 at 14:47
  • I'm so sorry to add extra trouble for you. And I deleted my initial response. Yes, this works. I utterly overlooked the adjustments to the script and only factored in the changes to the workflow. I really appreciate this. A big cheers to you! – James Levine Oct 28 '16 at 16:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .