Automatic add 1 to value in clipboard
Automatic add 1 to value in clipboard
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!
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!
Re: Automatic add 1 to value in clipboard
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
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
Re: Automatic add 1 to value in clipboard
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.
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
Re: Automatic add 1 to value in clipboard
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:
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.
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
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.
Re: Automatic add 1 to value in clipboard
@Exaskryz
Hallo,
(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.
Hallo,
How can i do add +1 to only the latest number but with text or signs after the latest number. For example like: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
(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.
Re: Automatic add 1 to value in clipboard
@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
Re: Automatic add 1 to value in clipboard
@boiler
Thank you a lot! Works perfect.
I have two more questions:
1.
And how can trim this:
to
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.
to
___________________
to
Thank you.
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])
Code: Select all
250,20
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)
Code: Select all
(number 55)
Code: Select all
number 1 (text) (list 21)
Code: Select all
(text) (list
Thank you.
Re: Automatic add 1 to value in clipboard
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)?
Re: Automatic add 1 to value in clipboard
@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.
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.
Re: Automatic add 1 to value in clipboard
Please post your attempts so that you can get feedback about how to fix them.
Re: Automatic add 1 to value in clipboard
@boiler
Ok.
to
1.
ends in result
2.
What means "K",
ends in result
3.
I want to do both at the same time:
_____________________________
to
or
to
Ok.
Code: Select all
Number_A100([250,20], [480,35])
Code: Select all
250,20
Code: Select all
1::
MsgBox, % RegExReplace(Clipboard, "^.*?\[")
return
Code: Select all
250,20], [480,35])
Code: Select all
2::
MsgBox, % RegExReplace(Clipboard, "^.*?\K\].*")
return
ends in result
Code: Select all
Number_A100([250,20
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)
Code: Select all
(number 55) (list 20)
Code: Select all
number 1 (text) (list 21)
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
Re: Automatic add 1 to value in clipboard
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:
...try it by thinking about what you're trying to keep, which is just a pair of numbers separated by a comma:
Now try to apply the above to your second one and post your code.
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, "^.*?\[")
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.
Re: Automatic add 1 to value in clipboard
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.
Re: Automatic add 1 to value in clipboard
Didn't you just want (number 55) from the second one?
Re: Automatic add 1 to value in clipboard
Ok. then:
Code: Select all
8::
Clipboard := "number 1 (number 55) (list 20)"
RegExMatch(Clipboard, "\(\w+\s\d+\)", M)
MsgBox, % M
return