OK. That is somewhat unintuitive. The offset clauses are all grouped together in an area which doesn't need to communicate with Photos so a single tell me ... end tell construct is enough to avoid all of those errors. Better yet a use scripting additions inserted near the top of the script does the same thing, so now we have:
-- 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 dialogmessagewith titleinfobuttons {"OK"}
end if
end tell
And what is interesting is that the log looks the same whichever way you do it.
tell application "Photos"
activate
get selection
--> {media item id "aXv5SiYeRyyCI%azWIuDnA"}
get filename of media item id "aXv5SiYeRyyCI%azWIuDnA"
--> "IMG_0735.png"
get name of media item id "aXv5SiYeRyyCI%azWIuDnA"
--> "IMG_0735.cr2"
exists "IMG_0735.cr2"
--> true
end tell
tell current application
offset of "." in "2rc.5370_GMI"
--> 4
offset of "." in "gnp.5370_GMI"
--> 4
offset of ".cr2" in ".jpeg.jpg.mov.png.tif.tiff"
--> 0
offset of ".png" in ".cr2"
--> 0
end tell
tell application "Photos"
display dialog "No items were updated." with title "Photos | Filename to Title" buttons {"OK"}
--> {button returned:"OK"}
end tell
Result:
{button returned:"OK"}
I can see use scripting additions becoming a standard declaration in my future scripts. 😉
Thanks for introducing me to the delights of the replies log.
tt2