Would it be possible to create a folder action script like this for Photos.app

Hi
I have this folder action to import photos into iphoto and it does exactly what i want.
If a file or a folder or more of them goes in, iphoto imports it as events and name the event like the file ore folder name.
In the Photos app events are gone so i search for a similar solution for albums.

Any help on this would be appreciated

property DELAY_TIME_SECONDS : 5
on adding folder items to thisFolder after receiving addedItems
	repeat with f in addedItems
		set oldSize to 0
		set newSize to -1
		repeat while newSize is not equal to oldSize
			set oldSize to size of (info for f)
			delay DELAY_TIME_SECONDS
			set newSize to size of (info for f)
		end repeat
-- HERE BEGINS THE IPhoto SPECIFIC STUFF
		tell application "iPhoto"
			try
				import from f without force copy 
			end try
		end tell
		-- HERE ENDS THE IPhoto SPECIFIC STUFF
		
	end repeat
end adding folder items to

You have to add them to an album. You can find some pointers on how to do it here:

macscripter.net/viewtopic.php?id=43831

Hi Andy,

As a starter, this will create a new folder called “My Events” and create an album (or event) from the name of a chosen folder:

set f to choose folder
tell application "System Events"
	set f_name to name of f
	set file_list to path of every item of f whose visible is true
end tell
tell application "Photos"
	activate
	set my_events to make new folder named "My Events"
	set new_event to make new album named f_name at my_events
	import file_list into new_event
end tell

I don’t know if you made the folder action, but the info is there in it. Note that if the folder already exists, then you don’t need to make it. Just get a reference to the event folder.

gl,
kel

Hi kel,

thank you for your reply with it finally i get it to work and you are right id didn’t wright the first part of the original script by myself but i know what it does, i am not really a script expert. With your reply in mind i dive now a little bit more into this stuff and learn a little bit so thank you again.

This is working well it creates an album and name it like the folder in Finder and imports the files in it into the album.
It works wit multiple folders to.
Exactly what i want.

property DELAY_TIME_SECONDS : 5
on adding folder items to thisFolder after receiving addedItems
	repeat with f in addedItems
		set oldSize to 0
		set newSize to -1
		repeat while newSize is not equal to oldSize
			set oldSize to size of (info for f)
			delay DELAY_TIME_SECONDS
			set newSize to size of (info for f)
			
			tell application "System Events"
				set f_name to name of f
				set file_list to (path of every item of f whose visible is true)
			end tell
		end repeat
		
		tell application "Photos"
			activate
			set new_event to make new album named f_name
			import file_list into new_event
		end tell
		
	end repeat
end adding folder items to

Hi Andy,

Nice work putting the folder action and the script together!

I didn’t have much time last time, but if you’re interested, folders contain invisible files sometimes. Mainly, there this invisible file .DS_Store. That’s why you have to eliminate the invisible file by filtering only visible files.

You could also use the line:

path of every file of f whose visible is true

Then, in case there a subfolders in the folder, the script will only look for files.

Good job,
kel

Hi,
yes you are right this is a better solution to avoid unwanted imports.
But I have another question there is a strange behavior with the Photos app.
The script works fine if the files are imported from the System drive but if the directory that has the folder action attached to it is on a different drive Photos app is not willing to import the files. This is strange it crates an blank album and when you drag the files from the finder into it the are importing fine.
I have no idea what’s going.
I think this is a bug in the Photos app ?

Thank you and have a nice day

Hi,
now i got it.:slight_smile:
The problem was sandboxing.
For now this is working.

property DELAY_TIME_SECONDS : 5

on adding folder items to thisFolder after receiving addedItems
	tell application id "com.apple.Photos" to run
	repeat with f in addedItems
		set oldSize to 0
		set newSize to -1
		repeat while newSize is not equal to oldSize
			set oldSize to size of (info for f)
			delay DELAY_TIME_SECONDS
			set newSize to size of (info for f)
			
			tell application id "com.apple.systemevents"
				set f_name to name of f
				set n to (POSIX path of every file of f whose visible is true)
				set theList to n
			end tell
		end repeat
		
		repeat with g in n
			set theList to theList & (POSIX file g)
		end repeat
		
		tell application id "com.apple.Photos"
			try
				set new_event to make new album named f_name
				import theList into new_event
			end try
		end tell	
	end repeat
end adding folder items to