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

发布于 2024-12-01 16:05:46 字数 1262 浏览 0 评论 0原文

我有一个简单的 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

I have a simple Applescript to resize photos with Image Events. The photos are all of football players so they are all named by their number as in "1.jpg", "4.jpg" and so on. The issue I run into is when I do multiple batches of players in different directories the script will overwrite a photo with one from another team that has the same filename and was previously done. Again, these photos were all placed in different directories. The end result is after running successfully two or three times the reformatted photos of the players will get confused.

Here's what I have in the script to call Image Events.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2024-12-08 16:05:46

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

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

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

You seem to have triggered a bug in Image Events processing multiple items with the same name. I'm not seeing the exact behavior you describe, but I am seeing similar behavior.

I'd suggest you simply tell Image Events to quit after processing each folder; that way it won't get confused. (You don't need the launch, either; the only reason to use launch is if you want a non-background application to open without presenting an untitled document window.)

Incidentally, if you want to overwrite the existing image with the scaled version, all you need is save this_image. Image Events behaves much like any other application if you were to open a document, modify it, and save it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文