Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Hotkey to move insertion point to end of sentence?



  • Please log in to reply
16 replies to this topic
poetofpiano
  • Members
  • 109 posts
  • Last active: Jan 25 2016 08:03 PM
  • Joined: 07 Apr 2013

Hi all, I was wondering if there is a script out there to move the insertion point to the end of a sentence. I would define the end of the sentence to be after the next period and space. I would also like to be able to select text to the end of the next sentence as well by pressing shift and the hotkey. And it would be great if the script could work both ways (as in you could press one hotkey to move to the beginning of the next sentence and another to move to the beginning of the previous sentence). My attempts to do this have failed miserably...



poetofpiano
  • Members
  • 109 posts
  • Last active: Jan 25 2016 08:03 PM
  • Joined: 07 Apr 2013

I would also define "end of sentence" to include a paragraph break, even if there is no period at the end.



rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
Well, It depends on what program you are trying to use this with, e.g. Word, Notepad, etc. I don't think you could use the same method in all cases.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


poetofpiano
  • Members
  • 109 posts
  • Last active: Jan 25 2016 08:03 PM
  • Joined: 07 Apr 2013

I am trying to use it with WordPerfect. Word actually has its own macro for this function, but not WordPerfect



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
Go back to the 90s http://www.wpunivers...6c2b627dbd13e21 ;-) (1st result in google)

AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012

I'd like to see this done in AHK in a non-application specific way.



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
That will not be possible. You will have to find a method for each program or group of programs. The best way is to rely on built-in methods of the program by way of a macro or script. If that fails you will need to resort to other methods which may include doing a simple search for a . using the search feature of the program in question (ctrl+f find . forward/backwards search) If the program doesn't have a search feature you could try to have AutoHotkey "grab" the text and the current caret position in there and find the next / previous . and send a number of lefts-rights to move. All in all it will be very clunky. So start with the program where you need it most, try to solve it and then on to the next.

AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012

That's pretty darn negative faqbot. I doubt it's impossible. Actually, I'm pretty sure that working with TEXT is the least of AutoHotkey's possibilities.

Will reply with an answer.



AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012
✓  Best Answer

Test this out.

F9 for Next

F8 for Previous

Shift to Select Next/Previous sentence.

 

PERSERVES CLIPBOARD

F9::
ClipboardOld := ClipboardAll
Clipboard := ""
InsertPoint := 0
InsertPoint2 := 0
Loop
{
Send, ^+{Right}
A_Index2 := A_Index
Send, ^c
Sleep, 20
If (Clipboard = "")
return
Length := StrLen(Clipboard)
InsertPoint := RegExMatch(Clipboard,"\.")
If (InsertPoint = 0)
continue
else
break
}
Send, {Left}
Send, ^{Right %A_Index2%}
Clipboard := ClipboardOld
ClipboardOld := ""
return

+F9::
ClipboardOld := ClipboardAll
Clipboard := ""
InsertPoint := 0
Loop
{
Send, ^+{Right}
A_Index2 := A_Index
Send, ^c
Sleep, 20
If (Clipboard = "")
return
Length := StrLen(Clipboard)
InsertPoint := RegExMatch(Clipboard,"\.")
If (InsertPoint = 0)
continue
else
break
}
Clipboard := ClipboardOld
ClipboardOld := ""
return

F8::
ClipboardOld := ClipboardAll
Clipboard := ""
InsertPoint := 0
InsertPoint2 := 0
EqualPoint := 0
LengthPoint := 0
Match2 := 0
Length2 := 0
Loop
{
Send, ^+{Left}
A_Index2 := A_Index
Send, ^c
Sleep, 20
If (Clipboard = "")
return
Length := StrLen(Clipboard)
If (InsertPoint2 <> 0 && LengthPoint <> 0)
Length -= Length2
If (InsertPoint = InsertPoint2 && InsertPoint2 <> 0)
{
InsertPoint2 += Length
EqualPoint := 1
}
If (EqualPoint <> 0)
{
If (RegexMatch(Clipboard,"\)\.|]\.|}\.") <> 0)
{
Match2 := 1
InsertPoint2 := Length + 2
}
else
InsertPoint2 := Length + 1
}
InsertPoint := RegExMatch(Clipboard,"\.")
If (InsertPoint = InsertPoint2)
continue
else
{
If (InsertPoint2 = 0)
{
Length2 := Length
InsertPoint2 := InsertPoint
LengthPoint := 1
continue
}
else
break
}
}
Send, {Right}
A_Index2 -= 1
Send, ^{Left %A_Index2%}
Clipboard := ClipboardOld
ClipboardOld := ""
return

+F8::
ClipboardOld := ClipboardAll
Clipboard := ""
InsertPoint := 0
InsertPoint2 := 0
EqualPoint := 0
LengthPoint := 0
Match2 := 0
Length2 := 0
Loop
{
Send, ^+{Left}
A_Index2 := A_Index
Send, ^c
Sleep, 20
If (Clipboard = "")
return
Length := StrLen(Clipboard)
If (InsertPoint2 <> 0 && LengthPoint <> 0)
Length -= Length2
If (InsertPoint = InsertPoint2 && InsertPoint2 <> 0)
{
InsertPoint2 += Length
EqualPoint := 1
}
If (EqualPoint <> 0)
{
If (RegexMatch(Clipboard,"\)\.|]\.|}\.") <> 0)
{
Match2 := 1
InsertPoint2 := Length + 2
}
else
InsertPoint2 := Length + 1
}
InsertPoint := RegExMatch(Clipboard,"\.")
If (InsertPoint = InsertPoint2)
continue
else
{
If (InsertPoint2 = 0)
{
Length2 := Length
InsertPoint2 := InsertPoint
A_Index3 := A_Index
LengthPoint := 1
continue
}
else
break
}
}
Send, {Right}
Send, ^{Left %A_Index3%}
Send, {Right 1}
A_Index2 -= A_Index3
Send, ^+{Left %A_Index2%}
Clipboard := ClipboardOld
ClipboardOld := ""
return

Works for me in Notepad as well as on this form editbox.

It is slightly "clunky" but it's much MUCH faster than seeking the sentence end/begin myself.



AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012

WARNING

Without additional code, selecting text in a NON-EDIT field and then activating this, it WILL BREAK and wreck everything (until a Ctrl+Alt+Del)



AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012

As well, this does not handle paragraph breaks. I'm not sure how to specify to include that. If VERY desired, I can update the code.



poetofpiano
  • Members
  • 109 posts
  • Last active: Jan 25 2016 08:03 PM
  • Joined: 07 Apr 2013

Thank you very much AfterLemon. I want to be sure I know what you mean by "Will break and wreck everything." Basically cause the application to not respond, or worse? I don’t want to use the code if there is too big a risk associated with it. Is it possible to only allow the code to be sent if you are in a text field? If it would take a substantial amount of work to perfect, don’t worry about it, I can use it as is. Thanks again :)



AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012

What I mean is, the computer sees the text, copies it to the clipboard and tests to see if it has a . somewhere, then "moves the caret".

The issue with a non-edit field is that it cannot move the caret, so it continually copies the clipboard, attempts the same thing, and repeats.

This will cause the sent keys to be repeated as well.

 

I fiddled with handling edit-only, but it'd be handled by possible variations in clipboard content, not by identifying the field.

As of the 10 minutes I spent on it, I found no solution.

 

I could delve into it for a second look. Give me ~an hour.



AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012

Further, about the risk...

 

Any loops can be stopped prematurely with a Ctrl-Alt-Del, but the script must be killed first (lest it continue looping after returning to the desktop).

 

In practice, this can be handled in ~3 seconds, which is only a few sent {Enter}s (which may activate shortcuts, open documents, confirm msgboxes, etc.)



AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012

This should work:

F9::
	ClipboardOld := ClipboardAll
	Clipboard := ""
	InsertPoint := 0
	InsertPoint2 := 0
	ClipboardFirst := ""
	Loop
	{
		Send, ^+{Right}
		A_Index2 := A_Index
		Send, ^c
		Sleep, 20
		If (ClipboardFirst = "")
			ClipboardFirst := Clipboard
		If (Clipboard = "" || ClipboardFirst = Clipboard)
			return
		Length := StrLen(Clipboard)
		InsertPoint := RegExMatch(Clipboard,"\.")
		If (InsertPoint = 0)
			continue
		else
			break
	}
	Send, {Left}
	Send, ^{Right %A_Index2%}
	Clipboard := ClipboardOld
	ClipboardOld := ""
return

+F9::
	ClipboardOld := ClipboardAll
	Clipboard := ""
	InsertPoint := 0
	ClipboardFirst := ""
	Loop
	{
		Send, ^+{Right}
		A_Index2 := A_Index
		Send, ^c
		Sleep, 20
		If (ClipboardFirst = "")
			ClipboardFirst := Clipboard
		If (Clipboard = "" || ClipboardFirst = Clipboard)
			return
		Length := StrLen(Clipboard)
		InsertPoint := RegExMatch(Clipboard,"\.")
		If (InsertPoint = 0)
			continue
		else
			break
	}
	Clipboard := ClipboardOld
	ClipboardOld := ""
return

F8::
	ClipboardOld := ClipboardAll
	Clipboard := ""
	InsertPoint := 0
	InsertPoint2 := 0
	EqualPoint := 0
	LengthPoint := 0
	Match2 := 0
	Length2 := 0
	ClipboardFirst := ""
	Loop
	{
		Send, ^+{Left}
		A_Index2 := A_Index
		Send, ^c
		Sleep, 20
		If (ClipboardFirst = "")
			ClipboardFirst := Clipboard
		If (Clipboard = "" || ClipboardFirst = Clipboard)
			return
		Length := StrLen(Clipboard)
		If (InsertPoint2 <> 0 && LengthPoint <> 0)
			Length -= Length2
		If (InsertPoint = InsertPoint2 && InsertPoint2 <> 0)
		{
			InsertPoint2 += Length
			EqualPoint := 1
		}
		If (EqualPoint <> 0)
		{
			If (RegexMatch(Clipboard,"\)\.|]\.|}\.") <> 0)
			{
				Match2 := 1
				InsertPoint2 := Length + 2
			}
			else
				InsertPoint2 := Length + 1
		}
		InsertPoint := RegExMatch(Clipboard,"\.")
		If (InsertPoint = InsertPoint2)
			continue
		else
		{
			If (InsertPoint2 = 0)
			{
				Length2 := Length
				InsertPoint2 := InsertPoint
				LengthPoint := 1
				continue
			}
			else
				break
		}
	}
	Send, {Right}
	A_Index2 -= 1
	Send, ^{Left %A_Index2%}
	Clipboard := ClipboardOld
	ClipboardOld := ""
return

+F8::
	ClipboardOld := ClipboardAll
	Clipboard := ""
	InsertPoint := 0
	InsertPoint2 := 0
	EqualPoint := 0
	LengthPoint := 0
	Match2 := 0
	Length2 := 0
	ClipboardFirst := ""
	Loop
	{
		Send, ^+{Left}
		A_Index2 := A_Index
		Send, ^c
		Sleep, 20
		If (ClipboardFirst = "")
			ClipboardFirst := Clipboard
		If (Clipboard = "" || ClipboardFirst = Clipboard)
			return
		Length := StrLen(Clipboard)
		If (InsertPoint2 <> 0 && LengthPoint <> 0)
			Length -= Length2
		If (InsertPoint = InsertPoint2 && InsertPoint2 <> 0)
		{
			InsertPoint2 += Length
			EqualPoint := 1
		}
		If (EqualPoint <> 0)
		{
			If (RegexMatch(Clipboard,"\)\.|]\.|}\.") <> 0)
			{
				Match2 := 1
				InsertPoint2 := Length + 2
			}
			else
				InsertPoint2 := Length + 1
		}
		InsertPoint := RegExMatch(Clipboard,"\.")
		If (InsertPoint = InsertPoint2)
			continue
		else
		{
			If (InsertPoint2 = 0)
			{
				Length2 := Length
				InsertPoint2 := InsertPoint
				A_Index3 := A_Index
				LengthPoint := 1
				continue
			}
			else
				break
		}
	}
	Send, {Right}
	Send, ^{Left %A_Index3%}
	Send, {Right 1}
	A_Index2 -= A_Index3
	Send, ^+{Left %A_Index2%}
	Clipboard := ClipboardOld
	ClipboardOld := ""
return

Give it a try on this forum text.