Autohotkey if in clipboard - autohotkey

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"

Related

Surrounding Clipboard with quotes in URL

Warning: I'm very new to this.
I'd like to make Google searches quicker by assigning a hotkey to search a text selection (in web browser) and surrounding that search with quotes (to get exact matches).
I've tried using bits of code I have found, but so far I can only search the selected text on Google, but don't know how to surround the selected text in quotes in the search.
^!d:: ;
prevClipboard := ClipboardAll
SendInput, ^c
ClipWait, 1
if !(ErrorLevel) {
Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "(^\s+|\s+$)")
If SubStr(ClipBoard,1,7)="http://"
Run, % Clipboard
else
Run, % "https://www.google.com/search?q=" Clipboard
}
Clipboard := prevClipboard
return
This simply opens a google search with the clipboard.
I cannot find out how to make it so "Clipboard" is searched instead of Clipboard.
Any suggestions? Thanks!
Run is a command and variables in commands have to be enclosed in percent signs.
To include an quote, specify two consecutive quotes twice:
Run, http://www.google.com/search?q=""%Clipboard%""
If you want to do a "Phase google search" from any Selected Text (And From Out Everywhere, on your Computer System.)
Then you can try this AHK Script.
you can [Select any Text] and then you can click the [F1] key from your Keyboard Device.
The Computer Find automatic out where it can do the Query Search (us 100x results) and what Browser it must use.
Example1.ahk
;#notrayicon
#SingleInstance force
GroupAdd, Browser, ahk_class Chrome_WidgetWin_1 ; Chrome or Iron
GroupAdd, Browser, ahk_class IEFrame ; Internet Explorer
GroupAdd, Browser, ahk_class MozillaWindowClass ; FireFox
GroupAdd, Browser, ahk_class ApplicationFrameWindow ; Edge
; here you can change the variable into exact search [
quote1 = "
; here you can change the variable into exact search ]
quote2 = "
; here you can change the variables intitle: - inurl: - inanchor: - allinurl: - allinanchor:
insearch = allintitle:
; + = Shift
; ! = Alt
; ^ = Ctrl
; # = Win (Windows logo key)
esc::exitapp ;You can click the (esc) key to stop the script.
f1::
If WinActive("ahk_group Browser")
{
sendinput ^c ;copy the selected text to clipboard memory
sleep 150
sendinput ^t ;CTRL+t make a new tab + goto address bar - use CTRL+L for the active tab + goto address bar
sleep 150
texta = https://www.google.com/?gfe_rd=cr&gws_rd=cr#q=%insearch%%quote1%
textb = %clipboard%%quote2% ;selected text
textc = &lr=lang_us&hl=us&num=100 ; google parameters
clipboard=%texta%%textb%%textc%
sleep 150
sendinput ^v ; paste the selected text
sleep 250
send {enter}
clipboard=%textb%
} else {
sendinput ^c ;copy the selected text to clipboard memory
sleep 150
texta = https://www.google.com/?gfe_rd=cr&gws_rd=cr#q=%insearch%%quote1%%quote1%
textb = %clipboard%%quote2%%quote2%%quote2% ;selected text
textc = &lr=lang_us&hl=us&num=100 ; google parameters
clipboard=%texta%%textb%%textc%
run %clipboard%
clipboard=%textb%
}
return

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

Join lines automatically using autohotkey

Long paragraphs in Adobe Acrobat results in a line break when text is copied to clipboard and pasted in Word.
To deal with this, I manually paste the copied-text in Notepad ++ > select all > ctrl+J (join lines) > select all > Copy...... and then paste it into my Word document.
I would like to automate this join line using autohotkey as I have a large body of documents to go through. I can't seem to find any examples online in dealing with this directly within autohotkey script.
Ex.
Data structures with labeled axes supporting automatic or explicit data alignment.
This prevents common errors resulting from misaligned data and working with
differently-indexed data coming from different sources.
After manually joining in Notepad ++
Data structures with labeled axes supporting automatic or explicit data alignment. This prevents common errors resulting from misaligned data and working with differently-indexed data coming from different sources.
This works:
#Persistent
OnClipboardChange("ClipChanged")
return
ClipChanged() {
If (WinActive("ahk_exe AcroRd32.exe")) {
For e, v in StrSplit(clipboard, "`n", "`r")
x .= v " "
clipboard := trim(x)
}
}
Try this:
#Persistent
return
OnClipboardChange:
If WinActive("ahk_exe AcroRd32.exe")
{
clipboard =
Send, {Ctrl down}c{Ctrl up}{Esc}
ClipWait
clip := RegExReplace(clipboard, "(\S.*?)\R(.*?\S)", "$1 $2") ; strip line breaks and replace them with spaces
clipboard = %clip%
StringReplace clipboard, clipboard, % " ", % " ", A ; replace double spaces with single spaces
ClipWait
ControlClick, x0 y0, A
}
return
EDIT
Or this:
#Persistent
return
OnClipboardChange:
If WinActive("ahk_class AcrobatSDIWindow")
{
clipboard := ""
Send, {Ctrl down}c{Ctrl up}{Esc}
ClipWait
clip := RegExReplace(clipboard, "(\S.*?)\R(.*?\S)", "$1 $2") ; strip line breaks and replace them with spaces
StringReplace clip, clip, % " ", % " ", A ; replace double spaces with single spaces
clipboard := ""
clipboard = %clip%
ClipWait 2
If !(ErrorLevel)
{
ToolTip, lines joined
Sleep 500
ToolTip
}
}
return
I added functionality considering lines ending with a hyphen.
Before:
This occurs in elec-
tricity generation
systems.
After:
This occurs in electricity generation systems.
Code:
#Persistent
return
OnClipboardChange:
If WinActive("ahk_exe AcroRd32.exe")
{
clipboard := ""
Send, {Ctrl down}c{Ctrl up}{Esc}
ClipWait
clip := RegExReplace(clipboard, "(\S.*?)-\R(.*?\S)", "$1$2") ; strip line breaks with hyphen
clip := RegExReplace(clip, "(\S.*?)\R(.*?\S)", "$1 $2") ; strip line breaks and replace them with spaces
StringReplace clip, clip, % " ", % " ", A ; replace double spaces with single spaces
clipboard := ""
clipboard = %clip%
ClipWait 2
If !(ErrorLevel)
{
ToolTip, lines joined
Sleep 500
ToolTip
}
}
return

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

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

SendEvent ^{ins} isn't copying content to the clipboard

!c::
file_name = footnote.ini
restore_original_clipBoard := clipboard
clipboard =
KeyWait, Alt
KeyWait, c ;small c
BlockInput, on
SendEvent, ^{ins} ;^c doesn't work
ClipWait, 2 ; Wait for the clipboard to contain text.
if ErrorLevel
{
MsgBox Failed to save the selection: %clipboard%
exit
}
BlockInput, off
save_selection := clipboard
Problem: Despite a selection being made, Sendevent ^{ins} does not save it to the clipboard. Sometimes I have to repeat my hotkey, alt + c several times before the selection is being copied to the clipboard. The KeyWait should ensure me that only ^{ins} is being processed without any additional keys. What am I doing wrong here?
UPDATE
One of the ways I tried to force copy a selection to the clipboard was by using a while loop. I got it to work through the post: Looping clipboard and errorlevel evaluation not working as expected
PROBLEM
When I make a selection and press alt + c it sometimes gets stuck in the infinite loop that I implemented. But as you can see from that code:
clipboard := ""
while( StrLen(clipboard) < 1 )
{
Send, ^{ins}
Sleep, 50
}
MsgBox % ClipBoard
The infinite loop incorporates within itself a continues resending of ^{ins}. For some reason, my selection is not being recognized as a selection. Whilst it is in that infinite loop, I try to reselect the text. It then recognizes it instantly and copies my selection to the clipboard. But alas! The selection is incomplete because it goes so quick.
This problem is not always like that. Sometimes it recognizes the selection first spot on! So sometimes it copies my selection to my clipboard sometimes not. When it does not, then a resending of a ^{ins} does not seem to work. I do not want to the user to reselect his selection. Is that possible to do?
Send {Ctrl Down}{c}{Ctrl Up}
That presses Ctrl+C, you must do it instantly as one command apposed to pressing Ctrl waiting then pressing C.
Never seen Insert key used for copying text.
Also found this sends Ctrl+C as well.
Send, ^c
To send insert key use
{Insert}
This way works for me:
!vk43:: ; alt+c
clipContent:=ClipboardAll
Clipboard:=""
SendEvent, ^{Ins}
ClipWait, .75
MsgBox, % 262 . (ErrorLevel ? 160:208)
, % ErrorLevel ? "Period expired:":"Result:"
, % ErrorLevel ? "Failed to save the selection.":Clipboard
, % (ErrorLevel ? 0:2) . .5
Clipboard:=clipContent
KeyWait, vk43
Return
!vk43:: ; alt+c
clipContent:=ClipboardAll ; backup clipboard content (if needed)
Clipboard:="" ; no comment :)
Loop
{
SendEvent, ^{Ins}
ClipWait, .75 ; means 750ms, same if write 0.75
; assign value of "ErrorLevel" an variable for further usage
errLvl:=ErrorLevel
; monitoring current action (for debugging purpose only)
TrayTip, % "attempt: #"A_Index
, % """ErrorLevel"" of ""ClipWait"" command is: "errLvl
}
; here you can set the condition of ending the cycle: either...
; ...variable errLvl has not a true value,...
; ...or the number of attempts is equal 5
Until, Not errLvl Or A_Index=5
; value of each field of the command "MsgBox"...
; ...are set depending on the value of errLvl variable...
; ...using a ternary expression
; means if errLvl is a true, "options" field is 262160
MsgBox, % 262 . (errLvl ? 160:208)
; means that "title" has a couple variants
, % errLvl ? "Period expired:":"Result:"
; means that "text" has a couple variants
, % errLvl ? "Failed to save the selection.":Clipboard
; means if errLvl is a true, "timeout" field is 0.5 (500ms)
, % (errLvl ? 0:2) . .5
/* same that and above:
IfEqual, errLvl, % True, MsgBox, 262160
, % "Period expired:"
, % "Failed to save the selection."
, 0.5
Else MsgBox, 262208, % "Result:", % Clipboard, 2.5
*/
TrayTip ; remove "TrayTip" (for debugging purpose only)
; save an positive result (if needed)
IfEqual, errLvl, 0, Sleep, -1, someVar:=Clipboard
; return a temporarily saved data into clipboard (if needed)
Clipboard:=clipContent
KeyWait, % "vk43"
Return
From my experience whenever keystrokes are not recognized reliably it's due to either the system or the targeted program not keeping up with the speed at which those keys are sent.
For SendEvent you could try something like SetKeyDelay, 1000, 1000 and see if this improves things. The other option would be to send explicit down and up keys with intermittent sleep calls as outlined in this answer.