Sunday, December 4, 2022

#1 2022-10-16 06:34:07 am

tempel
Member
From:: Germany
Registered: 2010-11-19
Posts: 81
Website

Every object from a list whose property is one in a list of object IDs

I am scripting Photos.app. I have a list of object IDs of type "media item", and I need to get all the media items that match one of the given ids.

Basically, something like this:

Applescript:

set theIDs to {"6S%QjtwmQcK22i6EV7Yyow", "BM12SMwNTDK+yZnZrXUsOA"}
tell application "Photos"
   set theItems to every media item whose (theIDs contains id)
end

The above leads to the error "Can't make {…} into type specifier".

I also tried to convert the list to a string and then check if "id is contained by theString", but that fails with the same error, too.

How to I accomplish in the most efficient (performance) way?

Last edited by tempel (2022-10-16 07:08:32 am)

Offline

 

#2 2022-10-16 07:15:34 am

tempel
Member
From:: Germany
Registered: 2010-11-19
Posts: 81
Website

Re: Every object from a list whose property is one in a list of object IDs

One solution I found was to go over the list of IDs and fetch the media items by their ids. This works because any object can be looked up by their ID directly. It would not work for any other property of such objects.

Applescript:

set theItems to {}
repeat with anID in theIDs
   set anItem to media item id anID
   set end of theItems to anItem
end repeat

Offline

 

#3 2022-11-07 04:43:16 am

technomorph
Member
Registered: 2017-12-14
Posts: 304

Re: Every object from a list whose property is one in a list of object IDs

Yeah I’ve attempted everything I could trying the same thing
Getting all iTunes library tracks who’s persistentID is in an array of persistentIDs.

Kept getting errors that were something like.  “Can’t use grouping operator on SBElement array.
The “IN” predicate for AppleScript and ScriptingBridge seems to only work with strings.

I tried predicate with block.  Which again can’t be used.
I finally found this post and it works and doesn’t seem to be too slow .
(My “in” list is never more than 250 Objects.

You create a compound OR predicate and use that to filter.
You have to translate it to AsOBJc but it will work (let me know if you need help translating it)

https://stackoverflow.com/questions/167 … ementarray

Offline

 

#4 2022-11-07 08:42:22 am

robertfern
Member
Registered: 2011-11-29
Posts: 225

Re: Every object from a list whose property is one in a list of object IDs

I made a simple repeat loop

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set theIDs to {"89288F35-8BCC-49AF-BBDF-C5DF0AD19455/L0/001", "67AD9ECC-C484-468A-89E4-439A94F3CDF5/L0/001"}
set theItems to {}
tell application "Photos"
   repeat with anID in theIDs
       set end of theItems to media item id (contents of anID)
   end repeat
end tell

This should work pretty well depending on the number of IDs you have in "theIDs".
How many on average do you normally have?

Last edited by robertfern (2022-11-07 10:05:04 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)