Automatic add 1 to value in clipboard Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MR Norway

Automatic add 1 to value in clipboard

23 Jul 2017, 15:16

Hi!


First of all; scripting is a new world for me. I find this forum as very useful, but I can not find topics that will help me along this path:

I want to make a script that by comand ctrl+shift+v takes the last value of the string in clipboard, increase it by 1, update the clipboard, and then paste it out.


This is what I want to accomplish:

Example 1: 125 Meeting room lamp 1 -> 125 Meeting room lamp 2

Example 2: 123 Living room lamp 14 -> 123 Kitchen lamp 15

Example 3: 8 -> 9

Example 4: 103 -> 104



I have sortet out how to manipulate the clipboard string, but not how to locate my value and the math for this function.

This is what I have got so far:


^+v::
StringReplace, clipboard, clipboard, oldvalue, newvalue, ; if the word oldvalue is in clipboard, it will be replaced by the word newvalue.
Send, ^v
return



Can anyone point out a direction in where to go from here? Tried this forum and AutoHotkey Help, but didn't find anything useful or understandable with my limited knowledge.


Thank you for any help!
MartinR
Posts: 2
Joined: 23 Jul 2017, 15:43

Re: Automatic add 1 to value in clipboard

23 Jul 2017, 16:12

Me again, now with registered profile and corrected a typo error.


This is what I want to accomplish:

Example 1: 125 Meeting room lamp 1 -> 125 Meeting room lamp 2

Example 2: 123 Kitchen lamp 14 -> 123 Kitchen lamp 15

Example 3: 8 -> 9

Example 4: 103 -> 104
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Automatic add 1 to value in clipboard

23 Jul 2017, 16:35

So the Example 3 and 4 are fairly easy to do. We can use the ++ operator to increase the value of a variable - and clipboard is a variable - by one.

But now when you are doing Example 1 and 2, there are two obstacles to overcome.

1) We need to identify the number inside the string, as the ++ operator wouldn't work on this (I don't think, you can try it, but I expect it to fail).

2) You actually have two numbers, but you want to increase only one of them. It looks like you want to increase the last number.

So, we can actually identify numbers by using something called Regular Expression Matching, with the RegExMatch() function. Learning it alone is about as tough as learning the general AHK language as a whole, and I don't have a good handle on it myself. But in this case, the haystack is the clipboard (this is what we'll search for a match in), and the needle is what we're looking for -- get it? needle in a haystack. But figuring out the needle is the tricky part. See RegEx. There are three things that come to mind: We'll use \d which means digits, then we'll use + to indicate minimum of 1 digit in a row, and then we'll use the $ to signal that this must be at the end of the string. We can output the matched number into a variable, as per the RegExMAtch() parameters.

Then we can increase that variable by 1.

But that is just to match the value at the end, then we need to replace it. RegExReplace() can do the same thing, but make a replacement for us. And now we can replace what we matched for the needle with the new value in the previous variable.

This uses the variable string instead of clipboard, but just substitute the word string for the word clipboard below.

Code: Select all

string:="123 Kitchen lamp 15" ; sample string
RegExMatch(string,"\d+$",match) ; variable is output into match, which is defined inside the function. Otherwise, RegExMatch would give us the position of the match in the string. (Read up on it in the documentation)
match++ ; increase by 1
string:=RegExReplace(string,"\d+$",match) ; the final result is output to string which is done with the := assign operator
MsgBox % string ; should get 123 Kitchen lamp 16
return
MartinR
Posts: 2
Joined: 23 Jul 2017, 15:43

Re: Automatic add 1 to value in clipboard

23 Jul 2017, 22:22

Thank you for superb answer, learned a lot!
RegEx is the function I need, and now I also understand why.

Again, thank you so much!


My final script looks like this:

Code: Select all

^+v::						 	         	          ;when Ctrl+Shift+v is typed in
  RegExMatch(clipboard,"\d+$",match)				  ;finds number last in clipboard string, and stores it to the value "match"
  match++							                          ;increases the value of "match" by 1
  clipboard:=RegExReplace(clipboard,"\d+$",match) 	       	  ;replaces the last number in clipboard string with the new value of "match"
  send ^v					  		                          ;sending Ctrl+V command
  return	
This script finds the last number in the clipboard string, increases it by 1, updates the clipboard, and paste it out.
If there are only one number in the clipboard, it will work for that number.
If there are multiple numbers in the clipboard, it will only affect the last one.
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 05:07

@Exaskryz

Hallo,
Example 1: 125 Meeting room lamp 1 -> 125 Meeting room lamp 2

Example 2: 123 Kitchen lamp 14 -> 123 Kitchen lamp 15

Example 3: 8 -> 9
How can i do add +1 to only the latest number but with text or signs after the latest number. For example like:
(I want to keep all the text and signs and only change the latest number.)

Example 1: 125 Meeting room lamp 1 -> 125 Meeting room (lamp 2)

Example 2: 123 Kitchen lamp 14 -> 123 Kitchen lamp 15 in the room.

Example 3: 8_ -> 9_

Thank you!
Have a good day.
User avatar
boiler
Posts: 17278
Joined: 21 Dec 2014, 02:44

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 05:46

@John1 - Your examples don’t match what you described, except for the last one. You said you want to keep text and signs, but your first two examples show text and signs that weren’t in the “before” version. Assuming you meant more like the third example:

Code: Select all

^+v::
	RegExMatch(Clipboard,"\d+(?=\D*$)", M)
	Clipboard := RegExReplace(Clipboard,"\d+(?=\D*$)", ++M)
	Send, ^v
return
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 07:35

@boiler

Thank you a lot! Works perfect.

I have two more questions:

1.
And how can trim this:

Code: Select all

Number_A100([250,20], [480,35])
to

Code: Select all

250,20
2.
How can i trim this to: always starting with that what is after "number 1"{space} and after that keep the next 12 letters/numbers/symbos, whatever will be the next signs.

Code: Select all

number 1 (number 55) (list 20)
to

Code: Select all

(number 55)
___________________

Code: Select all

number 1 (text) (list 21)
to

Code: Select all

(text) (list

Thank you.
User avatar
boiler
Posts: 17278
Joined: 21 Dec 2014, 02:44

Re: Automatic add 1 to value in clipboard  Topic is solved

04 Sep 2022, 08:27

Rather than just keep writing code for you, I’m willing help you learn how to do it. Are you looking to learn regular expressions (RegEx)?
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 08:45

@boiler

Thank you for your reply.

I watched videos from here:
https://www.youtube.com/watch?v=od6QYIrs2xk

And read at the moment the tutorial:
viewtopic.php?t=28031

I tried before myself before posting and it did not work after several attemps.

But Thank you.
User avatar
boiler
Posts: 17278
Joined: 21 Dec 2014, 02:44

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 08:48

Please post your attempts so that you can get feedback about how to fix them.
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 12:51

@boiler

Ok.

Code: Select all

Number_A100([250,20], [480,35])
to

Code: Select all

250,20
1.

Code: Select all

1::
MsgBox, % RegExReplace(Clipboard, "^.*?\[")
return
ends in result

Code: Select all

250,20], [480,35])
2.

Code: Select all

2::
MsgBox, % RegExReplace(Clipboard, "^.*?\K\].*") 
return
What means "K",

ends in result

Code: Select all

Number_A100([250,20
3.
I want to do both at the same time:

Code: Select all

3::
MsgBox, % RegExReplace(Clipboard, "^.*?\[", "^.*?\]\K.*")
return
_____________________________

Code: Select all

number 1 (number 55) (list 20)
to

Code: Select all

(number 55) (list 20)
or

Code: Select all

number 1 (text) (list 21)
to

Code: Select all

(text) (list

Code: Select all


7::
Clipboard := "number 1 (number 55) (list 20)"
MsgBox, % RegExReplace(Clipboard, "^.*?number\s\d\s")
return

;First i didn't figure it out how to remove the spaces and the number, so i found also this solution:
8::
Clipboard := "number 1 (number 55) (list 20)"
MsgBox, % RegExReplace(Clipboard, "`nm)^.{1,9}")
return

User avatar
boiler
Posts: 17278
Joined: 21 Dec 2014, 02:44

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 14:01

Let's just take them one at a time, starting with the first one. It's also easier to not have to tie it to hotkeys while developing the RegExPattern. Just assign the Clipboard (or whatever variable you want) in the script for now.

And I think it would be much easier for you to start with RegExMatch so that you can think about what you're trying to match correctly. By using RegExReplace and replacing it with a null string, you are keeping the opposite of the match, which is more complicated to conceptualize if you're new to this. So instead of this like you tried:

Code: Select all

Clipboard := "Number_A100([250,20], [480,35])"
MsgBox, % RegExReplace(Clipboard, "^.*?\[")
...try it by thinking about what you're trying to keep, which is just a pair of numbers separated by a comma:

Code: Select all

Clipboard := "Number_A100([250,20], [480,35])"
RegExMatch(Clipboard, "\d+,\d+", M)
MsgBox, % M

Now try to apply the above to your second one and post your code.
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 14:23

Thank you a lot teacher!

Code: Select all

8::
Clipboard := "number 1 (number 55) (list 20)"
RegExMatch(Clipboard, "\(\w+\s\d+\)\s\(\w+\s\d+\)", M)
MsgBox, % M
return
Last edited by John1 on 04 Sep 2022, 14:58, edited 1 time in total.
User avatar
boiler
Posts: 17278
Joined: 21 Dec 2014, 02:44

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 14:55

Didn't you just want (number 55) from the second one?
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: Automatic add 1 to value in clipboard

04 Sep 2022, 15:00

Ok. then:

Code: Select all

8::
Clipboard := "number 1 (number 55) (list 20)"
RegExMatch(Clipboard, "\(\w+\s\d+\)", M)
MsgBox, % M
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Kellyzkorner_NJ and 127 guests