I'm new to Autohotkey, tried to make a macro to:
Copy selection
Paste it in Notepad++
Playback Notepad++ macro (for formatting) shortcut: Ctrl+Shift+B
Copy all the edited text
Paste in Firefox text field
I tried starting with the following code, but I couldn't even get AHK to copy paste my selection to Notepad++.
^!x::
Send, ^c
ClipWait
IfWinExist, Notepad++
{
WinActivate
Send ^v
}
Here is working script:
^!x::
Send, ^c
ClipWait
SetTitleMatchMode, 2
IfWinExist, Notepad
{
WinActivate
Send, ^v
}
return
I added command SetTitleMatchMode, 2 as Joe DF mentioned in comments. That command (link) with parameter 2 sets the matching behavior of IfWinExist command so that a window's title can contain WinTitle anywhere inside it to be a match. Also added return in the end.
Related
The AutoHotkey Beginner Tutorial includes a sample script to demonstrate window specific hotkeys and hotstrings.
#IfWinActive ahk_class Notepad
::msg::You typed msg in Notepad
#IfWinActive
#IfWinActive untitled - Paint
::msg::You typed msg in MSPaint!
#IfWinActive
The second example is fairly self-explanatory: Check for a window named "untitled - Paint". It's the first example's use of ahk_class that has me confused.
I haven't been able to find any explanation of the variable in the AHK documentation. According to an AHK forum post, ahk_class is the name of a window as given by Windows Spy, the post didn't go into any more detail.
Would there be any difference between using ahk_class Notepad and Untitled - Notepad? Would the second example work if replaced with #IfWinActive ahk_class Paint?
What is ahk_class and how can I use it for window matching?
From https://autohotkey.com/docs/misc/WinTitle.htm#ahk_class
A window class is a set of attributes that the system uses as a template to create a window. In other words, the class name of the window identifies what type of window it is.
In other words you can use it to identify windows of the same type, if you open notepad the title will be Untitled - Notepad if you save it to temp.txt the title will become temp - Notepad. ahk_class on the other hand will always remain Notepad.
The second example will work if you replace it with #IfWinActive ahk_class MSPaintApp because that's the class of mspaint.
Usually you find ahk_class using Window Spy and then use it in your scripts. If you don't have Window Spy you can use the following hotkey:
#1::WinGetClass, Clipboard, A ; Will copy the ahk_class of the Active Window to clipboard
After you found it you can use it in any place you can use window title for example instead of writing WinActivate, Untitled - Notepad you can write WinActivate, ahk_class Notepad.
Check this article. Ahk_class is the class that is given to you when you use the WindowSpy tool. This tool should be in the same folder as your AutoHotkey executable.
How shouuld I tweak the following script so that the hotkey is activated not at ANY moment where I'm at the class shown (that is Outlook) but at a specific sub window ( the preview pane of the Inbox box (whose classNN is _WwG1 )) ?
#IfWinActive ahk_class rctrl_renwnd32
+!m::
ControlFocus, OutlookGrid1, ahk_class rctrl_renwnd32
if ErrorLevel ; i.e. it's not blank or zero.
MsgBox, You don't seem to be in context.
return
#IfWinActive
Make the hotkey look for active controls once it is activated. This way you can use the same hotkey for multiple commands, each command depending on the control. You can do this with several if/else statements to test for the subcontrols.
The hotkey only works in Outlook.
Each control has its own command
Each command is limited to that particular control
#ifwinactive, ahk_exe outlook.exe
{
+!m::
controlgetfocus, thiscontrol
if(thiscontrol = "_Wwg1"){
ControlFocus, OutlookGrid1, ahk_class rctrl_renwnd32
if ErrorLevel ; i.e. it's not blank or zero.
MsgBox, You seem to focused on %thiscontrol%
}else if(thiscontrol = "_Wsg2){
msgbox, you've discovered the second control!
}
return
}
I am using the following code:
#IfWinActive ahk_class Notepad
4::Send,4
5::
Hotkey,4,off
;~ do something
Hotkey,4,on
Return
#IfWinActive
but it generates this error:
Prompt Error: Nonexistent hotkey variant (IfWin).
I don't understand what you mean by using 4 to send 4 - it seems redundant.
I tried it like this.
pushing 4 will send 6, and pushing 5 will send 4.
#IfWinActive ahk_class Notepad
{
4::Send,6
5::
Hotkey,4,off
send, 4
Hotkey,4,on
Return
}
The idea here is that the brackets encase the entire function within the IfWinActive. If notepad isn't active, nothing at all happens.
I am mapping Alt+F4 to ESC so that I can close windows just by pressing escape. However there are two windows that I need to actually use ESC in. So, when either of these two windows are active I want to be able to press ESC without closing the window. What is the easiest way to accomplish this? I have my script working when I just am excluding one active window but I need to work when either of the two windows are active.
Here is my attempted code:
GroupAdd, ESC, Untitled - Notepad
GroupAdd, ESC,
#IfWinNotActive, ahk_group, ESC
Escape::!F4
Return
This is the code that works properly with just one window:
;#IfWinNotActive, Untitled - Notepad
;Escape::!F4
;Return
UPDATE: Should this work?
SetTitleMatchMode, 2
SetTitleMatchMode, 2
#IfWinNotActive, Untitled - Notepad
#IfWinNotActive, Document 1 - Microsoft Word
Escape::!F4
Return
You have an extra comma in your #IfWinNotActive line after ahk_group
Try the following:
GroupAdd, ESC, Untitled - Notepad
; Add more windows to the group if necessary
#IfWinNotActive, ahk_group ESC
Escape::!F4
Return
Try using SetTitleMatchMode.
http://www.autohotkey.com/docs/commands/SetTitleMatchMode.htm
2: A window's title can contain WinTitle anywhere inside it to be a
match.
Then you can do this (it is case-sensitive by default):
settitlematchmode, 2
#IfWinNotActive, Untitled -
Try this:
First, you can't remap the same key twice in the same script.
This command affects the behavior of all windowing commands, e.g.
IfWinExist and WinActivate.
Second, you stack lines like this:
#IfWinNotActive, Untitled - Notepad
#IfWinNotActive, Document 1 - Microsoft Word
you are saying, if win1 is not active, THEN if win2 is not active.
Try this, instead:
settitlematchmode, 2
app1 := winexist("other app")
app2 := winexist("Untitled - Notepad")
if(app1 || app2) ;the || means OR. you can use && for AND.
Escape::!F4 ;you can only map a particular key one time per script
or this, which is more along the lines of your approach:
settitlematchmode, 2
GroupAdd, ESC, Untitled - Notepad
GroupAdd, ESC, My other window
IfWinNotActive, ahk_group ESC
Escape::!F4
Return
This worked for me:
1: write this at the top of your script (before the first hotkey):
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetTitleMatchMode, Regex
2: Then you can use the not operator !:
if !(WinActive("ahk_class Notepad") or WinActive("ahk_class Calculator")) {
...
}
Credit
I tried searching for an answer beforehand but didn't find what I was looking for. Apologies in advance if this has been answered before.
I do some web work and created a macro in AHK that binds Ctrl+Shift+B to the HTML equivalent of adding bold tags around a text selection.
The flow is:
cut (ctrl+x), type <b>, paste cut text (ctrl+v), type </b>.
The macro runs fine, but sometimes I want to undo it. However, whenever I press Undo (ctrl+z), I'm left pressing the command 4 times, with each press reverting 1 of the commands posted above.
Is there a better way to write my AHK macro so that I'm able to undo the entire macro in 1 keypress? Any tips would be great. For Windows 7 if that makes a difference.
I've added the macro below.
^+b::
{
SendInput ^x
SendInput <b>
SendInput ^v
SendInput </b>
return
}
Edit: & #60; is the hmtl equiv on '<', but I was worried that this post would convert the HTML tags instead of showing the characters. Fixed.
Sorry about that, I tend to use a combination of notepad, notepad++, internet explorer to access a CMS. –
I think adding a delay and rewriting the AHK macro in the following way has solved my issue. Thanks for the help!
^+b::
clipboard =
SendInput ^x
ClipWait,1
if ErrorLevel
{
MsgBox, The attempt to copy text onto the clipboard failed.
return
}
SendInput < b >%clipboard% < /b>
return
Try this instead:
^+b::
{
Clipboard =
SendInput ^c
ClipWait, 1
Clipboard = <b>%Clipboard%</b>
SendInput ^v
return
}
Because the only thing you are doing is a paste, undoing this will undo both of the bold tag. The clipboard edits are not registered as an "undo" action.
Here is some code:
^+b::
Click, 2 ; Highlight current word
Send, ^x
ClipWait, 1 ; ADDED to wait for clipboard
SendInput, <b>^v<`/b>
Return
!b::
Send, ^{z 4}
Return
or
!b::
Send, "command to search backwards" for </b>
Send, {Del}
Send, "command to search backwards" for <b>
Send, {Del}
Return
or
!b::
Send, {Home}+{end} ; [Home] then [Shift][End] to highlight current line
Send, ^h ; Or any other command to start find/replace
Send, <b>^v<`/b>{Tab}^v{Enter} ; or what is required to replace in current section only...
Return