Slow enumeration of images in collection

Discussion regarding all scripting related questions
Please DO NOT post to this thread anything that is not directly related to scripting of Capture One.

Slow enumeration of images in collection

Postby StoppingDown » Tue Jul 30, 2019 2:20 pm

I'm trying to write my first Applescript scripts - I'm a professional software engineer and I know a number of programming languages, but Applescript is totally new to me and appears really unusual...

I have this code.

Code: Select all
on processCollection(_collection)
   set _name to (name of _collection) as string
   set _properties to (properties of _collection)
   my trace("Processing " & _name)
   
   tell _collection to set _images to images  # ?!?!
   set _imageCount to count of _images
   my trace("# of image " & _imageCount)
   
end processCollection


The subroutine is called with a proper collection - I can see in "replies"

Code: Select all
   get properties of collection id "7967" of collection id "6217" of collection id "15" of document "Fabrizio's Catalog"
      --> {sorting order:by name, user:true, compare variant:missing value, sorting reversed:false, file:missing value, id:"7967", kind:album, rules:missing value, name:"06/21-24. Hautes-Alpes", class:collection}


Then I'd like to exctract the collection of contained photos and their count - but clearly I don't know what to write at line marked with !?!?! - the current code doesn't work (it interprets 'images' as a variable, and not the element 'image' inside _collection.

I've looked at some example code in other posts, but they are still too complex for me.

Thanks.
Thanks.
StoppingDown
 
Posts: 11
Joined: Wed Oct 01, 2014 11:02 am
Location: Genoa, Milan (Italy)

Re: Slow enumeration of images in collection

Postby StoppingDown » Tue Jul 30, 2019 6:54 pm

Ok, i did it (so I changed the subject). It didn't work because it needed another "tell application ..." (which of course is in the main code, but i didn't get it must be repeated in a handler).

My problem now is that the code executes slowly, approximately 2 images per second... Ideas?

Code: Select all
on createImagesXml(_collection)
   set _r to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" & CRLF & "<gallery>" & CRLF & "    <album>" & CRLF
   
   tell application "Capture One 12"
      set _kind to (kind of _collection) as string
      # FIXME assert _kind is album
      tell _collection to set _images to variants
      repeat with i from 1 to length of _images
         set _image to item i of _images
         set _imageProperties to properties of _image
         set _imageName to name of _imageProperties
         set _r to _r & "        <img src=\"" & _imageName & ".jpg\"/>" & CRLF
         my trace("IMAGE: " & _imageName)
      end repeat
   end tell
   
   set _r to _r & "    </album>" & CRLF & "</gallery>" & CRLF
end createImagesXml
StoppingDown
 
Posts: 11
Joined: Wed Oct 01, 2014 11:02 am
Location: Genoa, Milan (Italy)

Re: Slow enumeration of images in collection

Postby StoppingDown » Wed Jul 31, 2019 12:38 pm

I made the whole source available on BitBucket:

https://bitbucket.org/fabriziogiudici/c ... pplescript
StoppingDown
 
Posts: 11
Joined: Wed Oct 01, 2014 11:02 am
Location: Genoa, Milan (Italy)

Re: Slow enumeration of images in collection

Postby Eric Nepean » Wed Jul 31, 2019 3:45 pm

Hi Fabrizio
500ms per image seems unexpectedly slow side.

Part of the explanation may be that the Apple events that communicate between processes are slow communication. I'd open up the Script Editor log history window, select Replies, and see how many events are being sent, and how many replies. It could be you have much redundant messaging.

The secret of speeding up Applescript is to to ask for information about as many objects as possible as possible in one message. Asking Capture One for long complex records, like properties, can be slow. Processing such records can also be quite complex. I tend not ask for "properties of ..." unless I'm debugging.

I would write your code like this:
Code: Select all
tell application "Capture One 12" to set _ImageNames to name of images
set _ImageCount to count of _ImageNames
tell application "Capture One 12" to tell current document to tell current collection to set {_albumName, _albumKind} to {(its name), ((its kind) as text)}

An Image may have one or many Variants so in your case you need identify and count images.
Eric Nepean
 
Posts: 615
Joined: Sat Oct 25, 2014 8:02 am
Location: Ottawa

Re: Slow enumeration of images in collection

Postby Eric Nepean » Wed Jul 31, 2019 4:47 pm

After reading your whole source, a bit more code. Runs almost instantly for me in a test collection with about 20 Images.

Code: Select all
tell application "Capture One 12" to set {_ImageNames, _ImageVariantRating, _ImageCaptureDate} to {(name of images), ((rating of variant) of images), (EXIF capture date of images)}
tell application "Capture One 12" to tell current document to tell current collection to set {_albumName, _albumKind} to {(its name), ((its kind) as text)}

set _ImageCount to count of _ImageNames
set _ImageVariantCount to {}
set _ImageMaxRating to {}
repeat with theVariantRatingsL in _ImageVariantRating
   set maxRating to 0
   repeat with theRating in theVariantRatingsL
      if maxRating < (get theRating as integer) then set maxRating to (get theRating as integer)
   end repeat
   set end of _ImageMaxRating to maxRating
   set end of _ImageVariantCount to count of theVariantRatingsL
end repeat
Eric Nepean
 
Posts: 615
Joined: Sat Oct 25, 2014 8:02 am
Location: Ottawa

Re: Slow enumeration of images in collection

Postby StoppingDown » Wed Jul 31, 2019 5:46 pm

Thanks! Problem fixed - now it runs in just a few seconds.

This is the change in details (I'm just extracting image names instead of the whole thing, as you suggested):

https://bitbucket.org/fabriziogiudici/c ... e5a9e67879
StoppingDown
 
Posts: 11
Joined: Wed Oct 01, 2014 11:02 am
Location: Genoa, Milan (Italy)

Re: Slow enumeration of images in collection

Postby Eric Nepean » Wed Jul 31, 2019 7:47 pm

I think you are still making one subtle error, you are getting name of variants. It’s ok now but in the long run will result in over counting the number of images, and number variants will not match the number of images.

I recommend that you get name of images. Besides the number of items, the name of the image includes the the file extension, which is not part of the name of the variant.
Eric Nepean
 
Posts: 615
Joined: Sat Oct 25, 2014 8:02 am
Location: Ottawa

Re: Slow enumeration of images in collection

Postby StoppingDown » Thu Aug 01, 2019 8:00 am

Thanks. Actually, for the way I'm working, at the moment there's no difference - I'm running the script on albums of photos to be published, and I put in them a single variant for image; furthermore what I need is the name of the image without extensions, to which I append ".jpg" because that's the way the images are exported.

But I've added a TODO in my notes to fix it, because I could change my conventions in future. My priority is now to fix an issues with the manipulation of some XML files related to the job (this is plain Applescript, not related to Capture One).
StoppingDown
 
Posts: 11
Joined: Wed Oct 01, 2014 11:02 am
Location: Genoa, Milan (Italy)

Re: Slow enumeration of images in collection

Postby Eric Nepean » Thu Aug 01, 2019 4:05 pm

I'm working on something similar.

In newer versions of OSX, a n application is required to be re-"approved" after modifying itself.
As a consequence saving current settings and states in Globals and Properties will no longer be practical.

System Events, in its Property list suite has a couple of comands that generate and modify PLIST ot property list files.
There are nothing but a file containing an XML structure, and the commands are there to move records to the XML structure and read them back.
https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/WorkwithPropertyListFiles.html

Some comments here about XML Libraries
https://forum.latenightsw.com/t/is-there-an-xmllib-script-library/2026

I use their Script Degugger tool which is much better than Script Editor.
Eric Nepean
 
Posts: 615
Joined: Sat Oct 25, 2014 8:02 am
Location: Ottawa

Re: Slow enumeration of images in collection

Postby rapdigital » Thu Aug 08, 2019 10:11 pm

I try to avoid declaring variables that start and/or end with an underscore. I dont recall the full reason (obj-C uses it) but its a good general rule.
rapdigital
Certified Professional
Certified Professional
 
Posts: 181
Joined: Mon Feb 02, 2009 1:52 pm


Return to Scripting



Who is online

Users browsing this forum: No registered users and 1 guest