colindunn
colindunn

Reputation: 3163

AppleScript: Organize images based on image dimensions

I'm brand new to AppleScript and I'm trying to write a basic script that does the following:

Finds images (PNGs) in the folder ~/Dropbox/Camera Uploads that are exactly 640x1136 (iPhone 5 screenshots) and moves them to ~/Dropbox/Camera Uploads/Screenshots.

This seems pretty straightforward, but so far I haven't been able to figure it out.

Upvotes: 0

Views: 1341

Answers (3)

Darrick Herwehe
Darrick Herwehe

Reputation: 3722

Here's how I would do it. I wouldn't worry about performance. I ran the Image Events section on 200 files, and it only took 1 second.

set picFolder to alias "Path:to:Dropbox:Camera Uploads:"
set screenshotFolder to alias "Path:to:Dropbox:Camera Uploads:screenshots:"


tell application "System Events"
    set photos to path of files of picFolder whose kind is "Portable Network Graphics image"
end tell

set screenshots to {}
repeat with imgPath in photos
    set imgAlias to alias imgPath
    tell application "Image Events"
        set img to open imgPath
        if dimensions of img = {640, 1136} then
            set end of screenshots to imgAlias
        end if
        close img
    end tell
end repeat

tell application "Finder"
    move screenshots to screenshotFolder
end tell

Upvotes: 3

adayzdone
adayzdone

Reputation: 11238

Try:

set folderPath to POSIX path of (path to home folder) & "Dropbox/Camera Uploads"
set screenshotsPath to POSIX path of (path to home folder) & "Dropbox/Camera Uploads/Screenshots"

try
    do shell script "mdfind -0 -onlyin " & quoted form of folderPath & " \"kMDItemPixelWidth == 640 && kMDItemPixelHeight == 1136\" | xargs -0 -I {} mv {} " & quoted form of screenshotsPath
end try

Upvotes: 0

Jerry Stratton
Jerry Stratton

Reputation: 3466

You need to have an AppleScript-aware application that can act based on the dimensions of an image file. I don’t think the Finder can do this, despite its ability to show the dimensions of images in Finder views.

iPhoto should be able to do this. The iPhoto dictionary indicates that “photos” have both the width and height of images. So you should be able to write an AppleScript that imports them into iPhoto first, then selects those that match your criteria, and then saves them to the appropriate Dropbox folder.

Depending on your needs, you might also look at Automator. It contains iPhoto actions as well, including one to “Filter iPhoto items”. If you create a Folder Action you should be able to create an Automator script that starts up whenever something new is added to your Camera Uploads folder, adds them to iPhoto, and then copies them to your Screenshots folder.

If nothing else, you should be able to use Image Events to get all images in the folder, and then act only on the ones that match your criteria. Something like:

tell application "Image Events"
    tell folder "Macintosh HD:Users:colin:Dropbox:Camera Uploads"
        copy (files where kind is "JPEG image") to potentialScreenshots
        repeat with potentialFile in potentialScreenshots
            set potentialScreenshot to open potentialFile
            set imageDimensions to dimensions of potentialScreenshot
            if item 1 of imageDimensions is 640 then
                set fileName to name of potentialFile
                tell me to display dialog fileName
            end if
        end repeat
    end tell
end tell

There ought to be a way to tell Image Events to only look at files whose dimensions match what you want, but I can’t see it.

Upvotes: 0

Related Questions