0

如何通过在驱动器上提供图像的文件名和路径来从 applescript 中删除图像(将其移动到垃圾箱)。

此脚本将从 shell 启动,并可能获取(有效)文件名,包括路径(大小或其他属性,如果需要)作为参数。

到目前为止,我可以设法按名称(通常是文件名)选择图像并对其进行评论。所以我可以搜索该评论并立即删除所有带有该评论的图像。但是对于随机图像名称,这是不可能的,因为您会找到与名称无关的图片。

4

3 回答 3

2

菲利普的想法是正确的,但让事情变得比他们需要的更难。:) 以下对我有用:

on run (args)
    set thePath to first item of args
    tell application "iPhoto"
        remove (every photo where image path is equal to thePath or original path is equal to thePath)
    end tell
end run

这将获取第一个命令行参数并将其用作您要删除的文件路径。由于 iPhoto 中的照片可以同时拥有每张照片的修改副本和原始副本,因此脚本会在查找要删除的照片时测试照片的原始路径和当前图像路径。

请注意,“原始路径”属性仅存在于 iPhoto 8.x 中,因此如果您有早期版本的 iPhoto,则需要将该部分取出并能够提供正确的照片路径(位于Modified 文件夹(如果有),否则为 Originals 文件夹中的路径)。

此外,“删除”命令有效而“删除”无效的原因是因为 iPhoto 很烂——“删除”确实应该有效!

于 2010-08-13T20:51:53.913 回答
1

iPhoto 存储 POSIX 路径,而不是 Finder 路径(即,“:”-分隔)。因此,如果您有名称和路径,则可以使用类的image path属性photo进行比较,并在匹配时删除照片。iPhoto 有一个列表属性,photos可以让这很容易。在与 iPhoto 中存储的图像路径进行比较之前,您必须清理从 shell 获得的路径(\主要是删除“”转义字符)。

tell application "iPhoto"
    set pathToFile to "/Users/username/Pictures/iPhoto Library/Originals/2007/Sep 10, 2007/genericImageName.jpg"
    set lastPhoto to count photos
    repeat with thisPhoto from 1 to lastPhoto
        set thePhoto to photo thisPhoto
        set pathToPhoto to image path of thePhoto
        if pathToFile contains pathToPhoto then
            try
                delete thePhoto
                exit repeat
            on error the error_message number the error_number
                display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
            end try
        end if
    end repeat
end tell

当然,当我尝试这个时,delete给了我一个错误-10000,这意味着处理程序失败了。(我对此无能为力。)

于 2010-08-13T17:23:16.473 回答
0
tell application "iPhoto"
    set pathToFile to "/Users/username/Pictures/iPhoto Library/Originals/2007/Sep 10, 2007/genericImageName.jpg"
    set lastPhoto to count photos
    repeat with thisPhoto from 1 to lastPhoto
        set thePhoto to photo thisPhoto
        set pathToPhoto to image path of thePhoto
        if pathToFile contains pathToPhoto then
            try
                remove thePhoto
                exit repeat
            on error the error_message number the error_number
                display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
            end try
        end if
    end repeat
end tell
于 2014-12-25T20:24:41.083 回答