ability set selection of current window?

Hello all,

As you can probably tell, I’ve been doing a little scripting lately. But I’m having trouble remembering a few of the more nuanced issues with scripting. The big one I have been wondering about is whether it is possible to preserve or set the current selection in dtpro scripting. I seem to vaguely remember this was once possible, but I can’t seem to get it to work. All I want is a little script that will create a new link record in the window where I’m currently working. But I want the script to leave me on the record I am on. Without any error-checking or anything, here’s what I’m trying to do…

tell application "DEVONthink Pro"
	set the_Window to window 1
		set content_record to content record of window 1
		set insert_location to first parent of content record
		set the_selection to selection
	end try
	set the_record to create record with {type:link, URL:"http://www.google.com", name:"new search"} in insert_location
	set selection of window 1 to the_selection
end tell

shouldn’t this work? Everything I try seems to not work. I guess maybe what has happened is that selection has become read-only… Or am I doing something really dumb… I sure would like to be able to restore the selection, or set it to something new…

I ask this without of course wanting to divert any attention to 2.0


Selection is read-only for the application. Selection, according to DTP’s AppleScript dictionary, is not marked as read-only for the viewer windows.

I know DTP uses a different way of implementing AppleScript than I did making apps, but I figure that the dictionary is still derived directly from the source documents and not maintained separately.


  1. The internal subroutine for setting the selection could have been disabled or might have a bug.

  2. The application definition of “selection” might be overriding any attempt to modify the viewer window “selection.”

  3. This could be a mistake in the AppleScript dictionary.

  4. Something else might be going wrong that I can’t even comprehend.

I’m not a god of AppleScript or anything, and of course I have zero knowledge of DTP internals, but it seems very peculiar to me that an app would be able to handle something like selecting specific elements when told to by AppleScript. I’m not saying it’s not possible or anything, but it seems like a lot of work when the items could simply be passed to another operation in a list like is normally done.

I just don’t know.

There’s always open window for, but I know that doesn’t really solve your problem.

Sorry I couldn’t help more.


I tend to agree with your analysis and/or confusion. I saw the scripting dictionary has it as read/write, and yet I’m not having any success. I also admit to not understanding the inheritance relation between a window, viewer window and a devon window. I’ve tried setting selection for every kind of window and passing the select as a list or not as a list, but no go.

I vaguely suspect this stopped working in devonthink sometime around when we obtained the three-column view. I guess there is nothing necessarily that prevents the 3-column view from setting a selection to a single record, but if there’s different parents leading to that child, it would be confusing. So I guess the fact that there is not a way to manipulate the two panes of the window is part of the issue. e.g.

set left pane of window 1 to {x};
set top pane of window 1 to {y}.

Ideally, I would like to do that. Selecting the item blindly would be option 2. Third best would be able to “remember” a selection and to then restore it.

But maybe the easiest request for the hard-working Christian would simply be if there were an option to tell the “create record” verb NOT to select the new record in the database. Though not as nice as being able to manipulate the selection, it would help a lot. I have a bunch of little script where I want to do something in situ with the current group, but don’t want to change the current document.

Let me give an example: I’m trying to write a script that I can invoke while writing to tell dtpro to put a wikipedia or britannica link to the currently highlighted word. I don’t want to however leave the editing I’m in, but just have that link there for when I’m ready to come back to it. Seems to me that a lot of people would like this kind of thing…a sort of assisted lookup while writing.
thanks for listening, and for your ideas!

I absolutely agree.

I would also like a way to set the viewed document of the specified window to be a passed argument – which wouldn’t quite solve the original problem, since it’d just show the new document and then go back to the previous one – but it’d be nice to have.

It’s only possible to set the selection of viewer windows. But DEVONthink Pro queues a lot of stuff internally to improve the AppleScript performance and that’s probably causing the issue. However, v2 will fix this.

Christian, thank you for listening and for your thoughts …I look forward to DTpro 2 and to be be able to restore the selection!

I should mention that I found a bit of a workaround in the meantime, which is to invoke the progress indicator. Your suggesting about queueing led me to experiment with what might change that queue relation. This script still only sort of works, in that it loses focus on the bottom window in a three-pane setup. But it does at least keep the focus on the left pane. Or at least that’s my experience right now.

The script below detects the current text selection, and then opens up “research” browser windows in the background. It’s handy to look a few things up while writing…if anybody thinks it useful, I can post it to a separate forum post.

For the sake of documenting the issue though, let me make a note here…As you can see there are some comments down toward the end of the script that tried to restore the selection…none of those work. I’d like to add here again that the ability to create a record without changing the selection might be the quickest fix to all these issues. But it is probably worth mentioning that given the way that the “get selected text” is set up, it doesn’t seem like it will be able to be restored without changing the scripting vocabulary. Ideally, “get text selection” would give a list, something like “characters 23 through 50 of text of thinkwindow 1”, but right now it gives just text. I assume that we would need a “get text selection range” command or something to do a proper restore on that. Seems to me that that is an an analog to the need for a “set selection of left/top pane” commands… Sorry to add to your list, Christian, but dt is so amazingly scriptable that these little things can get you down… much appreciation for all your work.

Okay, enough of the tedious debugging kvetching, here’s the promised Create “research browsers” from current text selection script:

---Create Search Browsers from Selected Text
----this script detects the selected text of the current document and creates three records in the same group as the current document
----these records are searches at google, wikipedia, and britannica.  
----version 1.0
--- This version is a bit hack-ish. 
----the script has to do a little trickery to try to get devonthink pro to remember your selection.  
----It basically opens up a new database window while it creates the records and then closes  that window.  
----you might need to change the timing of the two delay statements if your machine is too fast

	set the_clipboard to ((the clipboard as text))
on error
	set the_clipboard to ""
end try
log the_clipboard

tell application "DEVONthink Pro"
		if not (exists (think window 1)) then error "No window is open."
	set the_Window to viewer window 1
	--get essential devonthink information
	--first use a list to avoid the dreaded " variable is not defined" error
	set devon_info to {selection of the_Window, incoming group, selected text of the_Window, selected text of think window 1, content record of the_Window}
	log devon_info
	repeat with this_item in devon_info
		log class of this_item
	end repeat
	if class of item 3 of devon_info is not application then --tests if we have a text selection
		set the_text to item 3 of devon_info
	else if class of item 4 of devon_info is not application then
		set the_text to item 4 of devon_info
		set the_text to text returned of (display dialog "Please enter the text you would like to search for" default answer the_clipboard)
	end if
	log "selected text is " & the_text
	set the_think_window to get think window 1
	set the_root to root of database 1
	if class of item 2 of devon_info is not application then
		set insert_location to incoming group of database 1
		log "setting to incoming group"
	else if ((class of item 1 of devon_info) as string) is "list" then --do we have  a record selected ?
			set insert_location to first parent of content record
			set the_selection to selection
		on error
			set insert_location to the_root
		end try
	end if
	show progress indicator "Creating research browsers...." steps 5
	(*   (invoking the progress indicator seems to do the same thing)
	----starting part of selection workaround
	set temp_window to open window for record the_root
	set visible of temp_window to false
	delay 4
	log the_text
	set window_count to count of windows
	-------browser creation 1 : make as many of the following browser creation blocks as you wish------ 
	set the_record to create record with {type:link, URL:("http://www.google.com/search?q=" & the_text) as string, name:(the_text & ": google " as string)} in insert_location
	set window_count to window_count + 1
	set window_1 to open window for record the_record
	set index of window_1 to window_count
	step progress indicator
	-------browser creation 2 : make as many of the following browser creation blocks as you wish------ 
	set the_record to create record with {type:link, URL:(("http://www.wikipedia.org/wiki/" & the_text) as string), name:the_text & ": wikipedia: "} in insert_location
	set window_count to window_count + 1
	set window_1 to open window for record the_record
	set index of window_1 to window_count
	step progress indicator
	-------browser creation 3 : make as many of the following browser creation blocks as you wish------ 
	set the_record to create record with {type:link, URL:(("http://search.eb.com/search?query=" & the_text) as string), name:the_text & ": britannica "} in insert_location
	set window_count to window_count + 1
	set window_1 to open window for record the_record
	set index of window_1 to window_count
	step progress indicator
	---ending part of selection workaround  (invoking the progress indicator seems to do the same thing)
	(*delay 2
	close temp_window  *)
	delay 1
	hide progress indicator
	-----reset selection graveyard-----
	---the following  bits of code don't seem to work. 
	---This one seems to be a simple bug: 
	if item 1 of devon_info is not application then
		set selection of the_Window to the_selection
	end if
	set index of the_Window to 1
	----The following one seems it would never work  because "selected text" doesn't return a text container, but rather the contents of the selection (i.e. the actual text, not a position) 
	----set selected text of the_think_window to the_text
	---A list of other possibilities  that were guesses at what might be plausible, but they don't work either...... :-(
	---set selection of window 1 to insert_location
	--set selection of viewer window 1 to {content_record}
	---set (selection of viewer window 1) to the_selection
	--set selection of viewer window 1 to {the_record}
end tell

I guess all those issues will disappear as soon as tabs and/or web windows will be scriptable. But I can’t promise this for v2.0 yet. At least it’s on my list :wink: