AHK: Select & append words - autohotkey

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

Related

AHK: Find text and Replace all

How do I replace the letters ve at the end of each word with the letters on. Please see the picture:I know that this word is not correct but it is an example only to clarify
Such a code sentence:
#IfWinActive ahk_class Chrome_WidgetWin_1
F2::
Clipboard := ""
Send, ^+{End}
Send, ^c
ClipWait
Clipboard := RegExReplace(Clipboard, "^(.*?)i(.*)", "$1o$2")
Send, ^v
return
Replace the
Clipboard := RegExReplace(Clipboard, "^(.*?)i(.*)", "$1o$2")
with
Clipboard := RegExReplace(Clipboard, "ve\b", "on")
The \b makes it match only the "ve" at the end of words, for example, it will change "vetvetve" to "vetveton"
Note that RegExReplace is case sensitive (it will not change "VETVETVE"), to make it case-insensitive use the i) option:
Clipboard := RegExReplace(Clipboard, "i)ve\b", "on")
You don't need a regex for this one. You can instead use a simple string replace
#IfWinActive ahk_class Chrome_WidgetWin_1
F2::
Clipboard := ""
Send, ^+{End}
Send, ^c
ClipWait
Clipboard := StrReplace(Clipboard, "ve", "on")
Send, ^v
return

AutoHotkey - Rename image by desired name only by selecting it and pressing shortcut

I have a quite organized workflow and I have an image that always needs to have the same name. It's always a PNG (Portable Network Graphics) and no matter if it's on Desktop or in a folder.
So i just want to select the image and with a one letter shortcut (for example "L") rename it (regardless it's previous name) to "LAYOUT"
F2::
ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
clipboard := "" ; empty clipboard
Send, ^c ; copy the selected file
ClipWait, 1 ; wait for the clipboard to contain data
if (!ErrorLevel) ; If NOT ErrorLevel clipwait found data on the clipboard
{
clipboard := clipboard ; convert to text (= copy the path of the selected file)
Sleep, 300
; MsgBox, %clipboard% ; display the path
if (SubStr(clipboard, -2) != "png")
{
MsgBox, No PNG-file selected
clipboard := ClipSaved
return
}
; otherwise:
SplitPath, clipboard, name, dir
FileMove, %clipboard%, %dir%\LAYOUT.png
Sleep, 100
clipboard := ClipSaved ; restore original clipboard
}
else
{
MsgBox, No file selected
clipboard := ClipSaved
}
return

AutoHotKey: Merge scripts that monitor selection in Firefox and other programs

I am struggling with a script that monitors selections by mouse in FireFox, Adobe Acrobat and one more program and then copies this selection to clipboard and changes it according to a regex. For each program another regex is needed. Each script works as a separate program, but when I merge them, the copied text is not changed according to my regex.
Script for Adobe Acrobat:
#ifWinActive ahk_class AcrobatSDIWindow
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 600 )
{
Send ^c
copied := true
}
return
OnClipboardChange:
if !copied
return
copied := false
ToolTip % Clipboard := RegExReplace(Clipboard, "\r\n", " ")
SetTimer, ToolTipOff, -1000
return
ToolTipOff:
ToolTip
return
And stript for Firefox:
#ifWinActive ahk_class MozillaWindowClass
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 600 )
{
Send ^c
copied := true
}
return
OnClipboardChange2:
if !copied
return
copied := false
ToolTip % Clipboard := RegExReplace(Clipboard, "[0-9]\.\s*|\s?\([^)]*\)|\.")
SetTimer, ToolTipOff1, -1000
return
ToolTipOff1:
ToolTip
return
The #If does only work on hotkeys, not on labels. Using OnClipboardChange seems unnecessary. When you press ctrl+c you already know that the clipboard changed.
I also really recommend setting indentations for hotkeys and also #If statements.
Here is how I would do it:
#If WinActive("ahk_class AcrobatSDIWindow") || WinActive("ahk_class MozillaWindowClass")
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 600 )
{
Send ^c
if WinActive("ahk_class AcrobatSDIWindow")
{
regex := "\r\n"
replace := " "
}
else if WinActive("ahk_class MozillaWindowClass")
{
regex := "[0-9]\.\s*|\s?\([^)]*\)|\."
replace := ""
}
ToolTip % Clipboard := RegExReplace(Clipboard, regex, replace)
SetTimer, ToolTipOff, -1000
}
return
#If
ToolTipOff:
ToolTip
return
(untested)
edit:
.....
~Left::
~Right::
~Up::
~Down::
now := A_TickCount
while GetKeyState("Shift", "P")
continue
if (A_TickCount-now > 600 )
{
oldShiftState := GetKeyState("Shift", "P")
Send, {Shift Up}
Send ^c
If (oldShiftState)
Send, {Shift Down}
.....
(untested)

Copy to clipboard the script path with AutoHotKey

I have tried this but it's not copying the path of the folder containing the script:
If(WinActive("ahk_class ExploreWClass"))
{ ClipSaved := ClipboardAll
; save original clipboard clipboard = ; clear
clipboard Send, "%A_WorkingDir%" ; copy selected folder
ClipWait, 2 ; wait until the clipboard contains data Sleep,
100 }
Thanks for your help!
P.S: For this, I am using AHK as a bat, not as a hotkey
ClipSaved := ClipboardAll ; Save the entire clipboard to the variable ClipSaved
clipboard = %A_ScriptDir%
; ClipWait
; do sth e.g, ; SendInput, ^v
Clipboard := ClipSaved ; Restore the original clipboard
https://autohotkey.com/docs/misc/Clipboard.htm#ClipboardAll

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