Help importing photos to specific album in Photos app

I cobbled together a script to import the files selected in Finder into the Photos app.


tell application "Finder"
	set imageList to (get selection) as list
end tell

set theList to {}

repeat with theImage in imageList
	set theList to theList & (theImage as alias)
end repeat

tell application "Photos"
	import theList
end tell

My AppleScript knowledge is limited, and what I’d like to do when importing the images is add them to a specific album (for the sake of argument, called “Test Album”).

It could import the images directly, or after importing the images grab the contents of the ‘Last Import’ album and add them to the named album.

Any help would be greatly appreciated :slight_smile:

Hi,

in Photos.app the albums are called containers


tell application "Finder" to set imageList to selection as alias list

tell application "Photos"
	import imageList into container "Test Album"
end tell

FWIW, this is a script I wrote to import files. It puts them in a new album you name, and names them after it, plus -1, -2, etc. It creates this album inside a folder. The idea is that the organisation is similar to what Photos does when it imports from iPhoto.

set enventsFolderName to "Pseudo Events" -- the name you want your "events" folder to have; change to suit

tell application id "com.apple.Photos" -- Photos
	-- choose the picture files to import
	set theFiles to choose file with prompt "Choose the pictures to import:" with multiple selections allowed
	-- get the name for the new event album
	set newName to text returned of (display dialog "Enter a name for the new event album:" default answer "")
	-- check the events folder exists, and if not, create it
	if exists container enventsFolderName then
		set eventsFolder to container enventsFolderName
		-- check the event album name is unique
		if exists container newName of container enventsFolderName then
			display dialog "That name is already in use" buttons {"Cancel"}
			error number -128
		end if
	else
		set eventsFolder to make new folder named enventsFolderName
	end if
	-- make the new event album inside the events folder
	set theAlbum to make new album named newName at eventsFolder
	-- sort the files by creation date
	tell application "Finder"
		set theFiles to (sort theFiles by creation date)
		repeat with i from 1 to count of theFiles
			set item i of theFiles to item i of theFiles as alias
		end repeat
	end tell
	-- import the files
	import theFiles into theAlbum
	-- rename them after the event album name, plus a digit
	set theItems to media items of theAlbum
	repeat with i from 1 to count of theItems
		-- this part is a bit icky; sometimes naming fails because there's a delay, so we keep trying until it succeeds
		repeat
			try
				set thisPic to item i of theItems
				set name of thisPic to newName & "-" & i
				exit repeat -- success
			end try
		end repeat
	end repeat
	-- all clear
	display dialog "Import finished" buttons {"OK"} default button "OK"
end tell

This is similar, except this time it works of images that have already been imported in Photos, and are selected:

set enventsFolderName to "Pseudo Events" -- the name you want your "events" folder to have; change to suit

tell application id "com.apple.Photos" -- Photos
	-- get list of selected images
	set theItems to selection
	-- make sure there are some selected
	set theCount to count of theItems
	if theCount = 0 then
		display dialog "You have to select the images to add to the event album first" buttons {"Cancel"}
		error number -128
	end if
	-- get the name for the new event album
	set newName to text returned of (display dialog "Enter a name for the new event album:" default answer "")
	-- check the events folder exists, and if not, create it
	if exists container enventsFolderName then
		set eventsFolder to container enventsFolderName
		-- check the event album name is unique
		if exists container newName of container enventsFolderName then
			display dialog "That name is already in use" buttons {"Cancel"}
			error number -128
		end if
	else
		set eventsFolder to make new folder named enventsFolderName
	end if
	-- make the new event album inside the events folder
	set theAlbum to make new album named newName at eventsFolder
	-- rename them after the event album name, plus a digit
	repeat with i from 1 to theCount
		-- this part is a bit icky; sometimes naming fails because there's a delay, so we keep trying until it succeeds
		repeat
			try
				set thisPic to item i of theItems
				set name of thisPic to newName & "-" & i
				exit repeat -- success
			end try
		end repeat
	end repeat
	-- now add them to the album
	add theItems to theAlbum
	-- all clear
	display dialog "Import finished" buttons {"OK"} default button "OK"
end tell

Great, thanks a lot!

Very useful, thanks :slight_smile:

Does anyone know how to import photos into the “Family” album automatically? Ideally, I want everything I mark as a favourite to go into the Family album. Ostensibly, it would seem simple enough…

tell application "Photos"
	
	set theItems to media items of container "Favorites"
	
	add theItems to album "Family" --skip check duplicates yes
	
end tell

BUT! The “Family” album doesn’t seem to be a regular album that Photos will accept as a valid container in applescript. I’ve tried referring to the “Family” album as a container, using “add” and “import”, but each time, “Family” is not accepted as a valid album.

Yes, Photos.app has the concept of a container. But the app also has folders and albums. It is better to refer to any element of a nested structure using these 2 concepts, specifying full paths. This is clearer and easier to eliminate user errors when specifying a path in a nested structure. Here an example that works and is clearer:


set imageList to choose file of type "public.image" with multiple selections allowed

tell application "Photos"
	-- import to album-child of Photos.app itself
	import imageList into album "Alexandra" with skip check duplicates
	-- import to album-child of some folder of Photos.app	
	import imageList into album "Untitled Album" of folder "Robert" with skip check duplicates
end tell