A simple macro for Autohotkey program - autohotkey

I work as an audio transriber and I need a script for transcribing interviews. Unfortunately, I have no experience in creating scripts for Autohotkey, so I would like to get a ready-made solution, if that’s even possible. In any case, any help is welcome.
What should the script do?
We have an empty Microsoft Word document in front of us. The first time I press Enter, the following text should be pasted (note, that only “Interviewer: ” text should be pasted (with whitespace in the end), all the other text will be typed by myself):
Screenshot
Then, the next time I press Enter, the text of the following content should be pasted on a new line:
Screenshot
And as you probably already guessed, the next time I press Enter, the text “Interviewer: “ should be pasted again on a new line.
Thus, the contents of the pasted text should change alternately (Interviewer, Respondent, Interviewer, Respondent and so on):
Screenshot

To get familiar with AutoHotkey read the Beginner Tutorial.
Create a script and add this code to it:
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
#IfWinActive Word-Title ; replace Word-Title by the title of the word document
Enter::
toggle := !toggle ; changes the value of the variable toggle between 1 (true) and 0 (false)
if (toggle)
SendInput, {Enter}Interviewer:{space}
else
SendInput, {Enter}Respondent:{space}
return
#IfWinActive
EDIT
To automatically swap to uppercase the first letter of the text which you will be typing, try this:
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
#IfWinActive Word-Title ; replace Word-Title by the title of the word document
Enter::
toggle := !toggle ; changes the value of the variable toggle between 1 (true) and 0 (false)
if (toggle)
SendInput, {Enter}Interviewer:{space}
else
SendInput, {Enter}Respondent:{Space}
SetCapslockState ON
SetTimer, Set_CapslockState_OFF, -1
return
#IfWinActive
Set_CapslockState_OFF:
Input, Char, B I L1 V
SetCapslockState OFF
return

Related

AutoHotKey String substitution

I am writing a small AHK script that defines a few simple HotStrings.
The concept is that when I type "Build QA1", the appropriate text associated with that HotString appears.
No problem there ...quite simple ...the issue is that I wish to have the string associated with the HotString appear as part of the substitute text ...in the example below, the results should be
Build QA1
Now is the time for all good men
The script below accomplishes this and works fine ...it does exactly what I ask, HOWEVER, when I enter the HotString text and hit Enter or Tab or whatever initiates the HotString, that first line of text ( Build QA1 ) will "flash" on the screen as it is being substituted ...it makes it obvious that a HotString substitution is in operation ...
I would ideally like the HotString ( Build QA1 ) to remain as a part of the
substitute text without being replaced ...is this possible or is there a way to avoid the "flash" as that string is substituted ?
::Build QA1::
send, Build QA1
send, {enter}
send, Now is the time for all good men
return
Try it this way:
::b0::Build QA1:: ; automatic backspacing is not done to erase the abbreviation you type.
ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the new text has arrived
clipboard = ; copy this text:
(
Now is the time for all good men
)
ClipWait, 1 ; wait for the clipboard to contain data.
if (!ErrorLevel) ; if NOT ErrorLevel ClipWait found data on the clipboard
Send ^v ; paste the text
Sleep, 300 ; don't change clipboard while pasting! (Sleep > 0)
clipboard := ClipSaved ; restore original clipboard
VarSetCapacity(ClipSaved, 0) ; free the memory of the variable ClipSaved
return
See Hotstrings Options, Clipboard and ClipboardAll in the documentation.

How to make script to paste something in with AutoHotKey

I'm trying to make a script in AutoHotkey where, when I press Numpad 1, it presses the slash button, then pastes in some text, let's say "hello world", and then presses enter, but I can't figure out how. Can someone help?
Welcome to Stack Overflow.
In the future, try to at least show what you tried. All of this should be accomplished pretty easily by e.g. looking at the beginner tutorial combined with a quick Google search.
But well, here it is:
Numpad1::
Clipboard := "/hello word"
SendInput, ^v{Enter}
return
Numpad1:: creates the hotkey label.
Clipboard:= ... puts something into the clipboard.
SendInput sends input.
^v means Ctrl+v.
{Enter} means the enter key (could've possibly appended `n (line feed) into the string as well).
Return stops the hotkey label's code execution (in other words, ends the hotkey's code).
Assuming that you already have some text copied inside your clipboard before pressing the numpad1, the following code will work.
Numpad1::
Send, /^v ; ^ means ctrl key,
Send, {Enter}
return

How do I delete the current line using AutoHotkey?

Using an AutoHotkey script I'd like to set the keyboard command Ctrl+D to delete the current line in any active Windows app.
How?
^d::Send {Home}{ShiftDown}{End}{Right}{ShiftUp}{Del}
Might not work in all edge cases, but passes some very basic testing in Notepad. =~)
HaveSpacesuit's answer works but after using it for a while I realized it deletes the active line and sometimes re-positions the spacing of the line below.
This led me to rethink his solution. Instead of going from the front of the line to the back, I tried going from back to front. This solved the re-positioning issue.
SendInput {End}
SendInput +{Home}
SendInput ^+{Left}
SendInput {Delete}
There is still a small problem though. If the cursor is on an empty line, with more empty lines above, then all empty lines get deleted.
I don't know a key combo to replace ^+{Left} that doesn't have this behavior so I had to write a more comprehensive solution.
^d:: DeleteCurrentLine()
DeleteCurrentLine() {
SendInput {End}
SendInput +{Home}
If get_SelectedText() = "" {
; On an empty line.
SendInput {Delete}
} Else {
SendInput ^+{Left}
SendInput {Delete}
}
}
get_SelectedText() {
; See if selection can be captured without using the clipboard.
WinActive("A")
ControlGetFocus ctrl
ControlGet selectedText, Selected,, %ctrl%
;If not, use the clipboard as a fallback.
If (selectedText = "") {
originalClipboard := ClipboardAll ; Store current clipboard.
Clipboard := ""
SendInput ^c
ClipWait .2
selectedText := ClipBoard
ClipBoard := originalClipboard
}
Return selectedText
}
As far as I can tell this produces no unexpected behaviour.
However, be careful if you're using a clipboard manager as this script uses the clipboard, if necessary, as an intermediary to get the selected text. This will impact clipboard manager history.
In case you run into problems where you need different behaviours for different programs, you can "duplicate" your ^d command for specific programs like this:
SetTitleMatchMode, 2 ; Makes the #IfWinActive name searching flexible
^d::Send {Home}{ShiftDown}{End}{Right}{ShiftUp}{Del} ; Generic response to ^d.
#IfWinActive, Gmail ; Gmail specific response
^d::Send {Home}{ShiftDown}{End}{Right}{ShiftUp}{Del} ; adapt this line for gmail
#IfWinActive ; End of Gmail's specific response to ^d
#IfWinActive, Excel ; Excel specific response.
^d::Send {Home}{ShiftDown}{End}{Right}{ShiftUp}{Del} ; adapt this line for Excel
#IfWinActive ; End of Excel's specific response to ^d
This way your ^d command will work differently in Excel and Gmail.
I have a simple way to solve the repositioning issue. Without using the clipboard.
The repositioning issue is due to the need to handle 2 separate cases.
if there's existing text in a line,
we want to select them all, and delete the text (backspace 1)
and backspace one more time to delete the empty line (backspace 2)
if it's a blank line,
we want to delete the empty line (backspace 1)
To cater for both of above cases, I introduced a dummy character.
This will make sure BOTH cases will act the same way.
So doing backspace 2 times, will result in the same transformation each time.
Simply,
; enable delete line shortcut
^d::
Send {Home}
Send {Shift Down}{End}{Shift Up}
Send d
Send {Backspace 2}
Send {down}
return
Disadvantage with this approach,
the dummy character "d" will appear when you undo. Not a bad tradeoff since I don't undo delete lines very often.

Insert Tab character into Word

I'm trying to create a script, which will insert Tab character into Word.
Quick note: I've also tested it in OpenOffice. So if you haven't Word, you may test it in OpenOffice or, probably, LibreOffice Writer.
If you are familiar with Word, you know that if you press Tab key on a blank line, you get the Word-like indentation, instead of inserting real Tab char.
Here is attempt to fix it:
$Tab::
old := ClipboardAll
Sleep, 1000 ; Just for testing. If I remove this line, the error still
; occurs, but much more randomly.
Clipboard := " " ; Tab character
ClipWait
SendInput, ^v
KeyWait, Tab
Clipboard := old
return
The problem is, that sometimes (when I press Tab quickly), it is inserted an old content of the clipboard, instead of Tab.
I've tried to use ClipWait, KeyWait, Sleep, InstallKeybdHook in different combinations.
Maybe someone knows what's the problem here and how it can be solved?
Sends tab without triggering indent in word
SetTitleMatchMode 2
#IfWinActive Microsoft Word
$tab:: sendinput .{tab}{left}{backspace}{right}
If I'm understanding you correctly, a better solution might be to turn off this feature with Options > Proofing > Autoformat as you type > Set left and first indent with tabs and backspaces (see here)

Autohotkey - Undo entire macro instead of single commands (Windows 7)

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