Applescripting RichText in DTPO

I want to; move the cursor to position zero (first character in the document), hit return twice, move the cursor back again to zero, enter a date staring and a colon and a space.

I really do not speak applescript-ese. I have looked in the DTPO applescript dictionary. All It tells me is that:

[at location specifier] : The location at which to insert the object.

It doesn’t tell me beans about what to call the location specifier.

I have a script in BBEdit I worked on a while back that was able to enter returns or line breaks as Bbedit called them. That looked like so:

	add line breaks text of front text window

Can anyone please tell me how to do this in applescript? I have various tools at my disposal that can do parts of this, TextExpander puts in the date string, Keyboard Maestro can assign all of this to a single macro keystroke. Without knowing how to tell applescript to go to position zero and enter 2 returns, this is not going to work. So simple yet so frustrating. Numerous google searches on cursor position only come up with mouse position.

The following script is as far as I have gotten, It puts in a new paragraph at the beginning - as long as I have the script editor running - but It won’t even do that much with the DTPO pull down AS menus.

tell application id "DNtp"
	try
		tell selected text of window 1
			make new paragraph at beginning with data " "
			--make new paragraph at beginning with data "¬" + "--------- This is a test text Insertion ---------"
		end tell
	end try
end tell

Are you posting this because you want to learn to write your own scripts, using this recipe as a test case; or because you want someone to write the script for you? You won’t learn much from the latter.

Scripting text editing isn’t complicated, and the MacScripter.net forum has dozens of tips for how to do that. You can learn more about AppleScript – and scripting Rich Text edits – at MacScripter than you can here. That said, there are many threads in the forum that contain script snippets to show you pretty much the answer to how to do this. I didn’t post the links because I assume you don’t want someone to search the forum for you.

I’m deliberately trying to avoid writing the script you want other than telling you it is actually pretty simple if you follow the examples of the built-in scripts and the advice already posted in other threads – deliberately avoiding because I’ve learned from recent experience that painting someone else’s fence isn’t as enjoyable as it might seem. But I’ll give a hint on how I’d solve this problem – in pseudocode:


set theText to dateString & ":" & return & return & (the text of think window 1)

Good luck! :slight_smile:

OK, can we please not get into flames here on this forum? I find your comment to be patronizing and your remarks are neither appreciated or are they helpful. Responding to my request for help to tell me that I should figure this out for myself is actually not necessary. If you would rather not help, then please, just - don’t help.

I framed my question by saying how I have already been working on the problem. I just wasted one (more) hour doing numerous searches on the net, reading through the applescript dictionary specific to Devonthink. I have tried researching this in many ways already for some time - before I posted my question to this forum. Applescript behaves in different ways specific to the dictionary of various apps. I gave an example of how I can do this in BBEdit and you know what? That wont work the same way in DevonThink. Which is, after all why I asked for help here. Devonthink has a big user base and helpful users. If you are not one of those, simply do not respond.

I strongly disagree. Applescript is very complicated. Some seem to understand It while others - and I am certainly NOT the only one who does not - find It obtuse. Some programming languages make sense to me and I can figure them out. Applescript is not one of those languages.

If you do not want to help, please refrain from responding.

Hello levelbest

I dont think korm was trying to be sarcastic or start a flamewar. He was just trying to give you a clue without necessarily giving you the whole answer. He is the most helpful member on the forum and you should definitely read his post in that light.

I am a bit of a beginner at Applescript myself but I tested your script:

tell application id "DNtp"
   try
      tell selected text of window 1
         make new paragraph at beginning with data " "
         --make new paragraph at beginning with data "¬" + "--------- This is a test text Insertion ---------"
      end tell
   end try
end tell

and it ran perfectly for me both from script debugger and when activated from the applescript menu. There is something else going on in your system if its not running from the DT AS menu. I would kill DT and reboot and try again.

To extend the script you could do the following:

tell application id "DNtp"
	try
		tell selected text of window 1
			make new paragraph at beginning with data (return & (current date) & ":"  & return)
		end tell
	end try
end tell

but I am sure you worked that part out.

DT has no concept of the cursor when referring to the text in text windows. You have to refer to the individual paragraph, words and characters. The following screenshot showing the selected text object shows you what objects are available within it,

Screen Shot 2015-05-03 .jpg

Frederiko

Thanks, It must have been a memory fluke as It is working now from the AS menu as well (didnt have to restart). I still cannot find a way to tell the insertion point where to land for entering my notes. But, thanks fot the help. Here is what I have gotten too, formatted the time and date and text to my liking.

tell application id "DNtp"
	try
		tell selected text of window 1
			
			set the_mon to month of (current date) as integer as string
			if (count characters in the_mon) = 1 then set the_mon to "0" & the_mon
			set the_day to day of (current date) as string
			if (count characters in the_day) = 1 then set the_day to "0" & the_day
			set the_date to the_mon & "/" & the_day & "/" & texts 3 thru 4 of (year of (current date) as string)
			
			set timeStr to time string of (current date)
			set Pos to offset of ":" in timeStr
			set theHour to characters 1 thru (Pos - 1) of timeStr as string
			set timeStr to characters (Pos + 1) through end of timeStr as string
			
			set Pos to offset of ":" in timeStr
			set theMin to characters 1 thru (Pos - 1) of timeStr as string
			set timeStr to characters (Pos + 1) through end of timeStr as string
			
			--Get "AM or PM"
			set Pos to offset of " " in timeStr
			set theSfx to characters (Pos + 1) through end of timeStr as string
			
			set the_date to (the_date & "   " & theHour & ":" & theMin & " " & theSfx) as string
			
			make new paragraph at beginning with data ((the_date) & ": " & return & return) with properties {alignment:justified, font:"Verdana", size:14, color:{5000, 5000, 5000}, background:{65535, 65535, 65535}}
		end tell
	end try
end tell

In any rich text note command-up arrow will move the cursor to the position immediately before the first character in the text. This TextExpander snippet will handle the date: