1

想了解有关 Mac 的图像事件的更多信息,我正在尝试学习如何将文件从一种类型保存到另一种类型。例如,如果我有一个名为的 BMP 图像,foobar.bmp我想学习如何将其保存为,foobar.jpg但在我的处理程序中出现错误:

图像事件出错:无法获取图像“foobar.bmp”的«class asDB»ID(应用程序“图像事件”)。

我的处理程序中的代码:

tell application "Finder"
    set directory to (choose folder with prompt "Please select a directory.")
    set someImages to (files of folder directory where name extension is "bmp") as alias list
    repeat with someImage in someImages
        tell application "Image Events"
            launch
            set workingImage to open someImage
            save workingImage as JPEG in directory
            close workingImage
        end tell
    end repeat
end tell

我确实测试了是否save可能需要 POSIX 路径而不是别名路径:

set directoryPosix to (POSIX path of directory)

并改变了save

save workingImage as JPEG in directoryPosix

但我仍然产生同样的错误,我不明白为什么。该代码有效,但只是引发错误,搜索后我无法找到解决方案。我已经知道如何使用 ImageMagick 使用 Bash 执行此操作,并且可以使用 AppleScript 和 SIP 执行此操作,但我想了解有关图像事件的更多信息。我做错了什么抛出错误?如果它有助于我的操作系统是最新的并运行 Yosemite 版本 10.10.5。

4

1 回答 1

0

您需要指定新文件的完整(HFS 或 POSIX)路径,而不是alias目标文件夹的说明符:

set directory to (choose folder with prompt "Please select a directory.") as text
tell application "Finder" to set someImages to (files of folder directory where name extension is "bmp") as alias list

tell application "Image Events"
    launch
    repeat with someImage in someImages
        set workingImage to open someImage
        set fileName to name of workingImage
        set newFilePath to directory & text 1 thru -4 of fileName & "jpg"
        save workingImage as JPEG in newFilePath
        close workingImage
    end repeat
end tell
于 2016-12-28T15:25:28.323 回答