Get Started With AppleScript – Scripting Selections
An intro series to scripting Capture One, with Ben Liddle
Splitting hairs: Variants v. Images
The thumbnails in Capture One’s browser pane are technically variants. A variant is a unique set of rendering instructions for a given source raw (or TIF, JPG, etc) image file. These instructions are lightweight (few hundred Kb at most), so you can think of these as virtual copies of your image files. Every image file you view in Capture One has at least one variant; unless you objectively create a new variant, there’ll be a 1:1 ratio of variants and images in your browser.
Setting up the Handler
Below is the code to set up the AS hander. Create a new script file and save this to the top of the file. Handlers are basically bits of code that you can call from anywhere else in the code (once you have already defined it, thus putting this at the top). They start with the on keyword, and are called with my (when they’re located in the same .scpt file as the code that’s calling it, but you can ignore this for now).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions on select_vars(color_tag as string) set color_tag_list to {"red", "orange", "yellow", "green", "blue", "pink", "purple"} repeat with i from 1 to length of color_tag_list if item i of color_tag_list = color_tag then set color_tag_number to i end if end repeat tell application "Capture One 20.0.4" tell current document set sel_vars to variants whose color tag is color_tag_number select variants sel_vars end tell end tell end select_vars |
Let’s break it down:
1 |
on select_vars(color_tag as string) |
Here, I’m declaring that we’re creating a handler arbitrarily called select_vars. The bit in parentheses is a parameter that I’m declaring. Basically, when we call this handler later on, we’ll be passing a variable as well. The ‘as string’ tells the script that whatever I pass to select_vars later on, it must be a string; eg if later on I pass it an integer or list, we’ll get an error.
1 |
set color_tag_list to {"red", "orange", "yellow", "green", "blue", "pink", "purple"} |
The color tags in Capture One are scripted to numeric values between 1 and 7. Here, I’m building a list of readable color tag names in their order: a red tag is actually a 1, a green is actually a 4, etc.
1 |
repeat with i from 1 to length of color_tag_list |
Repeat with a variable i, from 1 to the length of (ie count) of the color tag list. On each loop through (iteration), i is the index (position) of the item in the list.
1 2 3 4 |
if item i of color_tag_list = color_tag then set color_tag_number to i end if end repeat |
Here we’re checking if the item in position i is the same as the text variable I’m setting. If so, create a new variable called color_tag_number and set its value to the current iteration’s value of i.
1 |
set sel_vars to variants whose color tag is color_tag_number |
Collect all variant objects of the current collection whose property color tag’s value is equal to the value of color_tag_number . Create a new variable, sel_vars, to store references to them. Since we used the plural of variant, our variable sel_vars is a list.
1 |
select variants sel_vars |
The current document is being told to select the variant objects referenced in sel_vars. This is actually an additive select- if we had four other variants selected, we’d have the count of green-tagged images plus those four selected. This can be prevented by throwing in a deselect command before the new select command:
1 2 3 4 5 6 7 |
tell application "Capture One 20.0.4" tell current document deselect variants (every variant whose selected is true) set sel_vars to variants whose color tag is color_tag_number select variants sel_vars end tell end tell |
With this handler set up, we can call it wherever we want in the script without repeating the code block:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions on select_vars(color_tag as string) set color_tag_list to {"red", "orange", "yellow", "green", "blue", "pink", "purple"} repeat with i from 1 to length of color_tag_list if item i of color_tag_list = color_tag then set color_tag_number to i end if end repeat tell application "Capture One 20.0.4" tell current document deselect variants (every variant whose selected is true) set sel_vars to variants whose color tag is color_tag_number select variants sel_vars end tell end tell end select_vars display dialog "Show me the green tags." my select_vars("green") display dialog "Show me the red tags." my select_vars("red") set new_color to choose from list {"blue", "orange", "green"} my select_vars(new_color) |
Et voila. Variants selected.
Ben Liddle is a data manager in Colorado who automates the shit out of the first half of the work/life balance so the second half can be more fun.