Macos 图像事件脚本正在用以前重新格式化的同名文件覆盖我的文件

Macos 图像事件脚本正在用以前重新格式化的同名文件覆盖我的文件,macos,applescript,image-events,Macos,Applescript,Image Events,我有一个简单的Applescript来调整照片的大小和图像事件。这些照片都是足球运动员的照片,所以他们都是以他们的号码命名的,如“1.jpg”、“4.jpg”等等。我遇到的问题是,当我在不同的目录下执行多批播放器时,脚本将用另一个具有相同文件名且以前执行过的团队的照片覆盖一张照片。同样,这些照片都放在不同的目录中。最终的结果是,在成功运行两到三次之后,重新格式化的玩家照片将变得混乱 下面是我在脚本中调用的图像事件 on open some_items repeat with this_i

我有一个简单的Applescript来调整照片的大小和图像事件。这些照片都是足球运动员的照片,所以他们都是以他们的号码命名的,如“1.jpg”、“4.jpg”等等。我遇到的问题是,当我在不同的目录下执行多批播放器时,脚本将用另一个具有相同文件名且以前执行过的团队的照片覆盖一张照片。同样,这些照片都放在不同的目录中。最终的结果是,在成功运行两到三次之后,重新格式化的玩家照片将变得混乱

下面是我在脚本中调用的图像事件

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 290
        -- 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) & "" & (name of this_item)
        save this_image in new_item as typ

    end tell
end rescale_and_save

您似乎在图像事件处理多个同名项目时触发了一个错误。我没有看到你描述的确切行为,但我看到了类似的行为

我建议您在处理每个文件夹后,简单地告诉Image Events退出;这样就不会混淆了。(您也不需要使用
launch
;使用
launch
的唯一原因是您希望打开非后台应用程序而不显示无标题的文档窗口。)

顺便说一句,如果要用缩放版本覆盖现有图像,只需
保存此图像
。如果要打开文档、修改文档并保存文档,则图像事件的行为与任何其他应用程序非常相似