DEVONthink selection bug with Yosemite JXA Javascript

Has anyone had luck with the Yosemite Javascript for Applications scripting interface to DEVONthink ?

I’ve successfully switched my scripting to Javascript with other applications, but I’m hitting an immediate problem with the DEVONthink Application.selection property, which:

  • is listed in the .sdef,
  • works with Applescript,
  • shows up in Javascript if we ask for Application.properties()

BUT provokes a non-terminating wait state (requiring Force Quit for Script Editor) if we ask for the value of the selection property.

(Note - to use JXA select Javascript at top left in Yosemite Script Editor)

FAILS (2.8.4):

function run() {
	var appDEV = Application("com.devon-technologies.thinkpro2"),
		varSeln = appDEV.selection;
	
	//return appDEV['selection'].length;
	//return appDEV.selection.length;

	return varSeln;
}

THO THIS WORKS (showing .selection as one of the Application properties … ):

function run() {
	var appDEV = Application("com.devon-technologies.thinkpro2"),
		varProps = appDEV.properties();
		
	return varProps;
}

Screen shot 2015-03-26 at 16.50.18.png

Similarly, Application.databases() works, but Application.windows() goes into a non-terminating spin, and Script Editor has to be forced to quit …

Screen shot 2015-03-26 at 19.01.45.png

Screen shot 2015-03-26 at 19.04.45.png

Over here I have the DEVONthink “Incoming Group” set to “Select Group” and so when I run the script the Group Selector opens. The script pauses until a group is chosen in that panel. However, nothing is written to that group.

I wondered what would happen if I use the DNtp ID in the script. The result is this hilarious error message:

EDIT: The wait for Group Selection occurs with both varieties of your script, Rob. The “hang” is actually DEVONthink waiting for input. When the input is given (i.e., group selected). I think if you modify the script to define the incoming group you might avoid the hang. Not sure of the proper syntax to do that.

Thanks, Korm – that’s interesting, and I think I would regard it as a bug. The parallel code in Applescript works without a wait …

Meanwhile I’m not sure whether these Console Log references to DevonThink and dictionaries (next to a slightly alarming message about the Script Editor 'burning CPU’) shed any light:

In the meanwhile, here is a summary of workarounds for getting the selection and the open windows (in the latter case, by avoiding the Standard Suite .windows collection, and using the DEVONthink Suite .thinkWindows and .viewerWindows collections instead.

function run() {
	var appDEV = Application("com.devon-technologies.thinkpro2");

	appDEV.activate();
	appDEV.includeStandardAdditions = true;

	var dctProps = appDEV.properties(),
		lstProps = [],
		lstThinkWins = [],
		lstViewWins = [],
		fnThinkWins = appDEV.thinkWindows,
		fnViewWins = appDEV.viewerWindows,
		lngThink = fnThinkWins.length,
		lngView = fnViewWins.length,
		// fnWins = appDEV.windows,  BUG !! this Standard Suite call → infinite loop
		i, j;

	for (var key in dctProps) {
		lstProps.push(key);
	}
	lstProps.sort();

	for (i = 0; i < lngThink; i++) {
		lstThinkWins.push(fnThinkWins[i]);
	}
	
	for (j = 0; j < lngView; j++) {
		lstViewWins.push(fnViewWins[i]);
	} 
	
	return {
		AvailableAppKeys : lstProps,
		ThinkWins : lstThinkWins,
		ViewerWins : lstViewWins,
		Selection : dctProps['selection']
	}
}

Over here the workaround still exhibits the bug of opening the group selector when the script executes. The new code also returns only one of the open databases then terminates.

I’m thinking DEVONthink and JXA are not ready to work together productively, perhaps. Would be useful to know if anyone from DEVONtech has managed to produce any scripts using JXA Javascript – something productive, more than a mere demo.

It’s curious – I haven’t seen this kind of difficulty with other scriptable applications.

(Thanks for checking that code, incidentally … )