Applescripting Keywords in Capture One

Applescripting Keywords in Capture One

In a previous article I wrote about using keywords to track and organise exported images. One of the things missing at the time was the ability to automatically apply the organising keywords to the variant being exported; the keywords had to be applied by hand. Doing things by hand in an automated process is generally to be avoided and it bugged me. 

Capture One 10 dropped a short while back. There was no AppleScripting of keywords but they had exposed metadata which, with a little thought and effort, can be hauled in as a passable substitute by using a metadata field to store comma separated keywords. Metadata can be used in search filters and smart collections just like keywords; all we need is some code to add and remove our keywords from the metadata string and we have something useful. 

Note that what we’re doing here is writing our own, custom keyword system. We’re not working with actual Capture One keywords, which would be prefereable but we still have a lot of usefulness. We can use it for smart collections and searches amongst other things, of which I’ll talk about below.

Choice of Metadata field.

You’re going to need, obviously, to pick a field that’s not being used. I picked the IPTC scene field, partly because I don’t use it but also because it’s recognised by Capture One as a comma separated list which makes it easier to see and edit in the metadata pane, if you need to.It’s found under “IPTC Image” in the Metadata panel.

Screen Shot 2016 12 20 at 08 00 09

There are other fields to choose from if this doesn’t suit. You can find a full list of fields with descriptions here.

We need three functions; one to add a keyword, one to remove and a third to toggle a keyword, adding it if it doesn’t exist, remove if it does. The functions tag, untag or toggle keywords on whatever variants are selected.

The Add Tag Function

The add tag function loops through selected variants, checking first to see if the tag already exists in the field, adding it if it doesn’t. You’ll notice that the tags in the image above are wrapped in ‘/’ characters. These are needed because Capture One’s  search criteria are relatively simple and can catch the wrong thing. Given, for example, two tags “Facebook” and “Facebook Page”, a filter search on just “Facebook” will find both. We work around this by topping and tailing keywords with forward slashes and searching on “/Facebook/”. It’s a little messy but it works.

on ApplyTagToSelectedVariants(theTag)
	
	tell application "Capture One 10"
		
		set vars to (selected variants)
		set theTag to "/" & theTag & "/"
		
		repeat with vv in vars
			
			set var to item 1 of vv
			set keys to image scenes of var
			
			if keys is equal to missing value then
				set keys to ""
			end if
			
			set keyList to text items of keys
			
			repeat with key in keyList
				set n to contents of key
				if contents of key is equal to theTag then
					return
				end if
			end repeat
			
			set image scenes of var to keys & "," & theTag
			
		end repeat
		
		display notification "Tag " & theTag & " has been applied to " & (count of vars) & " Selected variant(s)" with title "C1 Script"
		
	end tell
	
end ApplyTagToSelectedVariants
-- Test:
ApplyTagToSelectedVariants("tag1")   -- remove after testing
ApplyTagToSelectedVariants("tag2")   -- remove after testing
ApplyTagToSelectedVariants("tag3")   -- remove after testing

Going through the code:

  • Line 5 gets a list of selected variants.
  • Line 6 wraps the tag with forward slashes, as noted above.
  • In line 8 we loop through the variant list
  • Line 11 picks up the string from the image scenes metadata field
  • In lines 13-15 we set the string to an empty string if the field was empty
  • Line 17 turns the string into a list using AppleScript’s text items.
  • Lines 19 through 24 loop through the key list checking to see if the tag we’re applying already exists. If it does then the function returns without doing anything
  • Line 26 concatenates the incoming tag to the original field string and replaces it into the image scenes field.
  • And finally, a notification at line 30.
And three test lines. You should remove these once you’re sure everything’s working. Run the script in the Script Editor then check the IPTC Scenes field in the metadata panel you’ll see three new lines, each with /tag1/, /tag2/, or /tag3/.
 

The Untag Function

 Like the tagging function the untag function loops through  the selected variants and removes the given keyword from each one if it exists. The actual removal is done by a third function, deleteKey( Key, inString ) which uses the key being deleted to split the string, effectively removing it, putting the result back into a string.
 
Here’s the code. Place it in the same file as the previous tagging function. If you run it with the test lines in place and check you should see the tags previously added have been removed. Again, remove the three test lines once you’re done testing
on RemoveTagFromSelectedVariants(theTag)
	
	tell application "Capture One 10"
		
		set vars to (selected variants)
		set theTag to "/" & theTag & "/"
		
		repeat with vv in vars
			
			set var to item 1 of vv
			set keys to image scenes of var
			
			if keys is not equal to missing value then
				set newKeys to my deleteKey(theTag, keys)
				set image scenes of var to newKeys
			end if
			
		end repeat
		display notification "Tag " & theTag & " has been deleted from  " & (count of vars) & " Selected variant(s)" with title "C1 Script"
		
	end tell
end RemoveTagFromSelectedVariants

on deleteKey(key, inString)
	
	set savedDelim to AppleScript's text item delimiters -- save the current delimiter setting
	
	set AppleScript's text item delimiters to the key
	set the textitemlist to every text item of the inString
	set AppleScript's text item delimiters to ""
	set the newitemname to the textitemlist as string
	
	set AppleScript's text item delimiters to savedDelim -- set the delimiters back
	
	return newitemname
	
end deleteKey

RemoveTagFromSelectedVariants("tag1")  -- remove after testing
RemoveTagFromSelectedVariants("tag2")  -- remove after testing
RemoveTagFromSelectedVariants("tag3")  -- remove after testing
The work is done at lines 13 to 16 where the script checks to make sure the metadata field isn’t empty and calls deleteKey() (lines 24-37) if it’s not. deleteKey() returns a string with the key removed which is then put back into the image scenes field.
 

The Toggle Function

The toggle function, again loops through the selected variants, checking in line 18 to see if the key exists,  removing it if it does, adding it if it doesn’t. 
on ToggleTagOnSelectedVariants(theTag)
	
	tell application "Capture One 10"
		
		set vars to (selected variants)
		set theTag to "/" & theTag & "/"
		
		repeat with vv in vars
			
			set var to item 1 of vv
			set keys to image scenes of var
			
			if keys is equal to missing value then
				set keys to ""
			end if
			
			if theTag is in keys then
				tell script "COLib"
					set newKeys to my deleteKey(theTag, keys)
				end tell
				set image scenes of var to newKeys
				
			else
				set image scenes of var to keys & "," & theTag
			end if
			
		end repeat
		display notification "Tag " & theTag & " has been Toggled on " & (count of vars) & " Selected variant(s)" with title "C1 Script"
		
	end tell -- CaptureOne
	
end ToggleTagOnSelectedVariants
 

Using the functions

The best way to use these functions is to place them in a Script Library where they can be called from any script, saving you from keeping copies of the functions in each script they’re used in. Drop the script file you’ve just created into either /Library/Script Libraries or /Users/<your home folder>/Library/Script Libraries. The functions can now be called using the form:
	tell script "COLib"
		ApplyTagToSelectedVariants("Facebook")
	end tell

Where “COLib” is the name of the script file you’ve just saved to the Library, without the .scpt extension.

Example: Quick Collections

Quick collections are a Lightroom thing. You hit a key and all selected images are dropped into a target folder. Hit the key again and it’s removed. We can do this now in Capture One with a smart collection and the ToggleTagOnSlectedVariants() function. Create a smart collection, call it, say, “Quick Collection”. Give it a search criteria, “IPTC Scene” “equals” “/Quick Collection/”. “IPTC Scene” is found under “IPTC Image” in the criteria dropdown menu. Be mindful of the forward slashes in the search string.

Screen Shot 2016 12 20 at 08 17 59

Now run the following in the Script Editor with some variants selected and they’ll appear magically in your collection. Run it again and they’ll be removed. It’s a toggle action:

	tell script "COLib"
		ToggleTagOnSelectedVariants("Quick Collection")
	end tell

You can create a script, put it in the Capture One scripts folder, if you like, and it will appear in the Scripts menu in CO but a better way is to create a hotkey that runs the script. There’s a number of good apps that let you do this really easily – Alfred, Keyboard Maestro, Launcher – but if you prefer a free solution, use Automator to create a service and assign it a hotkey. A description of how to do this can be found here.

Example: Automatic tagging on export

At the start of this blog post I mentioned this article which talked about using keywords to keep track of exports. The missing piece of magic there is the bit that automatically adds keywords to variants when they’re exported. We still can’t add keywords but we can adapt our substitute tagging method instead by creating an export recipe for each tracked export destination and adding a simple script, saved from Script Editor as an app, using the Open With dropdown at the bottom of the Basic tab in the export. So, for example, if you’re tracking Instagram exports, you’d create a suitable Instagram export recipe and add the following script in the Open With dropdown:

tell script "COLib"
	ApplyTagToSelectedVariants("Instagram")
end tell 

For a fuller description of how to use AppleScript in export recipes, see my previous blog article Capture One and the Recipe Open With 

If you’re already using some variant of the export tracking method, you’ll need to adjust your smart collections to look at the IPTC Scene field as well as keywords.

Caveats

Be aware that this method of tagging on export isn’t entirely foolproof. There’s an assumption that the variants selected for export will still be selected by the time Capture One gets round to running the script, after the export. Exports are run as a background process and it’s possible to change the selection before Capture One gets to the script. So you should wait until all exports are complete before making selection changes.

Secondly, the script is called and acts on all selected variants for every variant exported. Which isn’t a bad thing in itself but it’s kludgy and worth mentioning.  

 

Pin It on Pinterest

Share This