Skip to content

Instantly share code, notes, and snippets.

@gklka
Last active February 1, 2018 12:45
Show Gist options
  • Save gklka/48e9787603db5456223888cd9e4b0559 to your computer and use it in GitHub Desktop.
Save gklka/48e9787603db5456223888cd9e4b0559 to your computer and use it in GitHub Desktop.
Gets the selected files from Finder, and tries to add the „NYCSEL” keyword to the images with the same filename between the selected images in Photos.
log "Hello"
on upper(texttochange)
return texttochange
--return uppercase texttochange
end upper
set finderfiles to {}
set passes to 0
set misses to 0
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set missedfiles to {}
tell application "Finder"
set selectedfiles to selection
log "Finder files"
repeat with itemref in selectedfiles
set thename to name of itemref as text
set thename to my upper(thename)
if thename contains "." then
set {displayName, nameExt} to {text 1 thru text item -2, text item -1} of thename
set end of finderfiles to displayName
else
set end of finderfiles to thename
end if
end repeat
end tell
log (count of finderfiles) & " finder files selected"
tell application "Photos"
set selectedphotos to the selection
log "Photos files"
repeat with k from 1 to count of selectedphotos
set thephoto to item k of selectedphotos
set thefilename to thephoto's filename as text
--log (count finderfiles) & " " & thefilename
if thefilename contains "." then
set {displayName, nameExt} to {text 1 thru text item -2, text item -1} of thefilename
set thedisplayname to displayName
else
set {displayName, nameExt} to {thefilename, ""}
set thedisplayname to thefilename
end if
set missed to true
set newfinderfiles to finderfiles
repeat with i from 1 to count finderfiles
if thedisplayname contains item i of finderfiles then
log k & "/" & (count selectedphotos & " PASS: " & thedisplayname)
set passes to passes + 1
set missed to false
set thephoto's keywords to "NYCVAL"
--remove item from finderfiles
set newfinderfiles to {}
repeat with j from 1 to count finderfiles
if {finderfiles's item j} is not in {thedisplayname} then
set newfinderfiles's end to finderfiles's item j
end if
end repeat
end if
end repeat
set finderfiles to newfinderfiles
if missed is true then
--log k & "/" & (count selectedphotos & " MISS: " & thedisplayname)
set end of missedfiles to thedisplayname
set misses to misses + 1
end if
end repeat
end tell
set AppleScript's text item delimiters to prevTIDs
log "Passes: " & passes
log "Misses: " & misses
log "Missed files: " & finderfiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment