Trying to compare clipboard value to a variable value to remove certain text and copy the remainder to a teraterm window - autohotkey

Notice the if statements below. I am copying the terminal output to the clipboard. It contains RP/0/RSP0/CPU0:cobn9-hub#. I want to remove RP/0/RSP0/CPU0: from from the clipboard and it does work however when I try to add an additional string to that variable var1 nothing works.
var1 := "RP/0/RSP0/CPU0:,mnet-prd-hub"
without ,mnet-prd-hub it will remove the unwanted text but if I add something else to var1 it stops working.
I want to also remove mnet-prd-hub:
from mnet-prd-hub:/home/data/configs/current$/home/data/configs/current$
I've tried if var contains %clipboard"
I've tried clipboard contains %var1%
I've tried IfInstring with no luck.
So I am asking for someones tutelage.
I've tried for hours with no luck any help would be greatly appreciated.
SetTitleMatchMode, 2
#IfWinActive, ahk_class VTWin32
::ttwa::
var1 =
var1 :=
clipboard = ; empty clipboard
sleep 200
send !e {enter}
send s
send {enter 100}
sleep 100
Send {click}
sleep 100
send {click}
sleep 100
send {click}
sleep 10
MsgBox 1st %clipboard% %var1%
;var1 represents a Cisco 9k this script removes var1 puts the proper name in the Title Window
var1 := "RP/0/RSP0/CPU0:,mnet-prd-hub"
MsgBox 2nd %clipboard% %var1%
;if var1 in %clipboard%
IfInString, var1, %clipboard%
{
MsgBox 3rd %var1% %clipboard%
StringReplace, clipboard, clipboard, %var1%,, All
StringReplace, clipboard, clipboard, #,, all
MsgBox 4rd %var1% %clipboard%
var1 =
var1 :=
MsgBox 5th %var1% %clipboard%
}
else
{
MsgBox 6th %var1% %clipboard%
StringReplace, clipboard, clipboard, #, , all
MsgBox 6th %var1% %clipboard%
}
sleep 200
send !s
sleep 200
Send w
send %clipboard% {enter}
sleep 200
send !e s {enter}
#IfWinActive
return

I'm not exactly sure what you are looking for, but since you said you had problems adding a string to your variable, try this code, it will show you how to remove and add text to/from your variables:
var1 := "RP/0/RSP0/CPU0:,mnet-prd-hub"
MsgBox, %var1% `n`nClick OK to remove RP/0/RSP0/CPU0: from that string!
var1 := RemoveFromString(var1, "RP/0/RSP0/CPU0:")
MsgBox, %var1% `n`nClick OK to add mnet-prd-hub:/home/data/configs/current$/home/data/configs/current$ to that string!
var1 := AddToString(var1, "mnet-prd-hub:/home/data/configs/current$/home/data/configs/current$")
MsgBox, %var1% `n`nClick OK to remove mnet-prd-hub: from that string!
var1 := RemoveFromString(var1, "mnet-prd-hub:")
MsgBox, %var1%
RemoveFromString(string,stringToRemove) {
Return StrReplace(string, stringToRemove, "")
}
AddToString(string,stringToAdd) {
Return string stringToAdd
}
Edit:
So you want to see if the contents of var1 are to be found in the clipboard?
It can be done like this:
If InStr(Clipboard,var1) {
MsgBox, the contents of var1 were found in the clipboard.
}
Edit 2:
Like this?
If InStr(Clipboard,var1) {
Clipboard := RemoveFromString(Clipboard,var1)
}
RemoveFromString(string,stringToRemove) {
Return StrReplace(string, stringToRemove, "")
}

This works perfectly a genius helped me out on this.
Groupadd, TerminalWindows, ahk_class VTWin32
Groupadd, TerminalWindows, ahk_class PuTTY
SetTitleMatchMode, 2
#If WinActive("ahk_group TerminalWindows")
::ttwa::
uNames := "mnet-prd-hub:,RP/0/RSP0/CPU0:,RP/0/RSP1/CPU0:"
Clipboard= ; empty clipboard
sleep 200
send !e {enter}
send s
send {enter 100}
sleep 100
Send {click}
sleep 100
send {click}
sleep 100
send {click}
sleep 10
Loop, Parse, uNames, `,
clipboard := RegexReplace(clipboard, a_loopfield)
; msgbox % clipboard
if (WinActive("ahk_class VTWin32"))
{
sleep 200
send !s
sleep 200
Send w
send %clipboard% {enter}
sleep 200
send !e s {enter}
} else if (WinActive("ahk_class PuTTY"))
{
; steps for putty
}
#IfWinActive
return

Related

AHK: Select & append words

I would like to select non-adjacent words while holding down some shortcut key, append them (space-separated) to one single string and put the final, appended string into the clipboard.
Something like:
{Control_down}::
OnDoubleClick{ my_appended_string += Str(current_text_selection) }
{Control_up}
Clipboard := my_appended_string
return
Just so it works :-)
Can you help?
; LCtrl+DoubleClick on words in text
<^LButton Up::
if (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < DllCall("GetDoubleClickTime"))
{
; KeyWait, LCtrl ; after releasing LCtrl
clip_old := clipboard ; save the clipboard to the variable clip_old (as text))
clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
Click 2 ; select a word by doubleclick
Send, {Ctrl down}c{Ctrl up} ; copy the selected word
ClipWait 1 ; wait for the clipboard to contain data.
If (!ErrorLevel) ; If NOT ErrorLevel ClipWait found data on the clipboard
{
clip_new := clip_old A_Space clipboard ; append the new copied text to clip_old
clipboard := clip_new
}
else
{
MsgBox, No text copied
clipboard := clip_old
}
ToolTip, %clipboard%
Sleep, 1000
ToolTip
}
Return
EDIT:
Try also this
; Press F1 after putting the mouse pointer over the word to copy
F1 Up::
clip_old := clipboard ; save the clipboard to the variable clip_old (as text))
clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
Click ; on the word under the mouse pointer
Send, {Ctrl Down}{Left}{Ctrl Up}{Ctrl Down}{Shift Down}{Right}{Shift Up}{Ctrl Up} ; select the word
Sleep, 300
Send, {Ctrl down}c{Ctrl up} ; copy the selected word
ClipWait 1 ; wait for the clipboard to contain data.
If (!ErrorLevel) ; If NOT ErrorLevel ClipWait found data on the clipboard
{
clipboard := Trim(clipboard) ; remove leading/trailing spaces and tabs
clip_new := clip_old A_Space clipboard ; append the new copied text to clip_old
clipboard := clip_new
}
else
{
MsgBox, No text copied
clipboard := clip_old
}
ToolTip, %clipboard%
Sleep, 1000
ToolTip
Return

I have an AHK script to extract a folder using 7-zip. Why does it not work?

The AHK script below should use 7-zip to extract a folder when ctrl+ALT+Left is pressed. When you manually right-click on a folder and then type "7eee" and then press enter, the folder extracts. I'd like to mimic this without the right-click and instead use the keyboard shortcut. I tried to do this two ways:
;alt + ctrl
!^LButton::
blockinput on
send {LButton}{RButton}7eee{enter}
blockinput Off
return
I also tried:
;alt + ctrl
!^LButton::
temp = %clipboard%
KeyWait, LButton, D
send {LButton}
sleep,100
Send, {Ctrl Down}c{Ctrl Up}
file = %clipboard% ;get file address
clipboard = %temp% ;restore clipboard
outdir := getdir(file)
if (A_Is64bitOS = 1)
{
runwait, "C:\Program Files\7-Zip\7z.exe" x "%file%" -o"%outdir%" -y,,hide
}
else
{
runwait, "C:\Program Files (x86)\7-Zip\7z.exe" x "%file%" -o"%outdir%" -y,,hide
}
msgbox, 7zip has finished extracting "%file%".
return
getdir(input)
{
SplitPath, input,,parentdir,,filenoext
final = %parentdir%\%filenoext%
return final
}
EDIT:
I have found something that works:
#IfWinActive, AHK_EXE Explorer.exe
^e::
temp = %clipboard%
Send, {Ctrl Down}c{Ctrl Up}
file = %clipboard% ;get file address
clipboard = %temp% ;restore clipboard
outdir := getdir(file)
if (A_Is64bitOS = 1)
{
runwait, "C:\Program Files\7-Zip\7z.exe" x "%file%" -o"%outdir%" -y,,hide
}
else
{
runwait, "C:\Program Files (x86)\7-Zip\7z.exe" x "%file%" -o"%outdir%" -y,,hide
}
msgbox, 7zip has finished extracting "%file%".
return
getdir(input)
{
SplitPath, input,,parentdir,,filenoext
final = %parentdir%\%filenoext%
return final
}
#If
But I do not like the message box and I wish there were a progress bar or indication that it is in the process of extracting.
This is an old question and you're probably fine with the workaround you've found, but if you're still looking for a simple script that mimics the mouse/keyboard procedure, here it is:
^#!z:: ;// Ctrl + Alt + Win + Z
blockinput on
Sleep, 300
SendInput, {AppsKey}
Sleep, 100
SendInput, 7
Sleep, 100
SendInput, e
Sleep, 100
SendInput, e
Sleep, 100
SendInput, e
Sleep, 100
SendInput, {Enter}
blockinput on
Return
I did not try your code but in general, using the context menu key (AppKey) is more reliable than mouse button clicks, and also adding some sleep time between key strokes helps. If the script doesn't work, you may need to increase the sleep time intervals, 100 ms at a time, till it works.

How to check if clipboard is equal to a predetermined number, or several numbers in AHK?

; Copy cell
var := clipboard
sleep, 1000
WinActivate, doesntmatter - Internet Explorer
IfEqual 30684047, %var%
{
sleep, 500
Send, inform
}
else
{
msgbox, nope
}
return
My problem is that even though I have the correct number (30684047) in the clipboard, the code still goes straight to the MsgBox and tells me that the clipboard (%var%) isnt equal to the predetermined code.
What am i missing? I am 100% sure that %var% contains my copied code from the clipboard because if i do a MsgBox with %var% after i copy it, it gives me a box containing that correct code.
Look back at the documentation for IfEqual...you switched var and value.
; Copy cell
var := clipboard
sleep, 1000
WinActivate, doesntmatter - Internet Explorer
IfEqual, var, 30684047
{
sleep, 500
Send, inform
}
else
{
msgbox, nope
}
return
There's really no need to save off the value of clipboard to a different variable unless you're wanting to re-use that value after clipboard has later changed. So the above could also be:
WinActivate, doesntmatter - Internet Explorer
If (clipboard = "30684047") {
sleep, 500
SendInput, inform
}
else
msgbox, nope
return

Autohotkey if in clipboard

So I am trying to look for a specific text on the webpage and do a thing if the text was found, here is my current script:
!m::
clipboard =
text = my text here
Send, {Ctrl}+A
Sleep, 100
Send, {Ctrl}+C
var1 = %clipboard%
IfInString, var1, %text%
msgbox found the text
else
msgbox no text found
And regardless if the text is on the webpage or not, it always returns "no text found"
Any help on this?
P.S. I've also tried "if contains" and removing line breaks from the variable but the result is the same :(
StringReplace, var1, var1, `r `n, All
The send commands are not correct.
A command {Ctrl}+A, will press Ctrl, release it, and then press A. The lower case letter should also be used.
You should use either:
Send, {Ctrl down}{a}{Ctrl up}
or
Send, ^{a}
Do this for both Send commands.
A return commend should also be included as the end of the hotkey code
sequence:
...
else
msgbox no text found
return
Try this:
!m::
clipboard := "", MyText := "Hello World"
cmds := ["{Ctrl down}", "a", "c", "{ctrl up}"]
Loop % cmds.MaxIndex() {
Send % cmds[A_Index]
if (A_index == 2)
sleep 100
}
MsgBox % clipboard ~= "i)" MyText ? "Found" : "Not Found"

AutoHotKey - Bulk email filtering script not working fully

So I currently have the following code:
f2::
SendInput, ^c
RegExMatch(clipboard, "[\w-_.]+#(?:\w+(?::\d+)?\.){1,3}(?:\w+\.?){1,2}", email)
WinActivate, emailaddresses - Notepad
SendInput, %email%`r
return
It seems to be working, but I'm trying to get it so that if I highlight 5 email addresses it will send them each to a different line in the emailaddresses - Notepad window.
If you know the delimiter, you can use StringSplit and run your function over it.
from http://www.autohotkey.com/docs/commands/StringSplit.htm
f2::
SendInput, ^c
;put your delimiter here, assuming space/tab for now
StringSplit, txtArray, clipboard, %A_Tab%%A_Space%
Loop, %txtArray0%
{
copyemail(txtArray%a_index%)
}
return
copyemail(sTxt) {
FoundPos := RegExMatch(sTxt, "[\w-_.]+#(?:\w+(?::\d+)?\.){1,3}(?:\w+\.?){1,2}", email)
if (FoundPos > 0) {
WinActivate, emailaddresses - Notepad
SendInput, %email%`r
}
}
see sample above, not tested!