Delete an Image in iPhoto having the Filename (incl. path)

1.2k views Asked by At

How I can delete an image (move it into the trash) from applescript by giving the filename and path of the image locate on the drive.

This script will be started from the shell and may get the (valid) filename including the path (size or other properties if needed) as parameter.

So far I could manage to select the image by name (which is the filename usually) and set a comment on it. So I can search for that comment and delete all images with that comment at once. But with random image names this is not possible because you find unrelated pictures for the name.

3

There are 3 answers

1
Brian Webster On

Philip has the right idea, but is making things a little harder than they need to be. :) The following does the trick for me:

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

This grabs the first command line argument and uses it as the file path you want to delete. Since a photo in iPhoto can have both a modified and an original copy of each photo, the script tests for both the photo's original path and its current image path when looking for which photo to delete.

Do note though that the "original path" property only exists in iPhoto 8.x, so if you have an earlier version of iPhoto, you'll need to take that part out and be able to provide the correct photo path (the one in the Modified folder if there is one, otherwise the path in the Originals folder).

Also, the reason the "remove" command works whereas "delete" does not is because iPhoto sucks - "delete" really should work!

0
Philip Regan On

iPhoto stores a POSIX path as opposed to the Finder path (i.e., ":"-delimited). So, if you have the name and the path, then you can use the image path property of the photo class for comparison and delete the photo when there is a match. iPhoto has a list property for photos to make this easy. You'll have to clean the path you get from the shell before you do the comparison with the image path stored in iPhoto (remove the "\" escape characters, mainly).

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

Of course, when I tried this, delete gave me an error -10000, which means the handler failed. (Not much I can do about that one.)

0
Roberto Piccirilli On
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