RhetTbull / osxphotos

Python app to work with pictures and associated metadata from Apple Photos on macOS. Also includes a package to provide programmatic access to the Photos library, pictures, and metadata.
MIT License
2.1k stars 98 forks source link

osxphotos add-locations causes ValueError: Invalid photo id #1644

Open danVnest opened 2 months ago

danVnest commented 2 months ago

Bug Description When I run osxphotos add-locations --window "2 hours" it eventually fails with a ValueError like the following: ValueError: Invalid photo id: B4E1567F-F5BF-4AC9-912D-AEEACE720F01

This also occurs with the default window osxphotos add-locations. It doesn't always fail with the same photo id.

Expected behaviour I'm not sure why these photos are missing, but it should probably just ignore them and keep searching? Potentially a very similar fix to https://github.com/RhetTbull/osxphotos/issues/1623

Additional Details

Traceback ![Warp - ook~codevideo-tools - 2024 08 13 at 16 39 43@2x](https://github.com/user-attachments/assets/61103480-4005-4f43-9069-2b6890ff36ab)
Version ![Warp - ook~codevideo-tools - 2024 08 13 at 16 38 23@2x](https://github.com/user-attachments/assets/54922374-c998-488d-85e9-527c00f63828)
RhetTbull commented 2 months ago

Thanks for the detailed report. Yes, I think this is the same thing as #1623 so the fix should be simple. What I think is happening is that the initial query that osxphotos runs to find all photos is finding photos that cannot be accessed programmatically via AppleScript (which is required to make the metadata change in Photos). The Photos AppleScript interface is finicky and has limitations. For example, it cannot access photos in the "Recently deleted" folder nor can it access hidden photos if the hidden folder is not unlocked. In recent versions of Photos Apple has also added "syndicated" photos from Messages and other apps that appear in the library but unless the user has explicitly imported them, AppleScript cannot access these photos.

I'll try to fine-tune the initial query to ensure these photos are excluded but will also catch the error and ignore it if it happens.

danVnest commented 2 months ago

Thanks for looking into this so quickly. That sounds like a good plan. Let me know if I can do any testing to help out.

RhetTbull commented 2 months ago

Edit: previous comment was on wrong issue. Still looking into this one!

allcontributors[bot] commented 2 months ago

@RhetTbull

I've put up a pull request to add @danVnest! :tada:

makuche commented 1 day ago

Hi @RhetTbull, I was running osxphotos add-locations --window "30 min" and I encounter the same error as described by @danVnest above: ValueError: Invalid photo id: A3D.... For context, this is on macOS Sequoia 15.0.1. When running the same via the --dry-run, everything seems to work fine. I took a look into the code base, and since the dry-run flag is only skipping photoscript.Photo(photo.uuid).location, I have encapsulated the line with a try-except statement, skipping uuid's that could not be parsed. However in my case, it seems to skip actually all UUIDs of pictures that miss a location and not just a subset. Any ideas what causes this?

RhetTbull commented 1 day ago

@makuche I don't have access to Sequoia to test yet so cannot be certain of the issue. It sounds like something may have changed with the AppleScript interface. Could be a permissions issue or could be Apple changed something with the AppleScript interface that OSXPhotos relies on to interact with Photos. I tend to run one version behind Apple's latest macOS for various reasons. Mostly because upgrading is a pain and always breaks something. I always try to ensure that osxphotos export will work with the latest Photos database version but the interactive tools such as add-locations, import, and timewarp require testing on a machine that actually is running the latest OS version. I'll look into this when I have a chance.

If you're a python user and want to try debugging this, something like the following might be helpful:

Select a photo then run:

osxphotos repl <--- opens an interactive Python REPL that loads your Photos database in OSXPhotos.

Then at the REPL prompt, see if you can instantiate a Photo object (which is a python proxy to the AppleScript object):

>>> import photoscript
>>> photo = selected[0]
>>> photo.uuid
'2F00448D-3C0D-477A-9B10-5F21DCAB405A'

>>> photoscript.Photo(photo.uuid)
<photoscript.Photo object at 0x116277da0>

>>> photoscript.Photo(photo.uuid).filename
'IMG_6501.jpeg'

>>>
makuche commented 1 day ago

Thanks for the quick response @RhetTbull ! Some experiments in the REPL indicate that there is an issue with the UUIDs, as every UUID returns the same ValueError: Invalid photo id, and I am certain I am using a ID that maps to an existing PhotoInfo object. I assume that there is some issue with the photoscript.applescript that is used when initializing a photoscript.Photo object, but I am not entirely sure, as I don't have experience with applescript

RhetTbull commented 1 day ago

@makuche A couple more debugging things to try.

First, ensure that your terminal app has access to scripting Photos via System Settings > Privacy & Security > Automation > Terminal > Photos. Replace Terminal with whatever your terminal app is (e.g. iTerm2 if that's what you're using. The default macOS terminal app is Terminal)

If you see Photos and it's enabled then it's not a permission issue.

Open Photos and select a photo. In the Terminal run:

osxphotos query --selected --print "{photo.uuid}" --quiet

Does that work? (e.g. can OSXPhotos access the selected photos in Photos?) If it does work, you should see a UUID like 5285C4E2-BB1A-49DF-AEF5-246AA337ACAB printed to Terminal.

If not, then try this:

Open Script Editor, then paste the following code and hit the run button (triangular "play" button):

set theItemids_ to {}
tell application "Photos"
    set theItems to selection
    repeat with theItem in theItems
        copy id of theItem to end of theItemids_
    end repeat
end tell

In the Result window at the bottom of the screen you should see the resulting UUID. It will look something like this:

"5285C4E2-BB1A-49DF-AEF5-246AA337ACAB/L0/001"

Copy the value then run the following code and open a new Script Editor window. Paste the following replacing the UUID with in the snippet below with the value you copied:

tell application "Photos"
    set thePhoto to media item id ("5285C4E2-BB1A-49DF-AEF5-246AA337ACAB/L0/001")
end tell

Run it and you should something like this in the Result window:

media item id "5285C4E2-BB1A-49DF-AEF5-246AA337ACAB/L0/001" of application "Photos"

Now run it again, but remove the "/L0/001" from the end of the UUID:

tell application "Photos"
    set thePhoto to media item id ("5285C4E2-BB1A-49DF-AEF5-246AA337ACAB")
end tell

The result should be the same:

media item id "5285C4E2-BB1A-49DF-AEF5-246AA337ACAB/L0/001" of application "Photos"

If one or more of these steps fails, it may provide some clues as what the issue is.