An Observation On Mark's containerOf() handler

In writing AppleScripts for Capture One, a frequent barrier is trying to discover which collection the current collection (or some other collection) is in.
Capture One’s object hierarchy is:

  • Documents contain collections.
  • Collections may contain other collections.
  • Colllections may contain Images and Image variants.

I’ve written a fairly lengthy handler that discoveres the hierarchy by requesting the property “||” of a collection (a property it couldn’t possibly have), and then parsing the error message. I borrowed this idea from one of bmose’s scripts on MacScripter.

So I was quite interested when I saw Mark’s one line containerOf() handler.

Then I had a terrible time using the result of this handler, I couldn’t seem to use it for anything with getting an error.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on containerOf(theObject)
	--tell application theAppName to  return «class from» of (theObject as record)
	return from of (theObject as record)
end containerOf
tell application "Capture One 12"
	tell current document to set R1 to my containerOf(current collection)
	log R1
	get name of R1
end tell

This code results in the following event log

[Capture One]
get collection id "8" of collection id "7" of collection id "6" of collection id "96" of document "TestStacks"
--> collection id "8" of collection id "7" of collection id "6" of collection id "96" of document "TestStacks"
(*collection id 7 of collection id 6 of collection id 96 of document TestStacks*)
[Script Debugger]
get name of «class COcl» id "7" of «class COcl» id "6" of «class COcl» id "96" of document "TestStacks"
Error: no such object (e.g. specifier asked for the 3rd, but there are only 2) (errAENoSuchObject:-1728)

and the Error Message

Can’t get collection id “96” of document “TestStacks”

Eventually I realised that AppleScript was asking Script Debugger about an object that actually belonged to Capture One. It didn’t actually know which application R1 belonged to and so it asked the parent application.

After some more head scratching I came up with the following workaround (that works). Unfortunately it is less universal and not as clean.

on containerOf(theObject, theAppName)
	--tell application theAppName to  return «class from» of (theObject as record)
	tell application theAppName to return «class from» of (theObject as record)
end containerOf
set theAppName to "Capture One 12"
tell application "Capture One 12"
	tell current document to set R1 to my containerOf(current collection, theAppName)
	get name of R1
end tell

(the reason for the comment line is that before every recompile, the line below has to be refreshed, so that from is replaced with «class from». Unless you have set Script Debugger to use chevron format - but then other things become a problem.