AppleScript Syntax for iPhoto Automation

I looked through some recommendations through Google to get me to the point that I need to do iPhoto through AppleScript, but haven’t found much yet. There are various old scripting discussions for various old versions of iPhoto floating around, but nothing that was particularly useful with what I needed. Basically, in pseudo code, I want to do this:

for each photo in library if photo.Description contains "a known string" photo.Description = photo.Description.Replace("a known string", "") end if end for 

That is, I have a fragment of erroneous text that made my way into every (well, almost every) photograph in my library. I guess that at some point last time I messed up the batch change and still haven't noticed it. Either that, or upgrading from iPhoto '08 to '11 did it somehow. In any case, the net result will be the same.

I'm not good at AppleScript, and it's hard for me to find the right syntax / dictionary to use in this. Basically, I'm in the tell application "iPhoto" , but I don’t know what to say. If the hierarchy of how photos are organized in the library is important:

  • Each photo is organized in chronological order of events. (Events are my primary form of organization.)
  • There are a large number of albums, but not all in the album.
  • There is one smart album that contains every mistake. This, of course, is based on the presence of a known line in the description of the photo. Therefore, I suggest that perhaps this should be borne in mind if the last code is drawn through the photos in this smart album, since a smart album can change a repeating array, no?

Does anyone have any links or sample code to help me? Conversely, does anyone know a better way to do this one-time fix?

Edit: I checked the test with the following code:

 tell application "iPhoto" activate set thePhotos to get every photo repeat with aPhoto in thePhotos if aPhoto comment contains "[known string]" then log aPhoto comment tell aPhoto to set it comment to text 1 thru (offset of "[known string]" in aPhoto comment) of aPhoto comment log aPhoto comment exit repeat end if end repeat end tell 

This led to the following conclusion:

 tell application "iPhoto" activate get every photo get comment of photo id 4.294977224E+9 (*comment of photo id 4.294977224E+9*) offset of "[known string]" in comment of photo id 4.294977224E+9 «event ascrgdut» offset of "[known string]" in comment of photo id 4.294977224E+9 end tell tell current application offset of "[known string]" in «class pcom» of «class ipmr» id 4.294977224E+9 Result: error "iPhoto got an error: Can't make comment of photo id 4.294977224E+9 into type string." number -1700 from comment of photo id 4.294977224E+9 to string 

Edit: I had time to work with him this morning, and it looks like all that was needed was some type. This code now successfully changes the first matching photo:

 tell application "iPhoto" activate set thePhotos to get every photo repeat with aPhoto in thePhotos if aPhoto comment contains "[known string]" then log aPhoto comment as text set theComment to aPhoto comment as text set theComment to text 1 thru (offset of "[known string]" in theComment) of theComment tell aPhoto to set it comment to theComment log aPhoto comment as text exit repeat end if end repeat end tell 

Now to backup my library and delete exit repeat . And probably keep doing something else for a while :)

+4
source share
2 answers

Here is the brute force version. This will go through each photo. You can make it more elegant if you want.

 tell application "iPhoto" set thePhotos to get every photo repeat with aPhoto in thePhotos if aPhoto comment contains "theString" then tell aPhoto to set it comment to "newString" end if end repeat end tell 
+4
source

How about this one. You will need to make an album or a smart album of items that you want to influence, but this is in any case less destructive.

 tell application "iPhoto" activate set thePhotos to get every photo in current album whose comment contains "TEST123" repeat with aPhoto in thePhotos tell aPhoto to set it comment to "123TEST" end repeat end tell 
+1
source

Source: https://habr.com/ru/post/1335768/


All Articles