Lost focus on paragraph spacing - help with small script

Hi,

I’m not proficient in apple script coding, so I would appreciate a bit of help.

Often I want to change paragraph spacing, but I want to do it via keyboard shortcut, not with mouse.

So I have this script (created with claude.ai):

tell application id "DNtp"
	set frontmost_record to record of think window 1
	
	-- Try setting line spacing on the text itself
	set line spacing of rich text of frontmost_record to 1.3
end tell

Now this is the rtf file before the script:

Once I run the script, the paragraph spacing is set to 1.3 , but the focus is lost from text area:

So in order to continue typing, again I need to use the mouse.

Is there a way to return the focus to text area via the script ?

p.s. I had multiple attempts with claude.ai but the code was not working

Thanks

I think it’s because you’re changing what you’re working on as far as DEVONthink is concerned – you’re working at the document level, so the text area loses focus.

After some experimentation, I found after your script runs that you can return to the text window at the prior insertion point with ⌃-Tab, so that can be added to your script via System Events:

tell application id "DNtp"
	set frontmost_record to record of think window 1
	-- Try setting line spacing on the text itself
	set line spacing of rich text of frontmost_record to 1.3
	delay 1
	tell application "System Events" to keystroke (ASCII character 9) using control down
end tell

The delay 1 allows the format modification to be processed before sending the UI level keystroke.

This is currently working for me, no matter where the insertion point is when I run it, including in a 4k+ document – I don’t know if a larger delay would be needed for larger documents, but you could perhaps figure out, via experimentation, a “per kilobyte delay” by getting the size of the content. I haven’t even experimented with sub-second delays, but that’s pretty straightforward for you to try.

Lastly, keep in mind “line spacing” is in points, so you’re going from default 0.0 points between lines to 1.3 – is that your intention, or did you mean to use 1.3 as a multiple (in which case you’d use multiple line height, not line spacing)?

Sean

OK, I couldn’t resist, I tried sub-second delays. for some reason there are weird results with that. Sometimes the ⌃-Tab endds up in the text area, sometimes the cursor is already there and the ⌃-Tab ends up in the tags bar at the bottom of the window (which I have showing).

1 seconds seems to just work, even at 5MB files, so I’d stick with that.

Sean

There are several examples available in the menu Scripts > Format. In this case a single line is sufficient. To modify only the text selection, use selected text instead of rich text.

tell application id "DNtp" to set line spacing of rich text of think window 1 to 1.3

I don’t think any text is selected in th eOP’s description of use.

Sean

@stratadata / @cgrunenberg thank you very much , really really appreciated .. I’ve learned some new things :slight_smile:

Few comments from my side:

  • first, ^ + Tab actually works: when conversing with AI it was suggesting cmd + arrow down but it was not working
  • second , indeed, what I needed was multiple line height :slight_smile:
  • third, yes, I was interested in whole document, not just selection: my need here is that often I instantly open new rtf with keyboard shortcut and start typing and after 2-3 lines I just need (sometimes) to change the spacing , I mean the multiple line height
  • fourth, I’ve checked the Scripts Folder → Format, indeed good to learn a few new tricks how to manipulate formatting.

Side question, I cannot figure out what is being achieved with the underscore in the naming of first two files - I assume it has to do with visual separators in the menu ?

Cheers

1 Like

This inserts a separator in the menu, either before (prefix) or after (suffix) the item.

4 Likes

I just want to follow up on this with some new findings.
I’m not sure if this is a bug or not but: if you have rtf document with outline like this:

and then run the script (I run it via Alfred), then it resets the outline, which is not good:

I tried this with the script provided by stratadata , also I was experimenting with commenting of delay and ^ + Tab lines.
Then I tried with script provided in Scripts folder (the one c/p below with minimal changes). Then manually I select whole text if rtf (cmd + a)

tell selected text of think window 1
		set line spacing to 1.3
end tell

and the effect is the same.

Then of course I tried with manual select + manually selecting (with mouse) from the menu → this didn’t mess up the outline.

So just wanted to share with the community here.
I guess I’ll stick to the manual way :smirking_face:

Cheers

A simple but non-working snippet…

tell application id "DNtp"
	if (selected text of think window 1) is "" then return
	
	tell rich text of think window 1
		set line spacing to 0.5
	end tell
end tell

Development would have to assess this but it appears the AppleScript command isn’t affecting rich text. It is adding an entry to the Spacing popup but not applying the value, even when selected manually…

The popup just shows the line spacing of the current selection, meaning that the script was successful. But.a line spacing of 0.5 points is of course more or less invisible.

1 Like

Selecting +2.0 didn’t affect the text either. That was also set via script in an earlier iteration.

I found using multiples of 10 when changing the line spacing item (which is in pts) much easier to see the effect. I couldn’t tell the difference at the original 1.3, not sure if 2.0 would be much better.

For line height multiple attribute (called multiple line height in AppleScript), I just used 1x and 3x, once again, to better visualise what was changing, and how quickly.

I may or may not explore more if I ever want to do this programmatically.

I cannot tell why the bullet indents are being reset, it’s not even using the first (or last) indent.

Sean

Your script requires selected text…

	if (selected text of think window 1) is "" then return

…but then you change the rich text of the window:

tell rich text of think window 1

Definitely works with a selection over here.