applescriptautomatoriphoto

Applescript- How to prevent script from adding duplicate folders or albums to iPhoto?


I found this script online (see below) for importing photos automatically into iPhoto. It uses AppleScript and it works perfectly. The only issue is that when you stop the script and run it again, it will duplicate the folders (i.e. it will reimport the same pictures and create the same folders and albums, creating duplicates) (see the picture below).

PICTURE OF IPHOTO LIBRARY FOLDERS

Does anybody know how could this script be modified so that if the folder or album already exists, it will import the photos into that folder or album? I know you can select "no" to stop duplicate pictures from being imported, but I am looking to stop duplicate albums and folders from being imported.

I am not 100% familiar with AppleScript, so any help would be greatly appreciated.

Thank you!

Here is the script:

on run
    set folderList to (choose folder with multiple selections allowed)
    
    tell application "Photos"
        activate
        delay 2
    end tell
    
    repeat with baseFolder in folderList
        importEachSubFolder(baseFolder, null)
    end repeat
end run

on importEachSubFolder(aFolder, parentFolder)
    tell application "Finder"
        set albumName to (name of aFolder as text)
        set subFolders to every folder of aFolder
    end tell
    
    if (count of subFolders) > 0 then
        set fotoFolder to createFotoFolder(aFolder, albumName, parentFolder)
        
        repeat with eachFolder in subFolders
            importEachSubFolder(eachFolder, fotoFolder)
        end repeat
    else
        set fotoFolder to parentFolder
    end if
    
    importFotos(aFolder, albumName, fotoFolder)
end importEachSubFolder

on importFotos(aFolder, albumName, parentFolder)
    set imageList to getImageList(aFolder)
    if imageList is {} then return
    
    set fotoAlbum to createFotoAlbum(albumName, parentFolder)
    
    tell application "Photos"
        with timeout of 600 seconds
            import imageList into fotoAlbum skip check duplicates yes
        end timeout
    end tell
end importFotos

on createFotoFolder(aFolder, folderName, parentFolder)
    tell application "Photos"
        if parentFolder is null then
            make new folder named folderName
        else
            make new folder named folderName at parentFolder
        end if
    end tell
end createFotoFolder

on createFotoAlbum(albumName, parentFolder)
    tell application "Photos"
        if parentFolder is null then
            make new album named albumName
        else
            make new album named albumName at parentFolder
        end if
    end tell
end createFotoAlbum

on getImageList(aFolder)
    set extensionsList to {"jpg", "png", "tiff", "JPG", "jpeg", "gif", "JPEG", "PNG", "TIFF", "GIF", "MOV", "mov", "MP4", "mp4", "MPG", "mpg", "BMP", "bmp", "TIF", "tif", "AVI", "avi", "PSD", "psd", "ai", "AI", "orf", "ORF", "nef", "NEF", "crw", "CRW", "cr2", "CR2", "dng", "DNG", "PEF"}
    tell application "Finder" to set theFiles to every file of aFolder whose name extension is in extensionsList
    
    set imageList to {}
    repeat with i from 1 to number of items in theFiles
        set thisItem to item i of theFiles as alias
        set the end of imageList to thisItem
    end repeat
    
    imageList
end getImageList


Solution

  • Check for album/folder existing before creating:

    on importFotos(aFolder, albumName, parentFolder)
        set imageList to getImageList(aFolder)
        if imageList is {} then return
        set fotoAlbum to createFotoAlbum(albumName, parentFolder)
        tell application "Photos" to set albumPictureNames to filename of media items of fotoAlbum
        set filteredImageList to {}
        repeat with anAlias in imageList
            tell application "Finder" to set pictureName to name of anAlias
            if not (pictureName is in albumPictureNames) then set end of filteredImageList to contents of anAlias
        end repeat
        with timeout of 600 seconds
            tell application "Photos" to import filteredImageList into fotoAlbum skip check duplicates yes
        end timeout
    end importFotos
    
    on createFotoFolder(aFolder, folderName, parentFolder)
        tell application "Photos"
            if parentFolder is null then
                if not (exists folder folderName) then make new folder named folderName
                return folder folderName
            else
                if not (exists folder folderName of parentFolder) then make new folder named folderName at parentFolder
                return folder folderName of parentFolder
            end if
        end tell
    end createFotoFolder
    
    on createFotoAlbum(albumName, parentFolder)
        tell application "Photos"
            if parentFolder is null then
                if not (exists album albumName) then make new album named albumName
                return album albumName
            else
                if not (exists album albumName of parentFolder) then make new album named albumName at parentFolder
                return album albumName of parentFolder
            end if
        end tell
    end createFotoAlbum
    
    on getImageList(aFolder)
        set extensionsList to {"jpg", "png", "tiff", "JPG", "jpeg", "gif", "JPEG", "PNG", "TIFF", "GIF", "MOV", "mov", "MP4", "mp4", "MPG", "mpg", "BMP", "bmp", "TIF", "tif", "AVI", "avi", "PSD", "psd", "ai", "AI", "orf", "ORF", "nef", "NEF", "crw", "CRW", "cr2", "CR2", "dng", "DNG", "PEF"}
        tell application "Finder" to set imageList to (files of aFolder whose name extension is in extensionsList) as alias list
    end getImageList