Capture One shortcuts for the terminally curious: part 3 (Final)
We have arrived at the final part of this series: the script!
This is a simplified extract of the full installer application I made, as it is simply far too big to break down into a single tutorial. Instead, I have made a fully functional version of the useful part, which does most of the heavy lifting and annoying bit – adding the shortcuts to a set of installed scripts.
Prep:
- Quit C1.
- Make a folder in ~/Library/Scripts/Capture One/Scripts/
- (For my example today: ~/Library/Scripts/Capture One/Scripts/Apply Latitude)
- Copy your scripts into this folder – for this, I am using scripts from another project post to come in the new year – yours will be whatever you make.
My scripts are called:
- “Apply SB-01.scpt”
- “Apply SB-02.scpt”
- “Apply SB-03.scpt”
- “Apply SB-04.scpt”
- “Apply SB-05.scpt”
- “Apply SB-06.scpt”
- “Apply SB-07.scpt”
- “Apply SB-08.scpt”
- “Remove Style.scpt”
You need to decide what the shortcuts will be, and you need as many shortcuts as there are scripts (9).
Next we need to map these together as a “value pair” for the script.
As a reminder on the first post in the series, coding shortcuts requires a bit of symbology to equate user facing stuff: Modifier keys are represented as: @ for Command, $ for Shift, ~ for ⌥ and ^ for ⌃.
So the string “@$~^t” represents ⌘+⇧+⌥+⌃+T
Im going to set these up as the full ⌘+⇧+⌥+⌃ + [key]. This is obviously impractical for a users, but it serves as a technical demonstration (and actually doesn’t matter in the other project). You might decide to use something else:
- “@$~^A”
- “@$~^B”
- “@$~^C”
- “@$~^D”
- “@$~^E”
- “@$~^F”
- “@$~^G”
- “@$~^H”
- “@$~^I”
Now you have the list of file names, and the list of shortcuts, we have to combine this into an object the script will parse into the shell command.
The simplest way (IMO) to do this (and there are many methods) is to make a list. As we have to make many value pairs, we could go one step further and make a list containing the smaller lists.
A list in apple script is defined with curly brackets:
1 2 |
set myshortcut set to {} |
To define the first value pair, combine the script name and the shortcut (“” makes them text values) and then split with a comma.
1 2 |
set shortcutSet to {"Apply SB-01", "@$~^A"} |
We could add more pairs to a single list, but this would be suboptimal. Instead we create a list of lists (a list containing several smaller lists) by puting the another pair of brackets around the first. This gives you a main list, containing each of the value pairs as addressable items within it.
1 2 |
set shortcutSet to {{"Apply SB-01", "@$~^A"},{"Apply SB-02", "@$~^B"}} |
When completed, it should look like this:
1 2 |
set shortcutSet to {{"Apply SB-01", "@$~^A"},{"Apply SB-02", "@$~^B"},{"Apply SB-03", "@$~^C"},{"Apply SB-04", "@$~^D"},{"Apply SB-05", "@$~^E"},{"Apply SB-06", "@$~^F"},{"Apply SB-07", "@$~^G"},{"Apply SB-08", "@$~^H"},{"Remove Style", "@$~^I"}} |
Which is basically a list containing a number of text value pairs, each as their own little lists of 2 items.
TIP: You can line break long lines of code by placing the cursor and using ⌥+[return] – this makes reading and editing long lines easier.
1 2 3 4 5 6 7 8 9 10 11 |
set shortcutSet to {¬ {"Apply SB-01", "@$~^A"}, ¬ {"Apply SB-02", "@$~^B"}, ¬ {"Apply SB-03", "@$~^C"}, ¬ {"Apply SB-04", "@$~^D"}, ¬ {"Apply SB-05", "@$~^E"}, ¬ {"Apply SB-06", "@$~^F"}, ¬ {"Apply SB-07", "@$~^G"}, ¬ {"Apply SB-08", "@$~^H"}, ¬ {"Remove Style", "@$~^I"}} |
When testing, you can now ask for a value pair from the list easily:
1 2 |
return item 3 of shorcutSet |
Which returns the 3rd item – {"Apply SB-03", "@$~^C"}
The script also needs to know the name of the folder you made your scripts in (seeing as we are using the full path approach to the shortcuts).
Create a new variable and assign the name:
1 2 |
set folderName to "Apply Latitude" |
Next we have to make some logic that will chop the value pairs for each row, and then run the shell command with those bits. A repeat loop can be used to cycle through the list, and extract the name and the shortcut from each pair by creating new varibles from the indexed position. Comments have been added to the code below explaingin each line (those lines starting –)
There are again, a few ways to do this. I used this approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
-- Loop as many times as there are items within shortcut set (in our case, 9). -- idx is "1" for the first loop, and increments by 1 for every loop. -- We can use this as a position counter for our list… repeat with idx from 1 to count of items in shortcutSet -- On the first loop, item idx is the first value pair in our list. -- We then assign item 1 from this value pair - "Apply SB-01" - to aScriptMenuItem set aScriptMenuItem to item 1 of item idx of shortcutSet -- On the second loop, item idx is still the first value pair in our list. -- This time we assign item 2 from this value pair - "Apply SB-01" - to aScriptMenuItem set aShortcut to item 2 of item idx of shortcutSet -- aScriptMenuItem, aShortcut and folderName are now variables we need to chop in to the shell script -- this is done using "do shell script" with the shell script between "" do shell script --"figure this out last" -- once the shell is executed, the script will now loop the repeat block again to the count of items in shortcut set (9 times total). -- In the next loop, idx increments to 2 and the second value pairs are processed, and so on. end repeat |
The last part is the shell command, and is fairly advanced if you have no experince in scripting shell in AppleScript, requiring skills like string concatination, charcter escaping… and massive patience. I do conceed, I should maybe have started this this blog with a gentler slope, but what can you do. Stuff tumbles out when it does.
The shell command from part 2 was:
1 |
defaults write com.captureone.captureone13 NSUserKeyEquivalents -dict-add "\033Scripts\033My Folder Name\033My Script" "\UF70D" |
You can type this into terminal and it will work – but it wont if run from within AppleScript.
Shell commands from an AppleScript are invoked from the “Do shell script” command – this recieves a single text paramater between “”. Because our command has several ” between the first and last ” the script interprets the code inncorrectly. To stop this from happening we need to escape certain characters so the script ignores them, but will still pass them to the command line. To do this, charcters we want, but want AppleScript to interpret as just text, are proceeded with “\”.
We also – and at the same time (bloody hell) – have to inject our variables into the text. This requires replaceing stanadrd text with the varibles and then concatenation of the various elements together with “&”.
So with all 3 variables, escaping and the new command, our script looks like this:
1 2 |
do shell script "defaults write com.captureone.captureone13 NSUserKeyEquivalents -dict-add \"\033Scripts\033" & folderName & "\033" & aScriptMenuItem & "\" \"" & aShortcut & "\"" |
Our script is now complete. Running the script will add the shortcuts to (in this case) Capture One 20.
The script from this post is below in full, if you want to copy paste this to your own IDE and start breaking stuff, go nuts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions -- © shootmachine.co 2020 -- A demo script for installing shortcuts to Capture One 20 set shortcutSet to {¬ {"Apply SB-01", "@$~^A"}, ¬ {"Apply SB-02", "@$~^B"}, ¬ {"Apply SB-03", "@$~^C"}, ¬ {"Apply SB-04", "@$~^D"}, ¬ {"Apply SB-05", "@$~^E"}, ¬ {"Apply SB-06", "@$~^F"}, ¬ {"Apply SB-07", "@$~^G"}, ¬ {"Apply SB-08", "@$~^H"}, ¬ {"Remove Style", "@$~^I"}} set folderName to "Apply Latitude" repeat with idx from 1 to count of items in shortcutSet set aScriptMenuItem to item 1 of item idx of shortcutSet set aShortcut to item 2 of item idx of shortcutSet do shell script "defaults write com.captureone.captureone13 NSUserKeyEquivalents -dict-add \"\\033Scripts\\033" & folderName & "\\033" & aScriptMenuItem & "\" \"" & aShortcut & "\"" end repeat |
If you screw up your C1 plist you can reset the custom shortcuts entry by using the following terminal command:
1 |
defaults delete com.captureone.captureone13 NSUserKeyEquivalents |
I hope this has been an interesting series – we have tonnes lined up, but I wonder what should be next?
Leave a comment after the break.
#makecaptureoneproagain