Text Position

Sorry for the NOOB comments, I am now building up a Snippets DB so moving forward I will try to capture all my AppleScript chunks for my future review. I am not understanding some basic text manipulation skills. How do I tell the script to move the insertion point to a specific location? Position zero is the first insertion point but, how do I write this? All I can find is loads of examples about moving the cursor related to setting window positions. That is not what I am asking here.

In a Rich Text note within DevonThink, I am trying to keep a log of my progress on a certain project. I am also using Keyboard Maestro to either enhance using Applescript or at the very least, attach a keystroke command to execute the script.

What I want to do upon execution of the macro:

  1. move the insertion point to the top of the text, position zero,
  2. put in two returns
  3. Return the insertion point to point zero
  4. put in a date and time string ending with a colon and a space.

I can use a script example I just found on these forums to use AppleScript to tell TextExpander to expand the date and time - or if there is a way to use AppleScript to do this I can do that too.

Sorry but I just cannot find the example that shows me how to know where my insertion point is? I know I Have done this a long time ago. I just don’t use AppleScript everyday and I still find much about It obscure and hard to understand. Building my own version of logic in understanding the components of AppleScript moving forward might help.

Thanks for any help.

ADDED: Her is what I have been able to figure out so far. I still cannot get the insertion point to where I Want to start typing the new note. I don’t know if It is possible but if I could find the first instance of “:” and count two spaces that would be the ideal starting point for entering my new notes.

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_day & "/" & the_mon & "/" & 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
			
			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

Hi level,

They used to use insertion point before, but not nowdays. Although I think Text Edit Plus uses insertion point. What might be the best thing to do nowadays is to parse the text if there is only text in the document. I don’t know about the app you’re using, so not sure.

But, you can use uielement scripting. What this is used for is to create a macro like program with System Events.

It’s hard to say what to do 'cause don’t have your app.

gl,
kel

Thanks. This is one of the things that makes me get headaches trying to work in AppleScript. Not all apps use the same language. Oh well.

In this link: https://discussions.apple.com/thread/1085873?start=0&tstart=0 there is a discussion using “offsets” as a way to tell the insertion point where to go. But I ah not able to make sense of this. Are you saying that there is likely no way to do this anymore?

I suppose I could look at copying all the text, doing the manipulation and then reinserting It again. Oh uh, my head is hurting again. :stuck_out_tongue:

Thanks.

I believe you want a journaling script.

Hi levelbest.

I’m not familiar with DevonThink’s scripting dictionary, so this is just theorising.

Applications which recognise ‘insertion points’ usually refer to them as coming before or after some element in the text, eg.:

insertion point before character 1 of text of window 1
insertion point after paragraph 2 of text of window 1

If an application supports both ‘select’ and ‘insertion point’, moving the cursor to a certain position may be as simple as selecting the insertion point:

select insertion point after paragraph 2 of text of window 1

With luck, something like this may work with DevonThink.

By the way, in the script you posted, only the ‘make’ line needs to be inside the ‘tell’ statements. The rest is all “vanilla” (as opposed to DevonThink) AppleScript. There are better ways to write it too, but that’s not what you asked. :slight_smile: