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

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)

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

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

MATLAB auto completing start and end statements of code body?

Is it possible in MATLAB to automatically insert start and end of a code body ?
For example: classdef and end; function and end; methods and end.
classdef Circle
properties
radius
end
methods
function dia = FindDia(obj)
dia = [obj.radius]*2;
end
function %no automatic insertion of 'end'
end
end
Since I work regularly in so many different editors, I don't rely on any one editor's features. It's a pain to learn them all and keep the configuration for all these editors in sync, on all my machines. Often, it's useful to have the same feature set I'm used to, in editors not even meant for code (like MS Word, or here on Stack Overflow).
Therefore, I use AutoHotkey for this kind of thing (and Autokey on Linux).
For functions and classes, I use a paste function to paste a specific template file, depending on whether I'm at work or at home, and which project I'm working on. A small GUI then asks me for the function or class name, which it then populates the template with. I can share that too if you want.
Below are a few AutoHotkey functions and hotstrings I use for including the end keyword. Note that this might all seem overly complex just to put an end there, and in this case, it probably is. But the clipCopy, clipPaste and getIndent functions have proven their usefulness in the rest of my programming snippets, and I hope they might be for you too.
I've thrown in the error functions as well, just because.
; Copy selected text
; More robust than C-c/C-x/C-v; see
; https://autohotkey.com/board/topic/111817-robust-copy-and-paste-routine-function/
clipCopy(dontRestoreClipboard := 0)
{
prevClipboard := Clipboard
Clipboard := ""
copyKey := "vk43sc02E" ; Copy
SendInput, {Shift Down}{Shift Up}{Ctrl Down}{%copyKey% Down}
ClipWait, 0.25, 1
SendInput, {%copyKey% Up}{Ctrl Up}
str := Clipboard
if (dontRestoreClipboard == 0)
Clipboard := prevClipboard
return str
}
clipPaste(ByRef txt, dontBackupClipboard := 0)
{
if (txt != "")
{
prevClipboard := ""
pasteKey := "vk56sc02F" ; Paste
if (dontBackupClipboard == 0) {
prevClipboard := Clipboard
Clipboard := ""
}
Clipboard := txt
ClipWait, 1.00, 1
; Start pressing paste key
SendInput, {Shift Down}{Shift Up}{Ctrl Down}{%pasteKey% Down}
; Wait until clipboard is ready
startTime := A_TickCount
while (DllCall("GetOpenClipboardWindow") && (A_TickCount - startTime < 1000)) {
Sleep, 50
}
; Release paste key
SendInput, {%pasteKey% Up}{Ctrl Up}
; TODO: a manual delay still seems necessary...this vlaue needs to be this large, to also have
; correct behavior in superslow apps like MS Office, Outlook, etc. Sadly, the SetTimer approach
; does not seem to work (doesn't correctly restore the clipboard); to be investigated.
Sleep 333
; Put previous clipboard content back onto the clipboard
Clipboard := prevClipboard
}
}
; Get current indentation level in an editor-independent way
getIndent(dontRestoreClipboard := 0)
{
; Select text from cursor to start of line
SendInput, +{Home}
indent := clipCopy(dontRestoreClipboard)
numsp := 0
if (indent != "")
indent := RegExReplace(indent, ".", " ", numsp)
; Undo selection (this is tricky; different editors often have
; different behavior for Home/End keys while text is selected
SendInput, {Right}{Left}{Home}
; NOTE: don't use {End}, because we might be in the middle of a sentence
; Use the "right" key, repeatedly
Loop, %numsp% {
SendInput, {Right}
}
return indent
}
mBasic(str)
{
current_indent := getIndent()
SendInput, %str% (){Enter}+{Home}%current_indent%{Space 4}{Enter}+{Home}%current_indent%end{Up 2}{End}{Left}
}
mErrfcn(str)
{
current_indent := getIndent()
spaces := RegExReplace(str, ".", " ")
clipPaste(str . "([mfilename ':default_errorID'],...`r`n" . current_indent . spaces . " 'Default error string.');")
return current_indent
}
; MATLAB Hotstrings for basic control structures
:o:mfor::
mBasic("for")
return
:o:mpar::
mBasic("parfor")
return
:o:mwhile::
mBasic("while")
return
:o:spmd::
mBasic("spmd")
return
:o:mif::
mBasic("if")
return
; ...etc.
; error, warning, assert
:o:merr::
:o:merror::
mErrfcn("error")
SendInput, {Up}{End}{Left 21}
return
:o:mwarn::
:o:mwarning::
mErrfcn("warning")
SendInput, {Up}{End}{Left 21}
return
_mlAssert()
{
current_indent := mErrfcn("assert")
SendInput, {Up}{End}{Left 34}true,...{Enter}+{Home}%current_indent%{Space 7}{Up}{End}{Left 8}
}
:o:massert::
_mlAssert()
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

toggling a set of hotkeys with Autohotkey

I have toggle variable outside of #IfWinActive block but the variable is not initialized with the #IfWinActive block. How can I toggle a set of hotkeys?
toggle := false
f12::
toggle:=!toggle
SendInput {F12}
return
#IfWinActive ahk_class Chrome_WidgetWin_1
;toggle := false ;this doesn't do anything either
if (toggle) {
^a::SendInput {HOME}
}
#IfWinActive
You can try this code.
; [^ = Ctrl] [+ = Shift] [! = Alt] [# = Win]
#SingleInstance Force
Mode := 0
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
f12::
mode:=!mode ;not! toggle
return
#If mode ; All hotkeys below this line will only work, if mode is true and Browser is active.
^a::
If WinActive("ahk_group Browser")
{
SendInput {HOME}
} else {
send ^a
mode := 0
}
return
q::
If WinActive("ahk_group Browser")
{
send 1
} else {
send q
mode := 0
}
return
w::
If WinActive("ahk_group Browser")
{
send 2
} else {
send w
mode := 0
}
return
#If