Search code examples
applescript

AppleScript Photos App "Invalid Index" Error on Sub-Folders


I am writing an AppleScript to organize photos taken that day, into albums grouped by location and time. That has nothing to do with my issue but it's the end goal for context. Normally I do this all manually so there's no issue, but I want to speed up my work so I am writing a script to do it for me. From the start, I'm having issues indexing the folder I want to create sub-folders in.

ApplesScript seems to be unable to index "a folder within a folder", but it can index "a folder" just fine. It is my understanding that folders are an element of the Photos application, so they can be addressed directly without needing to know the path that leads to them.

Ignoring the fact that I can rearrange my folders to compensate for this error, I want to know what I'm doing wrong so I can address deeper folders/albums.

Currently, the hierarchy of folders looks like:

My Albums > Business > Work Photos

Where I put an album for each job inside Work Photos.

If I try to get the ID if the Work Photos folder, I get the error: "Photos got an error: Can’t get container 1 whose name = "Work Photos". Invalid index."

If I move Work Photos up in the hierarchy so it looks like:

My Albums > Work Photos

There is no issue, the code returns the folder ID.

The only thing different between the working and non-working scenario is that in the first scenario that doesn't work, I am looking for a folder inside another folder, inside My Albums, which I believe is a folder. In the second scenario that works fine I am looking for a folder directly inside My Albums.

The AppleScript is very simple:

tell application "Photos"   
set detailingFolder to the id of the first container whose name is "Work Photos"
display dialog detailingFolder 
end tell

If I try this AppleScript instead:

tell application "Photos"   
set detailingFolder to the folder of "Business" whose name is "Work Photos" 
display dialog detailingFolder 
end tell

I get error: "Can’t get folder of "Business"." number -1728 from «class IPfd» of "Business"


Solution

  • When indexing, you need to specify the reference/name/index/id for each level in the hierarchy. For example, you may have several base folders, so you can search for the one you are looking for by using something like the first folder whose name is "Business". You then use that reference when looking for folders contained in it, the resulting reference when looking for folder in that, and so on. Using filter references (whose/where) tends to get a bit wordy, so you can use tell statements or variables to make it a little more readable, but if you know the exact hierarchy it is a lot simpler. For example:

    tell application "Photos"
       set harder_to_read to first folder of (first folder whose name is "Business") whose name is "Work Photos"
       -- or --
       set hard_to_read to first folder of (folder "Business") whose name is "Work Photos"
       -- or --
       tell (first folder whose name is "Business") -- folder "Business"
          tell (first folder of it whose name is "Work Photos") -- folder "Work Photos"
             set wordy_but_flexible to it
          end tell
       end tell
       -- or --
       set simple to folder "Work Photos" of folder "Business"
    end tell
    

    Note there will be an error if a folder doesn't exist, so if that is a possibility a try statement or intermediate tests can be added.