ZuluDeltaNiner
ZuluDeltaNiner

Reputation: 735

How to sort images by width in Applescript?

I would like to sort my pictures by size, ad it is easiest to do so by width. I would like to get images with a width larger than 1919px and put it in another folder. I have googled and tried things for hours with no luck.

I've been getting this error: 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 specifier at item 1 in the repeat loop. any help on how to fix this?

My Code:

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

Upvotes: 0

Views: 663

Answers (3)

regulus6633
regulus6633

Reputation: 19032

A simple solution is to use the "get" command and parenthesis. In general "get" is understood so you normally don't have to use it in commands... but a quirk in applescript requires you to explicitly use it sometimes... especially when you have multiple commands in one line. Also the parenthesis ensure the multiple operations of the width line of code are performed in the right order

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

However there's a few other optimizations you could use so here's how I would write your script.

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

Upvotes: 2

Darrick Herwehe
Darrick Herwehe

Reputation: 3722

It works if you split up the dimension and width into two separate lines.

tell application "Image Events"
    set img to open imgPath
    set dim to dimensions of img
    set width to item 1 of dim
    [...]
end

Upvotes: 0

Lri
Lri

Reputation: 27613

You might just use mdls -rn kMDItemPixelWidth in the shell:

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

If mdls doesn't show the sizes of some images, try using ImageMagick or sips:

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

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

Upvotes: 0

Related Questions