Using Applesript to select an image

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

Using Applesript to select an image

Postby Skids » Sun Nov 24, 2019 9:29 am

Hi,

I'm struggling to manage to use Applescript to command Capture One to select an image. The dictionary informs me that the syntax is
Select document variant
but I am unable to make it work. I suspect that the problem with my script is that I am not referencing the variant correctly.

I'm experimenting and have Capture One returning both a list of images and a list of variants that are in a target collection, these lists are of the form:

e.g.
List of five items
variant id "46062" of collection id "289" of document "SK_Images"
... and so on

List of five items
image id "46063" of collection id "289" of document "SK_Images"
... and so on
These items are objects with many parameters.

However I am unable to command Capture One to select a particular image or variant .

My somewhat scrappy script is here. The collection "Pink" contains five images and Capture One is open and the catalog that holds the target image is loaded.

Code: Select all
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--set tTargetImageName to "NameOfImageToDisplay"
-- hard code to known image _1060652.rw2
-- image is in collection "Pink"
set tTargetImageName to "_1060652" -- uncertain how C1 deals with extensions
--tell application "Finder" to open :Users:skids:Pictures:Capture_One_Catalog.cocatalog
--tell application "Capture One 12" to open POSIX file "/Users/skids/Pictures/Capture_One_Catalog.cocatalog"

tell application "Capture One 12" to tell current document
   --set current collection to collection "All Images"
   -- test using a small collection of five images
   set current collection to collection "Pink"
   set CollectionID to the id of the current collection
   
   -- Note : name of image returns name including extension whereas name of variant returns name minus extension
   set tListOfNamesImage to get name of every image
   set ImageCount to count of tListOfNamesImage
   -- List of 5 names
   
   set tListOfNamesVariant to get name of every variant
   set VariantCount to count of tListOfNamesVariant
   --set everyImageIDList to get name of every image
   --List of 5 names without extensions
   
   set AllVariants to (get variants)
   set AllImages to (get images)
   -- ***** ATTEMPTING TO TEST THE SELECTION OF AN IMAGE OR VARIANT ***
   select variant "46071" --of collection id "289"
   
   set ItemCount to 0
   repeat with theImageName in tListOfNamesVariant
      set ItemCount to ItemCount + 1
      if theImageName as string = tTargetImageName then
         
         set tId to the id of item ItemCount of tListOfNamesVariant
         select variant tId -- List of 8 integers ?
         --select the image tTargetImageName
         say "Found and Selected"
         exit repeat
      end if
      
   end repeat
   say "Nothing found"
end tell


The obvious line Select variant id nnnnn does not work so can anyone tell me how I can select an image that is in a collection or better still have Capture one create a smart collection based on a search for a given image name?

best wishes
Simon
Skids
 
Posts: 36
Joined: Wed Apr 22, 2015 2:38 pm

Re: Using Applesript to select an image

Postby emorydunn » Sun Nov 24, 2019 8:05 pm

If you wanted to make a smart album that filters for a certain name this is the rule you would use, replacing **YOUR FILE NAME HERE** with the actual name:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?><MatchOperator Kind="AND"><MatchOperator Kind="AND"><Condition Enabled="YES"><Key>displayName</Key><Operator>6</Operator><Criterion>**YOUR FILE NAME HERE**</Criterion></Condition></MatchOperator></MatchOperator>


This script might be a good starting point for making smart albums. It shows how to pull information from Capture One, set up the rules, and create an album.
https://github.com/emorydunn/CaptureOneScripts/blob/master/Capture%20One%20Scripts/smartAlbumForSelection.applescript
User avatar
emorydunn
 
Posts: 27
Joined: Fri Jun 14, 2013 7:36 am
Location: San Francisco, California

Re: Using Applesript to select an image

Postby Skids » Sun Nov 24, 2019 11:35 pm

Emory,

Thanks for the xml and the link to your git hub entry. Is the xml line is documented anywhere? I will have a play with the xml and your script tomorrow as it is getting late.

best wishes

Simon
Skids
 
Posts: 36
Joined: Wed Apr 22, 2015 2:38 pm

Re: Using Applesript to select an image

Postby emorydunn » Sun Nov 24, 2019 11:57 pm

The XML is what’s returned when you call a smart album’s rule parameter. I don’t believe the XML itself is documented anywhere, so the easiest way to get it is to make a test album and change the parameters.
User avatar
emorydunn
 
Posts: 27
Joined: Fri Jun 14, 2013 7:36 am
Location: San Francisco, California

Re: Using Applesript to select an image

Postby rapdigital » Mon Nov 25, 2019 12:27 am

I don’t use catalogs at all so not sure if there are different syntax requirements but I’d say that you need the path to a file to set it to the primary variant or selected variant rather than just the name.

Possible issue with generating an XML is getting CO to refresh and read it. Before create collections and favourites where added to the A’s dictionary I used to do XML and it gets messy. Again this was with sessions, catalogs may be different.
rapdigital
Certified Professional
Certified Professional
 
Posts: 185
Joined: Mon Feb 02, 2009 1:52 pm

Re: Using Applesript to select an image

Postby emorydunn » Mon Nov 25, 2019 1:22 am

The XML is just for setting the smart album's rules. As far as I know there's no native AppleScript way to do that. If there is I'd love to know about it.
User avatar
emorydunn
 
Posts: 27
Joined: Fri Jun 14, 2013 7:36 am
Location: San Francisco, California

Re: Using Applesript to select an image

Postby Skids » Mon Nov 25, 2019 11:54 am

Excellent! Many thanks, I had failed to discover the rule property so had been attempting to do search outside Capture One which was proving to be difficult and slow.

Using your xml and some lines of your script I am well on the way to enabling Capture One to operate alongside a standalone DAM.

When you say
when you call a smart album’s rule parameter
are you calling from within Capture One or is this an Applescript command. I should say I've looked in Capture One but not found anyway of directly extracting the XML.

My code which is a modification of yours is below and includes a couple of lines to read the xml associated with a collection.
Code: Select all
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set tTargetImageName to "_1060652" -- search key displayname fails if extension is included

-- build unique name for new collection
set {year:y, month:m, day:d, hours:h, minutes:min, seconds:s} to (current date)
set tCollectionName to (y & m & d & "_" & h & min & s & "_" & "MyNewCollection") as string

-- build the xml search string
set theRules to "<?xml version=\"1.0\" encoding=\"UTF-8\"?><MatchOperator Kind=\"AND\"><MatchOperator Kind=\"AND\"><Condition Enabled=\"YES\"><Key>displayName</Key><Operator>6</Operator><Criterion>" & tTargetImageName & "</Criterion></Condition></MatchOperator></MatchOperator>"

tell application "Capture One 12" to tell current document
   
   -- Create a new smart collection and then select it
   make new collection with properties {name:tCollectionName, kind:smart album, rules:theRules}
   set current collection to collection tCollectionName
   
   -- read back the rules set
   set tExtractedRules to (get the rules of collection tCollectionName)
   log tExtractedRules
end tell


While all the above seems to work I will keep an eye open for problems as described by RapDigital.

Thanks to you both for solving the problem.

Simon
Skids
 
Posts: 36
Joined: Wed Apr 22, 2015 2:38 pm

Re: Using Applesript to select an image

Postby rapdigital » Mon Nov 25, 2019 1:51 pm

I might be missing something but I think this will also work.
Not really tested but should work.
Code: Select all
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set searchItem to "_1060652.rw2" --change this to suit requires extension I think
set colName to "theCollection" --again this could be anything
tell application "Capture One 12"
   tell current document
      set theImage to every image of collection "All Images" whose name is searchItem
      if not (exists collection named colName) then
         set theAlbum to make new collection with properties {kind:album, name:colName}
      else
         set theAlbum to collection named colName
      end if
      add inside theAlbum variants (get variants of (get item 1 of theImage))
   end tell
end tell
rapdigital
Certified Professional
Certified Professional
 
Posts: 185
Joined: Mon Feb 02, 2009 1:52 pm

Re: Using Applesript to select an image

Postby Eric Nepean » Mon Nov 25, 2019 2:43 pm

No need for a special album for this.

The Applescript syntax for selecting works like this
Code: Select all
tell application "Capture One 20"
   set theID to id of first variant of current collection of current document
   select current document variant (get variant id theID)
   select current document variants (get variants 1 thru 4 of current collection of current document)
end tell


The target of the select command is a variant or a list of variants - objects that refer to a variant.
Eric Nepean
 
Posts: 637
Joined: Sat Oct 25, 2014 8:02 am
Location: Ottawa

Re: Using Applesript to select an image

Postby Skids » Mon Nov 25, 2019 3:31 pm

Wow!

Tested and working!

Thanks.

I've added to your script to allow multiple images to be added to the collection. Initially I tried using "every" but could not get it to work. Also attempting to append each record to the list using "end" in the first loop created a list with a deeper structure than desired item n > item 1 > image record.

Code: Select all
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set tSeekImages to {"_1060652.rw2", "_1060653.rw2"} --change this to suit requires extension
set colName to "theCollection" --again this could be anything
set tImageList to {}
tell application "Capture One 12"
   tell current document
      --build a list of image records based on the list of filenames in list tSeekImages
      repeat with tFileName in tSeekImages
         set theImage to (every image of collection "All Images" whose name is tFileName)
         set tImageList to tImageList & theImage
      end repeat
      
      -- create or use the collection named colname
      if not (exists collection named colName) then
         set theAlbum to make new collection with properties {kind:album, name:colName}
      else
         set theAlbum to collection named colName
      end if
      
      -- loop the list of image records adding each variant to the album
      repeat with tImage in tImageList
         add inside theAlbum variants (get variants of tImage)
      end repeat
      
   end tell
end tell


My scripting is quite limited and I naturally tend to writing a loops to either search for or add the the images to an Album. I wonder if it is possible to streamline the code with greater use of the "every" construct.

Thanks and best wishes

Simon
Skids
 
Posts: 36
Joined: Wed Apr 22, 2015 2:38 pm

Re: Using Applesript to select an image

Postby Skids » Tue Nov 26, 2019 10:42 am

Initially I missed Eric's post - thank you for publishing the syntax which eluded me. I will experiment with it later today.

I have conducted some simple timing tests seeking three images using the script I posted above. On a catalogue of 90,000 images the script takes 9 seconds to complete. On a catalogue of 9,000 images it completes in 1.4 seconds. No great surprises given what has been written about the Capture One SQL database.

The tests were conducted on a MacBook Pro (Retina, 15-inch, Late 2013), 2.3 GHz Quad-Core Intel Core i7, 16 GB 1600 MHz DDR3.

best wishes

Simon
Skids
 
Posts: 36
Joined: Wed Apr 22, 2015 2:38 pm

Re: Using Applesript to select an image

Postby Eric Nepean » Sun Dec 08, 2019 8:23 pm

Skids wrote:Wow!

Tested and working!

Thanks.

I've added to your script to allow multiple images to be added to the collection. Initially I tried using "every" but could not get it to work. Also attempting to append each record to the list using "end" in the first loop created a list with a deeper structure than desired item n > item 1 > image record.

Code: Select all
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set tSeekImages to {"_1060652.rw2", "_1060653.rw2"} --change this to suit requires extension
set colName to "theCollection" --again this could be anything
set tImageList to {}
tell application "Capture One 12"
   tell current document
      --build a list of image records based on the list of filenames in list tSeekImages
      repeat with tFileName in tSeekImages
         set theImage to (every image of collection "All Images" whose name is tFileName)
         set tImageList to tImageList & theImage
      end repeat
      
      -- create or use the collection named colname
      if not (exists collection named colName) then
         set theAlbum to make new collection with properties {kind:album, name:colName}
      else
         set theAlbum to collection named colName
      end if
      
      -- loop the list of image records adding each variant to the album
      repeat with tImage in tImageList
         add inside theAlbum variants (get variants of tImage)
      end repeat
      
   end tell
end tell


My scripting is quite limited and I naturally tend to writing a loops to either search for or add the the images to an Album. I wonder if it is possible to streamline the code with greater use of the "every" construct.

Thanks and best wishes

Simon

I have developed some techniques to optimize Applescriots for large catalogs. Basic principles:
  • Minimize the number of commands sent Capture One
  • Avoid sending long lists of images or variants as a parameter to a Capture One command, replace these with a filter
Example One
The first form is extremely fast, the second form is faster than the third form, because the third form causes Capture One to search for each variant individually
Code: Select all
deselect variants (every variant)  --1
deselect variants (variants whose name is tVariantName)  --2
deselect variants tVariantList  --3

Example 2
Consider the script above
  • The script first builds a list of names tImageList by telling Capture One to search for each Image
  • Then the script asks Capture One to search for the variants of each image
  • Then the script sends the list of variants back to Capture One to be added to a collection.
Optimisation
  • The variant also has a name property, which is the image name without the extension.
  • Avoid asking Capture one to search twice
  • Make the search part of the add inside theAlbum command
Code: Select all
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

use framework "Foundation"
use scripting additions
set tSeekImages to {"_1060652", "_1060653"} --extension not required
set colName to "theCollection" --again this could be anything

tell application "Capture One 12" to tell current document
   -- create or use the collection named colname
   if not (exists collection named colName) then
      set theAlbum to make new collection with properties {kind:album, name:colName}
   else
      set theAlbum to collection named colName
   end if
   
   repeat with tVariantName in tSeekImages
      add inside theAlbum variants (variants whose name is tVariantName)
   end repeat
end tell

Late Night Software has a Script Debugger product, which has really excellent dictionary and explorer tools. These make an exercise like this much simpler.
Eric Nepean
 
Posts: 637
Joined: Sat Oct 25, 2014 8:02 am
Location: Ottawa

Re: Using Applesript to select an image

Postby Skids » Mon Dec 09, 2019 6:31 pm

Thanks again,

I have a copy of Script Debugger but I find Application Applescript commands are always a bit of a challenge so some example syntax is always welcome.

best wishes

Simon
Skids
 
Posts: 36
Joined: Wed Apr 22, 2015 2:38 pm

Re: Using Applesript to select an image

Postby Eric Nepean » Tue Dec 10, 2019 1:27 am

Skids wrote:Thanks again,

I have a copy of Script Debugger but I find Application Applescript commands are always a bit of a challenge so some example syntax is always welcome.

best wishes

Simon

A subtle point that took me a while to figure out

Sometimes some code that should obviously work (to a human) comes back with a compile error.
Code: Select all
tell application "Capture One 12" to tell current document
   -- each line generates generates an error
   add inside theAlbum variants variants whose name is "AAAA" -- Expected expression but found parameter name
   add inside theAlbum variants  variants of tImage --- Expected expression but found parameter name
end tell
The problem is that the Applescript parser doesn't group words in the way we expect
The fix is to add brackets:
Code: Select all
tell application "Capture One 12" to tell current document
   add inside theAlbum variants (variants of tImage)
   add inside theAlbum variants (variants whose name is "AAAA")
end tell
--This compiles with out error

Adding brackets can't hurt. When I get a mysterious error, I start by adding brackets
Eric Nepean
 
Posts: 637
Joined: Sat Oct 25, 2014 8:02 am
Location: Ottawa

Re: Using Applesript to select an image

Postby NNN637149307921824277 » Sat Jan 18, 2020 7:55 am

Skids wrote:Hi,

I'm struggling to manage to use Applescript to command Capture One to select an image. The dictionary informs me that the syntax is
Select document variant
but I am unable to make it work. I suspect that the problem with my script is that I am not referencing the variant correctly.

I'm experimenting and have Capture One returning both a list of images and a list of variants that are in a target collection, these lists are of the form:

e.g.
List of five items
variant id "46062" of collection id "289" of document "SK_Images"
... and so on

List of five items
image id "46063" of collection id "289" of document "SK_Images"
... and so on
These items are objects with many parameters.

However I am unable to command Capture One to select a particular image or variant .

My somewhat scrappy script is here. The collection "Pink" contains five images and Capture One is open and the catalog that holds the target image is loaded.

Code: Select all
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--set tTargetImageName to "NameOfImageToDisplay"
-- hard code to known image _1060652.rw2
-- image is in collection "Pink"
set tTargetImageName to "_1060652" -- uncertain how C1 deals with extensions
--tell application "Finder" to open :Users:skids:Pictures:Capture_One_Catalog.cocatalog
--tell application "Capture One 12" to open POSIX file "/Users/skids/Pictures/Capture_One_Catalog.cocatalog"

tell application "Capture One 12" to tell current document
   --set current collection to collection "All Images"
   -- test using a small collection of five images
   set current collection to collection "Pink"
   set CollectionID to the id of the current collection
   
   -- Note : name of image returns name including extension whereas name of variant returns name minus extension
   set tListOfNamesImage to get name of every image
   set ImageCount to count of tListOfNamesImage
   -- List of 5 names
   
   set tListOfNamesVariant to get name of every variant
   set VariantCount to count of tListOfNamesVariant
   --set everyImageIDList to get name of every image
   --List of 5 names without extensions
   
   set AllVariants to (get variants)
   set AllImages to (get images)
   -- ***** ATTEMPTING TO TEST THE SELECTION OF AN IMAGE OR VARIANT ***
   select variant "46071" --of collection id "289"
   
   set ItemCount to 0
   repeat with theImageName in tListOfNamesVariant
      set ItemCount to ItemCount + 1
      if theImageName as string = tTargetImageName then
         
         set tId to the id of item ItemCount of tListOfNamesVariant
         select variant tId -- List of 8 integers ?
         --select the image tTargetImageName
         say "Found and Selected"
         exit repeat
      end if
      
   end repeat
   say "Nothing found"
end tell


The obvious line Select variant id nnnnn does not work so can anyone tell me how I can select an image that is in a collection or better still have Capture one create a smart collection based on a search for a given image name?

best wishes
Simon

This can be done by launching the Script Editor, and then selecting Image Events and clicking the Dictionary icon in the Library palette.
NNN637149307921824277
 
Posts: 1
Joined: Sat Jan 18, 2020 8:53 am


Return to Scripting



Who is online

Users browsing this forum: No registered users and 4 guests