0

How can I iterate over the media items of an album in Photos.app?

By copying and editing online code, I got this far:

for (const album of Application("Photos").albums()) {
    console.log("Processing album '" + album.name() + "'")
    for (var idx in album) {
        console.log(idx)
     } 
}

However, this doesn't work. I know that each album in Application("Photos").albums() is a container, but I couldn't find an example how to iterate over its content using javascript.
In other code, I have already created a javascript Map where each key is the filename(+size+ratio) and it contains lists of duplicates (all but one to be deleted).
Iterating through the albums, I want to extract the properties of the media items to check whether an item is to be deleted and if yes, replace them with the non-deleted duplicates.

Any help or pointers to examples greatly appreciated!

4
  • Are you using Node or some other scripting environment? Please tell us how you are invoking this code plus what does console.log(Application("Photos")) output? Commented Jan 3, 2022 at 14:54
  • Thanks for your help. I am working directly in apple's ScriptEditor.app. I just checked console.log(Application("Photos")) which results in Error -2700: Script error.. However, I think I found the answer to my original question, which I will post in a minute...
    – Harmeling
    Commented Jan 3, 2022 at 15:31
  • Is console.log() valid inside of Script Editor? Doesn't work for me. Commented Jan 3, 2022 at 15:51
  • Try "Show Log" (or Cmd-3) from the View menu in the menu bar. Then there should be an area below the the code with four tabs "Result", "Messages", "Events", "Replies". Choose "Messages". Next run your script, e.g. console.log("Hello world"). Does it work?
    – Harmeling
    Commented Jan 4, 2022 at 13:33

1 Answer 1

1

Ok, after reading https://stackoverflow.com/questions/59471889/jxa-get-containers-of-an-element I found out, how to do it:

for (const album of Application("Photos").albums()) {
    console.log("Processing album '" + album.name() + "'")
    for (var photo of album.mediaItems()) {
        console.log(photo.filename())
    }
}

So, the correct way to do it is to iterate over the album.mediatItems() with for (var ... of ...) { ... }.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .