Using Applesript to select an image
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
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.
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
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 variantbut 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.
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
0
-
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:
<?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.applescript0 -
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
Simon0 -
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. 0 -
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.0 -
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. 0 -
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 saywhen 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.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.
Simon0 -
I might be missing something but I think this will also work.
Not really tested but should work.
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 tell0 -
No need for a special album for this.
The Applescript syntax for selecting works like this
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.0 -
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.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
Simon0 -
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
Simon0 -
[quote="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.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
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
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.0 -
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
Simon0 -
[quote="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.The problem is that the Applescript parser doesn't group words in the way we expect
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 fix is to add brackets:
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 brackets0 -
[quote="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 isSelect 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.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.0 -
Copy and paste from those, or type the AppleScript source given here, into the editor window. 0 -
Hi there,
So I have been working on something similar, so I adjusted my script to take variants, either by name or a selection from a collection view, and move them to a new collection named by you.
Let me know if it works for you
I wrote it on Mac OSX 10146, Capture One 20, build 13.0.0.186
It would be nice to see how the script behaves on other OSes
Thanks
-s
EDIT:
I have added some error trapping for user interface logic and placed the script below!
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
(*
BY: Stewart Ghazvini
DATE: 05/19/2020
steuieg@gmail.com
*)
(*
Can get selected of an image in a collection; set myVChoicePick to selected of myVChoice
But can not set an item as the selected; set myChoice to selected ??????
*)
################################################################################################
# >>FUNCTIONS START<<
################################################################################################
on diag_askQuestion(aQuestion, anAns)
--set myQ to "Question" & return & return & ""Quesiton"?"
--set myA to "Answer"
--diag_askQuestion(myQ, myA)
--set myResponse to the result
tell application "SystemUIServer"
activate
set myResponse to the text returned of (display dialog aQuestion default answer anAns buttons {"Cancel", "OK"} default button "OK")
return myResponse
end tell
end diag_askQuestion
--
on diag_getListChoice(allChoices, aMsg, aTitle, multipleTrue)
--set myMSG to "Message... Thanks!"
--set myTitle to "TITLE"
--set myList to {"ITEM1", "ITEM2", "ITEM3"}
--set multipleTrue to true
--diag_getListChoice(myList, myMSG, myTitle, multipleTrue) of me
--set myChoice to result
tell application "SystemUIServer"
-- activate
if multipleTrue is true then
set myList to choose from list allChoices with title aTitle with prompt aMsg with multiple selections allowed without empty selection allowed
else
set myList to choose from list allChoices with title aTitle with prompt aMsg without multiple selections allowed and empty selection allowed
end if
if myList is false then error number -128
return myList
end tell
end diag_getListChoice
--
on diag_showMsg(aMsg, giveUp)
--set giveUp to 5
--set myMSG to "All done!"
--diag_showMsg(myMSG, giveUp)
if aMsg is "" then
set aMsg to "Hmmmm... no message was sent into the function!"
end if
if giveUp is "" then
set giveUp to 5
end if
tell application "SystemUIServer"
activate
display dialog aMsg buttons {"OK"} default button "OK" giving up after giveUp
end tell
end diag_showMsg
--
on diag_showNotify(aMsg, aTitle, aSubTitle)
--set myMsg to "Hello"
--set myTitle to "Some title"
--set mySubTitle to "Some subtitle"
--diag_showNotify(myMsg, myTitle, mySubTitle)
display notification aMsg with title aTitle subtitle aSubTitle
end diag_showNotify
--
################################################################################################
# >>FUNCTIONS END<<
################################################################################################
################################################################################################
# >>MAIN START<<
################################################################################################
on run
set myMSG to "Choose how to select your image/s!"
set myTitle to "IMAGE CHOICE"
set myList to {"By Name", "From Collection"}
set multipleTrue to false
diag_getListChoice(myList, myMSG, myTitle, multipleTrue) of me
set findMethod to item 1 of result
set myQ to "Collection Name" & return & return & ""Enter the name of the collection please"!"
set myA to "Collection_Name"
diag_askQuestion(myQ, myA)
set newCollectionName to the result
set newName to false
repeat until newName is true
tell application "Capture One 20"
tell current document
try
set newCollection to make new collection with properties {kind:album, name:newCollectionName}
set newName to true
on error
set myMSG to "That name is taken already! How shall I proceed?"
set myTitle to "COLLECTION NAME TAKEN"
set myList to {"Use It", "Make new collection", "Stop"}
set multipleTrue to false
diag_getListChoice(myList, myMSG, myTitle, multipleTrue) of me
set collectionKeep to item 1 of result
if collectionKeep is "Make new collection" then
set myQ to "NAME OF NEW COLLECTION" & return & return & ""Exists"!"
set myA to "New_Collection"
diag_askQuestion(myQ, myA) of me
set newCollectionName to the result
else if collectionKeep is "Use It" then
set newCollection to item 1 of (every collection whose name is newCollectionName)
set newName to true
else
error number -128
end if
end try
end tell
end tell
end repeat
if findMethod is "By Name" then
set myQ to "IMAGE NAME" & return & return & ""What is the name of image" ?"
set myA to "Image_Name" -- 2020_0514-Baronsmede-Timelapse-Test 67
diag_askQuestion(myQ, myA)
set aName to the result
tell application "Capture One 20"
tell current document
set thisDocName to name
try
set variantsToMove to every variant whose name is aName
if variantsToMove is {} then
set giveUp to 5
set myMSG to "Sorry, the name "" & aName & "" was not found in "All Images" Collection of "" & thisDocName & """
diag_showMsg(myMSG, giveUp) of me
error number -128
end if
on error errMsg number errNum
set giveUp to 30
set myMSG to "Sorry, there was an error with the following code: " & """ & errMsg & "" number: "" & errNum & """
diag_showMsg(myMSG, giveUp) of me
error number -128
end try
end tell
end tell
else
tell application "Capture One 20"
tell current document
set currCollectionName to name of current collection
if newCollectionName is currCollectionName then
set myMSG to "The requested colleciton " & """ & newCollectionName & """ & return & return & "is the current collection, which means you are trying to move variants from this collection back into itself..." & return & return & "Sorry, does not make sense!" & return & return & "Either use another collection to choose variants, or run script again and use a different name! Thanks"
set giveUp to 120
diag_showMsg(myMSG, giveUp) of me
error number -128
end if
end tell
set thisCollectionImages to the name of every variant
if thisCollectionImages is {} then
set myMSG to "The current colleciton " & """ & currCollectionName & """ & return & return & "is empty..." & return & return & "Sorry, does not make sense!" & return & return & "Choose a collection that has some variants inside it and rerun script! Thanks"
set giveUp to 120
diag_showMsg(myMSG, giveUp) of me
error number -128
end if
set myMSG to "Please choose the images you would like to move from the list... Thanks!"
set myTitle to "IMAGES TO MOVE TO "" & newCollectionName & "" COLLECTION"
set myList to thisCollectionImages
set multipleTrue to true
diag_getListChoice(myList, myMSG, myTitle, multipleTrue) of me
set varNamesList to result
set variantsToMove to {}
repeat with i from 1 to count of varNamesList
set thisVariantName to item i of varNamesList
set the end of variantsToMove to variant thisVariantName
end repeat
end tell
end if
tell application "Capture One 20"
tell current document
try
set newCollection to make new collection with properties {kind:album, name:newCollectionName}
on error
set newCollection to item 1 of (every collection whose name is newCollectionName)
end try
add inside newCollection variants variantsToMove
end tell
end tell
set myMSG to "Script has completed!" & return & return & "There were "" & (count of varNamesList) & "" variants moved to collection "" & newCollectionName & """
set giveUp to 30
diag_showMsg(myMSG, giveUp)
end run
################################################################################################
# >>MAIN END<<
################################################################################################0
投稿コメントは受け付けていません。
コメント
16件のコメント