0

我想按大小对图片进行排序,按宽度最简单。我想获取宽度大于 1919px 的图像并将其放在另一个文件夹中。我已经用谷歌搜索并尝试了几个小时,但没有运气。

我一直收到这个错误:error "Image Events got an error: Can’t make item 1 of dimensions of image \"1mouwi.jpg\" into type specifier." number -1700 from item 1 of dimensions of image "1mouwi.jpg" to specifieritem 1重复循环中。有关如何解决此问题的任何帮助?

我的代码:

set picFolder to alias ":Users:USERNAME:Pictures:DESKTOPS:"
set hdFolder to alias ":Users:USERNAME:Pictures:DESKTOPS_HD:"


tell application "System Events"
    set photos1 to path of files of picFolder
    set num to count of photos1
    set photos to items 2 thru num of photos1
end tell

set hd to {}
repeat with imgPath in photos
    set imgAlias to alias imgPath
    tell application "Image Events"
        set img to open imgPath
        set width to item 1 of dimensions of img
        if width > 1919.0 then
            set end of hd to imgAlias
        end if
        close img
    end tell
end repeat

tell application "Finder"
    move hd to hdFolder
end tell
4

3 回答 3

2

一个简单的解决方案是使用“get”命令和括号。一般来说,“get”是可以理解的,因此您通常不必在命令中使用它……但是 applescript 中的一个怪癖要求您有时显式使用它……尤其是当您在一行中有多个命令时。括号也确保了代码宽度行的多个操作以正确的顺序执行

set width to item 1 of (get dimensions of img)

但是,您可以使用其他一些优化,所以这就是我编写脚本的方式。

set picFolder to (path to pictures folder as text) & "DESKTOPS:"
set hdFolder to (path to pictures folder as text) & "DESKTOPS_HD:"

tell application "System Events" to set photos1 to path of files of folder picFolder
set photos to items 2 thru end of photos1

set hd to {}
repeat with imgPath in photos
    set imgAlias to alias imgPath
    tell application "Image Events"
        set img to open imgAlias
        set width to item 1 of (get dimensions of img)
        close img
    end tell

    if width > 1919 then
        set end of hd to imgAlias
    end if
end repeat

tell application "Finder"
    if hd is not {} then move hd to folder hdFolder
end tell
于 2013-10-08T16:43:43.990 回答
0

您可能只mdls -rn kMDItemPixelWidth在外壳中使用:

for f in ~/Pictures/DESKTOPS/*; do [[ $(mdls -rn kMDItemPixelWidth "$f") -ge 1920 ]] && mv "$f" ~/Pictures/DESKTOPS_HD/; done

如果mdls不显示某些图像的大小,请尝试使用 ImageMagick 或sips

brew install imagemagick; identify -format %w input.png

sips --getProperty pixelWidth input.png | awk 'END{print $NF}'

于 2013-10-08T11:21:10.727 回答
0

如果您将尺寸和宽度分成两条单独的线,它会起作用。

tell application "Image Events"
    set img to open imgPath
    set dim to dimensions of img
    set width to item 1 of dim
    [...]
end
于 2013-10-08T14:03:45.917 回答