How is the selection object set?

I am having problems setting the ‘selection’ object.

The syntax appears to have the general form of:


set the selection of think window 1 to {[content record], [content record] ....}

I can’t set the selection reliably. The following short demonstration code gets the content record of the first selected item, clears the selection and then sets the selection with the same content record.

tell application id "DNtp"
	activate
	try
		set theWindow to think window 1
		-- get the first selected item
		set firstItem to item 1 of theWindow
		-- successfully clear the selection item record
		set selection of theWindow to {}
		-- try to set the selection to a selection 
		set selection of theWindow to {firstItem}
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

The script fails at the line ```

set selection of theWindow to {firstItem}


Does anyone have any ideas of what I am doing wrong or have another method to set the selection?

Frederiko

(2.8.8 appears to have changed the behaviour of 'import' verb. Import now immediately selects the document which is being imported. This has broken a number of my scripts.)

Looking at this in ScriptDebugger I see that


set firstItem to item 1 of theWindow

sets firstItem to the first tab of theWindow, not to a record. I believe you need the first part of your code to do


set firstItem to the selection of theWindow

or


set firstItem to the first item of the selection of theWindow

Not sure about the rest, but I’ll look in my library of scripts collected here and see what I find.

Thanks korm for looking at the code. That was a stupid error I should have picked up but its not the source of the applescript event handle failure unfortunately.

This is the corrected code which returns the same error

tell application id "DNtp"
	activate
	try
		set theWindow to think window 1
		-- get the first selected item
		set theSelection to the selection of theWindow
		set firstItem to first item of theSelection
		-- successfully clear the selection item record
		set selection of theWindow to {}
		-- try to set the selection to the original selected document 
		set selection of theWindow to {firstItem}
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
		
	end try
end tell

Frederiko

Except for the viewer window entry, selection is marked as R/O in the AS dictionary (and I think the viewer window property not being marked is a documentation oversight).

Also, just a question and an opinion… why do you want to make selections? Not only are selections generally problematic to make in most applications I’ve scripted in the last 15+ years, there is rarely a cause for it, other than it looks familiar. Selections are what we - as human operators - do. Computers don’t need to make selections to operate on data. Just tell them what / where and they’ll sort it out. 8) :smiley:

I was wondering about window > selection being marked r/w, and I’ve been unable to locate any example snippets that write window > selection. The selection is stored as a specific {content ID, window ID} pair and being able to write content ID to a window would likely cause violations and possible corruption. Just saying.