-
All replies
-
Helpful answers
-
Apr 18, 2015 6:00 AM in response to R C-Rby inverseroom,Thanks for throwing in on my other thread...I have tried this script a dozen times, and I always get an error: "AppleEvent handler failed." This happens even after multiple reboots, and also while stepping through the script. Any ideas? Thank you!
EDIT: Wait a minute, I just found that when I select images in the Photos tab, the script won't work, but when I select them in albums > all photos, it does. We'll see if the names propogate into iCloud. Thank you!
-
Apr 18, 2015 6:54 AM in response to thedatadudeby inverseroom,OK, now that I seem to have this working well, I wonder if somebody might be able to help me out with a script that removes the file extensions from the titles?
-
May 3, 2015 7:29 AM in response to inverseroomby turingtest2,Hi,
I'd started with a script that included the extensions but then realised that I'd rather remove them. Possibly a little late in the day for you but this is my variation on the theme:
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 title to the name of image
if not (exists (title)) or title = "" then
set revTitle to (reverse of (characters of the filename of image)) as string
set pos to offset of "." in revTitle
set prefix to (reverse of (characters (pos + 1) thru -1 of revTitle)) as string
set the name of image to prefix
set updated to updated + 1
else
set revTitle to (reverse of (characters of title)) as string
set pos to offset of "." in revTitle
set prefix to (reverse of (characters (pos + 1) thru -1 of revTitle)) as string
set postfix to (reverse of characters 1 thru (pos - 1) of revTitle) as string
if (offset of postfix in "jpg|mov|png") > 0 then -- Extend list as required
set the name of image to prefix
set updated to updated + 1
end if
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
This will add the filename without the extension as an image title, or remove any extension currently included in the title. I haven't caught up with all of the discussions but it seems that Photos can sometimes bomb out when running the script on large numbers of items, and it may be more reliable when running the script with the contents of an Album rather than the global photos collection.
tt2
-
May 3, 2015 7:51 AM in response to turingtest2by R C-R,turingtest2 wrote:
I haven't caught up with all of the discussions but it seems that Photos can sometimes bomb out when running the script on large numbers of items, and it may be more reliable when running the script with the contents of an Album rather than the global photos collection.
That looks like a very useful, well thought out script!
I haven't tried it yet but as I noted somewhere in one of these discussions (& of course can't find now because the ASC search engine seems to be close to useless) it seems like sometimes Photos can't make changes in response to Apple Events very quickly & this causes random errors in Applescripts with repeat loops. A workaround for this that sometimes (but not always) works is to insert a brief delay in the repeat loop, like "delay 0.1" for a tenth of a second pause before beginning the next loop.
The downside is obvious: applying this to a large number of items can greatly extend the execution time but it still beats having to do everything manually or in small batches ... when it works.
-
May 3, 2015 8:01 AM in response to turingtest2by léonie,I'd started with a script that included the extensions but then realised that I'd rather remove them. Possibly a little late in the day for you but this is my variation on the theme:
How about including a parameter? Sometimes the filename extensions are useful, so we can see at a glance if we are about to trash the high resolution raw or a jpeg thumbnail.
-
May 3, 2015 1:26 PM in response to léonieby turingtest2,Is there a definitive list of the file types/extensions that Photos imports? As written the script will only remove the extensions .jpg, .mov, and .png from a title that has already been set, and it won't add any extensions to new titles it creates. It ought to be fairly easy to tweak things so that certain extensions are always added and others always removed, whether a title has already been set, or is being set for the first time from the filename, but I might as well make sure I know all of the correct extensions before I start. The user could edit the two parameters for included and excluded extensions.
tt2
-
May 3, 2015 2:01 PM in response to turingtest2by léonie,I searched the Photos help, but it only describes the file formats it can export, but not the file formats it can import. with all he supported RAW formats the list of filename extensions will be pretty long, if yu look at the list of supported cameras:
OS X Yosemite: Supported digital camera RAW formats - Apple Support
-
May 3, 2015 2:28 PM in response to léonieby turingtest2,The internal formats aren't an issue in this respect, just the filename extensions that are used. Do your RAW images have a .raw file extension, or do different camera makers use different extensions?
tt2
-
-
May 3, 2015 3:56 PM in response to inverseroomby Jay Deitch,Inverseroom,
I've been having problems getting a script to work to batch change the titles (see batch title change in photo version 1 ). I saw your comment, above, about selecting the "All Photos" album instead of the Photos tab. I tried it and it worked with the batch change script!
Thanks for the observation,
Jay
-
May 3, 2015 4:24 PM in response to Old Toadby turingtest2,It would be easy enough to snip off the last four characters, but I want to make sure they are only removed if they match a known extension, so running the script repeatedly on the same selection won't have adverse effects.
The script I've posted will strip off .jpg, .mov, or .png already. For anything else expand the string of known file extensions as required, e.g. "jpg|mov|png" could become "jpeg|jpg|mov|png|tif|tiff" and any other extensions that you might want to remove. What extensions exist in your Photos library?
tt2
-
May 3, 2015 7:15 PM in response to léonieby turingtest2,Here is an improved version that allows specific extensions to be excluded or included in the titles.
-- Photos | Filename to Title | V1.2
-- Images with no title will have the filename used as the title
-- Use values below to exclide 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
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
It could be further enhanced by adding specific text to the title instead of the extension, e,g, add say " | RAW" instead of ".cr2", or various other automatic substitutions could be made.
tt2
-
May 3, 2015 9:05 PM in response to turingtest2by R C-R,I noticed something I don't understand occurring in both the original & improved version of your script:
When I run it in Script Editor with the log window set to show replies, every time the script calls 'offset of' inside the tell application "Photos" block, the log shows that statement returns error number -10004. That is immediately followed by an end tell, a tell current application block with a duplicate of the 'offset of' statement that returns the correct offset value, & then another tell application "Photos" statement to resume the tell Photos block. For example:
tell application "Photos"
offset of ".jpg" in ".cr2"
--> error number -10004
end tell
tell current application
offset of ".JPG" in ".cr2"
--> 0
end tell
tell application "Photos"
This doesn't prevent the script from running -- it is like Photos can't hand off the offset string command to StandardAdditions but Script Editor (or something) is smart enough to handle the error & do that internally. FWIW, the error is eliminated by enclosing each of the 'offset of' commands inside the tell application "Photos" block in a tell me to statement to make that explicit.
Anybody else seeing this?
-
May 4, 2015 2:53 AM in response to R C-Rby turingtest2,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 dialog message with title info buttons {"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
-
May 4, 2015 3:14 AM in response to turingtest2by turingtest2,Bah, nothing is ever easy. That works fine if you run the script from the script editor, but If I try to wrap it up as part of an automator action in the way that léonie has shown in Script: Batch Changing the Titles to the Filename w/Extension then the use scripting additions clause won't compile, but tell me... end tell seems to work.
tt2