applescript调整用户输入的图片大小

时间:2014-07-17 09:12:32

标签: image resize applescript

我正在尝试在当前的工作流程中实现一些自动化。我想将图像拖放到我的AppleScript应用程序中,然后将其自动调整为用户设置的大小。 我提示用户一个对话框,可以输入像素大小的宽度,然后让脚本完成驴工作。

由于某种原因,它运行不正常,而我的生活无法找出原因。 非常感谢任何和所有的帮助!

这是脚本:

display dialog "What is the desired size?" default answer "64"
set my_answer to text returned of result

on open some_items
    repeat with this_item in some_items
        try
            rescale_and_save(this_item)
        end try
    end repeat
end open


to rescale_and_save(this_item)



    tell application "Image Events"
        launch
        set the target_width to my_answer
        -- open the image file
        set this_image to open this_item

        set typ to this_image's file type

        copy dimensions of this_image to {current_width, current_height}
        if current_width is greater than current_height then
            scale this_image to size target_width
        else
            -- figure out new height
            -- y2 = (y1 * x2) / x1
            set the new_height to (current_height * target_width) / current_width
            scale this_image to size new_height
        end if

        tell application "Finder" to set new_item to ¬
            (container of this_item as string) & "scaled." & (name of this_item)
        save this_image in new_item as typ

    end tell
end rescale_and_save

1 个答案:

答案 0 :(得分:1)

我刚才写了这个剧本。它将重新采样图像,使其最大尺寸为输入值。

on open of myFiles
    set my_answer to text returned of (display dialog "What is the desired size?" default answer "64")

    repeat with aFile in myFiles
        set filePath to aFile's POSIX path
        set bPath to (do shell script "dirname " & quoted form of filePath)

        tell application "System Events" to set {fileName, fileExt} to {name, name extension} of aFile
        set baseName to text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName

        do shell script "sips " & quoted form of aFile's POSIX path & " -Z " & my_answer & " --out " & quoted form of (bPath & "/" & baseName & "-" & my_answer & "." & fileExt as text)

    end repeat
end open