-1

This is useful when you have a folder of images that may contain images that you already have in your library.

Varun Singh
  • 1,676
  • 3
  • 18
  • 25

1 Answers1

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

on importFotos(aFolder, albumName)
    set imageList to getImageList(aFolder)
    log (albumName)
    if imageList is {} then return
    
    set fotoAlbum to createFotoAlbum(albumName)
    
    repeat with image in imageList
        set fileInfo to info for image
        set filename to name of fileInfo
        set filenameStr to filename as string
        log (filename)
        
        tell application "Photos"
            activate
            set photosImage to search for filenameStr
            add photosImage to fotoAlbum
            log (photosImage)
            
        end tell
        
        
    end repeat
    log (albumName)
    
end importFotos

on getImageList(aFolder)
    set extensionsList to {"jpg", "png", "tiff", "JPG", "jpeg", "gif", "JPEG", "PNG", "TIFF", "GIF", "MOV", "mov", "MP4", "mp4", "M4V", "m4v", "MPG", "mpg", "BMP", "bmp", "TIF", "tif", "AVI", "avi", "PSD", "psd", "ai", "AI", "orf", "ORF", "nef", "NEF", "crw", "CRW", "cr2", "CR2", "dng", "DNG", "PEF", "HEIC"}
    with timeout of (30 * 60) seconds
        tell application "Finder" to set theFiles to every file of aFolder whose name extension is in extensionsList
    end timeout
    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

on createFotoAlbum(albumName)
    tell application "Photos"
        make new album named albumName
    end tell
end createFotoAlbum

This is also available from my GitHub repository: https://github.com/vjsingh/make-album-without-importing

Makyen
  • 31,849
  • 12
  • 86
  • 121
Varun Singh
  • 1,676
  • 3
  • 18
  • 25
  • You don’t need to have the extensions in uppercase and lowercase. AppleScript ignores case by default, so you’re just making Finder search through twice the number of items. You also don’t need to loop through `theFiles`, which is performing a really expensive set of operations and creating a new list object. You just need `tell app id "com.apple.Finder" to return (files in aFolder whose name extension is in extensionsList) as alias list` . In fact, you should **always** convert to `alias list` as it’s a lot quicker by bypassing the creation of Finder references. – CJK Dec 08 '20 at 08:38
  • Also, upon further examining what’s going on, your script only makes use of the file names of the files returned by Finder. So you should just retrieve the file names from the outset, and do away with the repeat loop (which shouldn’t be telling Photos.app to `activate` in every single iteration). You’ve built in inefficiency and maximised overhead all over the place, which isn’t wise. I’d recommend `tell app id "com.apple.finder" to return the name of every file in aFolder whose name extension is in the extensionsList` . – CJK Dec 08 '20 at 08:50