Hello everyone,
I’m still pretty new to applescript and this is my first post to this forum.
I recently started using Adobe Lightroom for editing, but I still want to manage the edited photos in Apple’s Photos app.
Let me explain what I exactly want to do:
I use the publish to hard drive function in Lightroom to export my edited photos to a folder I called “Add to Photos”.
I want the ‘published’ photos to be added to a new or existing album in Photos and if I already imported a specific photo before, the old version should be deleted (I don’t think that’s possible with applescript, so I move photos with the same name to an album I named “To Delete”.
This is the code I came up with:
on adding folder items to this_folder after receiving these_items
tell application "Photos"
activate
delay 2
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items as alias
tell application "Finder"
set item_name to name of file (this_item)
end tell
set items_to_move to (every media item whose filename is item_name)
add items_to_move to container "To Delete"
end repeat
display dialog "Add to album with name" default answer "Imports"
set albumName to text returned of the result
if container albumName exists then
import these_items into container albumName skip check duplicates yes
else
import these_items into (make new album named albumName) skip check duplicates yes
end if
end tell
tell application "System Events"
delete (every file of this_folder whose name extension is "jpg")
end tell
end adding folder items to
The code worked pretty well until I exported more than 3 photos at a time. Because it takes some time for Lightroom to export the photos, the folder action gets activated to early. I tried to delay it by using a subroutine I found here “if folder ready”. However, now the script doesn’t finish and I think the folder action gets triggered twice.
This is the code where I’m currently at:
on adding folder items to this_folder after receiving these_items
set mypath to POSIX path of (path to home folder)
set importFolder to POSIX file (mypath & "Pictures/Add to Photos/") as alias
set extensionsList to {"jpg", "png", "tiff"}
tell application "Finder" to set theFiles to every file of importFolder whose name extension is in extensionsList
if folderReady(importFolder) then
tell application "Photos"
activate
delay 2
repeat with i from 1 to number of items in theFiles
set this_item to item i of theFiles as alias
tell application "Finder"
set item_name to name of file (this_item)
end tell
set items_to_move to (every media item whose filename is item_name)
add items_to_move to container "To Delete"
set the end of imageList to this_item
end repeat
display dialog "Add to album with name" default answer "Imports"
set albumName to text returned of the result
if container albumName exists then
import imageList into container albumName skip check duplicates yes
else
import imageList into (make new album named albumName) skip check duplicates yes
end if
end tell
tell application "System Events"
delete (every file of importFolder whose name extension is "jpg")
end tell
end if
end adding folder items to
on folderReady(tFolder)
set myFolder to tFolder as alias -- use the path to the folder that's loading
set firstSize to size of (info for myFolder) --get initial size
delay 5 --wait 5 seconds
set newSize to size of (info for myFolder) --get a newer size, bigger
repeat while newSize ≠firstSize --if they don't equal, loop until they do
set firstSize to newSize --new base size
delay 3 --wait three seconds
set newSize to size of (info for myFolder) --get a newer size
end repeat --once the sizes match, the transfer is complete
return true
end folderReady
I would be grateful for any help!
Cheers
Philipp