Bug: scripting selected text in side-by-side view

A minor bug/undesirable behavior:

  1. Open a markdown document in side-by-side view.
  2. Select some random text in the right-side (preview) pane.
  3. Run any script which involves modifying the selected text of the window. An example script is given below:
tell application id "DNtp"
	set selected text of think window 1 to "loren ipsum"
end tell

As far as I can tell, the aforementioned script will insert loren ipsum at a specific place, that is, the last position of the caret in the left-side (editing) pane. The selected text in the right-side pane does not matter at all.

It becomes weirder when selected text is involved in the replacement string. Use the following script and repeat steps 2 and 3 a couple of times. Script Editor will churn out an error: The variable theText is not defined despite the fact that some text in the right-side pane is selected.

tell application id "DNtp"
	set theText to selected text of think window 1
	set selected text of think window 1 to theText & " loren ipsum"
end tell

A side question: Is it possible to get and/or set the active view (i.e. what is selected in Menu Bar > View > Document display) of a document tab in scripting?

The selected text in the right-side pane does not matter at all.

Correct. The preview is only the preview. You aren’t going to change the text in it. You can only change text in the source.

Also, replacing selected text doesn’t leave the text selected.

A side question: Is it possible to get and/or set the active view (i.e. what is selected in Menu Bar > View > Document display ) of a document tab in scripting?

No, there are no commands in DEVONthink’s dictionary for this.
Why?

Two use cases I can think of:

  1. For specifying the view of each tab when opening a good number of tabs via open tab for.
  2. To prevent the unexpected behavior described in this thread by checking the active view of the current tab.

I see how it makes sense from the developer’s perspective. However, this could be misleading for the user, who surely expects something to happen to the text they selected in the right-side pane but gets something different instead.

In Preview view, selected text corresponds to user-selected text, whereas set selected text would not change anything. This makes sense. IMO DT should behave this way when the user selects text in the right-side view, too.

Getting the selected text in the rendered preview does make sense as it is selectable.
If the text in the rendered preview was editable, a behavior found in Markdown editors with a hybrid view, e.g. Typora, the expectation would be reasonable. However, that is not the behavior found in DEVONthink so it’s not a reasonable expectation for the user to have, nor should it be seen as misleading.

I understand your point about the programming logic. However, I don’t think one should expect the user’s expectation to be reasonable all the time. Users like me are dumb, so you can’t really assume that they think like a developer does.

I have actually been surprised by this behavior a few times while editing HTML documents in Side-by-Side view before posting here. Undo is impossible because the change was done through scripting. It’s really hard to find out what has been injected by set selected text in a loooong document!

When the user makes a questionable move, as they do every once in a while, it would be nice if the program offers a degree of protection by either (1) producing an error/warning, (2) simply rejecting the move and doing nothing. Option (2) is implemented in Preview view, and I appreciate that. Any chance of granting us such a nicety in Side-by-Side as well? :blush: