Version 1.3 works as a Service! Thanks.
-- Photos | Filename to Title | V1.3
-- Images with no title will have the filename used as the title
-- Use values below to exclude or include specfic file extensions from the generated title
-- Existing titles will be modified to add or remove any extension from the title as required
set exclude to ".jpeg.jpg.mov.png.tif.tiff" -- Extensions to exclude from the image titles
set include to ".cr2" -- Extensions to include in the image titles
use scripting additions-- Prevents errors triggered by offset clause
tell application "Photos"
activate
set updated to 0
set info to "Photos | Filename to Title"
set images to (get selection)
if images is {} then
display dialog "Please select items in Photos before calling this script." with title info buttons {"OK"} giving up after 5
else
repeat with image in images
set fullName to filename of image
set title to the name of image
if not (exists (title)) or title = "" then
set title to fullName
set current to ""
else
set current to title
end if
set pos to offset of "." in ((reverse of characters of title) as string)
set prefix to characters 1 thru (-1 - pos) of title as string
if pos > 0 then
set postfix to characters -pos thru -1 of title as string
else
set postfix to ""
end if
set pos to offset of "." in ((reverse of characters of fullName) as string)
set ext to characters -pos thru -1 of fullName as string
if (offset of postfix in exclude) > 0 then
set newTitle to prefix
else
set newTitle to prefix & postfix
end if
if (offset of ext in include) > 0 and not ext = postfix then
set newTitle to newTitle & ext -- Or could test here and add something like " | RAW" for raw formats
end if
if not current = newTitle then
set the name of image to newTitle
set updated to updated + 1
end if
end repeat
if updated = 1 then
set message to " item was updated."
else
set message to " items were updated."
end if
if updated = 0 then
set message to "No" & message
else
set message to (updated as string) & message
end if
display dialog message with title info buttons {"OK"}
end if
end tell