Copy selected text in notepad a certain amount of times based on number in selected text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BonomBonom
Posts: 15
Joined: 24 Aug 2016, 23:25

Copy selected text in notepad a certain amount of times based on number in selected text

28 Mar 2017, 03:34

Hello there!

I would like to do the following :

If I select a text like this " Apple 6 " and press a hotkey ( for ex ctrl-shift-down ) it will copy " Apple 6 " six(6) times in a specific notepad document, one under the other.

Apple 6
Apple 6
Apple 6
Apple 6
Apple 6
Apple 6

Then, if I select " Toaster 3" and do the same hotkey, it will copy "Toaster 3" three times under the previous.

Apple 6
Apple 6
Apple 6
Apple 6
Apple 6
Apple 6
Toaster 3
Toaster 3
Toaster 3

Then , if I select another string of text that has a number in it, it will copy the full string even if the number is not at the end of the sentence. " I want 2 kittens for my experiences " would then be :

Apple 6
Apple 6
Apple 6
Apple 6
Apple 6
Apple 6
Toaster 3
Toaster 3
Toaster 3
I want 2 kittens for my experiences
I want 2 kittens for my experiences

...etc...

However, I would like that if its a 2 digit number (lets say 12) it will copy it 12 times, (1+2 times).

I "know" how to copy a string at the end of a notepad document, however I do not know how to do the counting part. When I say "I know", I mean that I've been given a script for a specific task that I asked here I believe, and I've understood some parts of it enough to modify it for different needs, however I have too many holes in what I understand to be able to push it further.

Here is what I use to copy selected text at the end of a specific tab in Notepad++ called "Bucket List".

Code: Select all

^+left::
^+s::
	titleOfTab:="new 1" 
	if !WinExist("ahk_exe notepad++.exe")
		return
	Clipboard:=""	
	Send,^c
	ClipWait,1
	if ErrorLevel
		return
		
	WinActivate ahk_exe notepad++.exe
	WinWaitActive ahk_exe notepad++.exe
	WinGetActiveTitle,title
	
	t:=title
	ctr:=0
	goBack:=0
	SetTitleMatchMode,2
	Loop
	{
		Send,^{Pgdn}
		ctr++
		WinWaitNotActive, %title%
		WinGetActiveTitle,title
		if InStr(title,titleOfTab)
		{
			SendInput,{Pgdn}	
			Sleep,50
			SendInput,{Enter}{Enter}{Enter}
			Sleep,50
			SendInput,^v
			goBack:=1
			break
		}
	} until (t=title)
	
	if !goBack
		return
	Loop, %ctr%
	{
		Send,^{Pgup}
		Sleep,25
	}
	Send {alt down}{tab}{alt up}
	;Msgbox Pasted !!
return
What should I add/modify to be able to do what I explained at the beginning of my message ? Also, I would like the pasting to happen in a specific notepad document (by its full name and path, not by its tab).

Thank you!
Nightwolf85
Posts: 302
Joined: 05 Feb 2017, 00:03

Re: Copy selected text in notepad a certain amount of times based on number in selected text

28 Mar 2017, 06:50

You can simplify what you already have since you can write to a file without it being open already.

check out the code below, it might work for what you want already, but if not should be easy to change to accomplish your task.

Code: Select all

^+s::
selectedText := GetSelectedText()
numberInString := RegExReplace(selectedText, "^.*?([0-9]+).*$", "$1") ; This will find the first number in a string. So if you have 'Test 3 text again 5" it will only find 3.
Loop %numberInString%
	FileAppend, % selectedText . "`n", Test.txt ; Change Test.txt to the file path you want the text appended to.
Return

GetSelectedText() {
	tmp := ClipboardAll
	Clipboard := ""
	Send, ^c
	ClipWait, 1
	retVal := Clipboard
	Clipboard := tmp
	Return retVal
}
Last edited by Nightwolf85 on 28 Mar 2017, 07:04, edited 1 time in total.
User avatar
aztec3
Posts: 177
Joined: 07 Apr 2014, 12:05

Re: Copy selected text in notepad a certain amount of times based on number in selected text

28 Mar 2017, 07:01

However, I would like that if its a 2 digit number (lets say 12) it will copy it 12 times, (1+2 times).
Just to clarify...do you want it to do it 12 times or 3 (1+2 times) if the double digit value is 12???
Nightwolf85
Posts: 302
Joined: 05 Feb 2017, 00:03

Re: Copy selected text in notepad a certain amount of times based on number in selected text

28 Mar 2017, 16:12

To cover the case where you want 12 to loop 3 (1 + 2) times, you can use this instead.

Code: Select all

^+s::
selectedText := GetSelectedText()
numberInString := RegExReplace(selectedText, "^.*?([0-9]+).*$", "$1") ; This will find the first number in a string. So if you have 'Test 3 text again 5" it will only find 3.
useNum := 0
Loop, Parse, numberInString
	useNum += A_LoopField
Loop %useNum%
	FileAppend, % selectedText . "`n", Test.txt ; Change Test.txt to the file path you want the text appended to.
Return

GetSelectedText() {
	tmp := ClipboardAll
	Clipboard := ""
	Send, ^c
	ClipWait
	retVal := Clipboard
	Clipboard := tmp
	Return retVal
}
BonomBonom
Posts: 15
Joined: 24 Aug 2016, 23:25

Re: Copy selected text in notepad a certain amount of times based on number in selected text

28 Mar 2017, 18:33

I tried it and it works perfectly! Thank you :-)

And yeah, I meant 12 = 12, not 1+2 ! (the word "not" was missing in the parenthesis of my first message)

Thanks again!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid and 199 guests