Autohotkey - Hotstring not working after Tab Trigger - automation

I've written a Autohotkey script to auto-complete print statement in java
System.out.println("");
by clicking s and then Tab and jump to next line when cursor is between quotation marks by clicking Shift + Enter as follows
:*:s`t::System.out.println("");{left}{left}{left}
+Enter::
ClipSaved := ClipboardAll
Loop
{
clipboard =
Send, +{Right}
Send, ^c
ClipWait , 0.2
StringRight := InStr(Clipboard,OutputVar, 1)
If OutputVar = {;}
Send ^v
Send {Right}
Send {Right}
Send {Right}
Send {Enter}
break
}
clipboard := ClipSaved
Return
The problem here is it works good when I type s and then Tab and when clicking Shift + Enter jumps to new line. But if I type anything between quotations and then hit Shift + Enter it jumps to new line but then onwards the auto-complete is not working i.e., I'm not getting print statement when typing s and then Tab.
The image of error is attached for illustration. You can copy/paste the code and tell me where the error is, as I'm unable to figure it out.

If you want to trigger a hotstring after you typed something, you need to use the question mark option. Try this:
:*?:s`t::System.out.println("");{left}{left}{left}

Related

Run word macro on keypress in document

What I am trying to create is a writing assistant for MS word that gives me advice during writing. In order for that, I need to check whether the letter or number keys on a keyboard are pressed and if so, run a macro that shows the assistant popup and do the background work.
The problem is that I can't get the keypress detection to work. I tried multiple things like the two examples below but they don't give me the desired effect.
Private Sub document_KeyPress(KeyAscii As Integer)
If KeyAscii > -1 Then
MsgBox ("You Pressed a key")
End If
End Sub
Private Sub document_open()
'This line may differ depending on whether you are running this from a document template or an add-in.
Application.CustomizationContext = ThisDocument.AttachedTemplate
' Create the keybinding.
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeySpacebar), KeyCategory:=wdKeyCategoryMacro, Command:="MyMacro"
End Sub
The first one is not working at all and the seconds overwrites the keybinding which makes the spacebar in this example useless. With the second example, I also have to assign a binding for each character.
Any advice would be appreciated.
Since your question implies that you are doing this for yourself and not trying to deploy this macro to others, you can simply assign a keyboard shortcut to the macro from Word Options and you don't have to write custom keyboard trapping VBA to do this.
From Word's File tab select Options
Click Customize Ribbon
Click Keyboard shortcuts: Customize...
Select Macros from the All Commands Categories list
Locate your custom Macro and Assign a custom keyboard sequence
Via links that were shared in the post, I came across a program called AutoHotKey. After some research, I set up a system where AutoHotkey does the detecting part and then calls the macro in word. I use the following hotkey script:
Keybinding for a-x before this
y::
send y
StartLabel("a")
return
z::
send z
StartLabel("a")
return
Space::
send {Space}
StartLabel("a")
return
Backspace::
send {Backspace}
StartLabel("a")
return
^Backspace::
send ^{Backspace}
StartLabel("a")
return
StartLabel(x)
{
word:=ComObjActive("word.application")
word.run( "Writeass" ) <-- macro name. No need to define the module first
}
This script calls the macro every time one of de defined keys is pressed.
With a simple bat file I can start and close the script (to make sure it does not run in the background when wordt is not open).
Bat code to start the script:
start C:\path to Autohotkey script\WriteAssistant.ahk
Bat code to end the script (killing the autoHotKey process)
taskkill /im AutoHotkey.exe
I use the following vba script to start and close the hotkeyscript from a word macro (in my case I made a button in the ribbonbar to start the macro and the first thing it does is start the autoHotKey script):
Path = "C:\path\start.bat"
Shell Path
And best of all, I didn't notice any speed loss.

Move cursor to a new line after "Send Selection to Terminal" action in Geany

I set in geany a key shortcut to Send Selection to Terminal action which send the current line (or selection) to the build-in terminal (according to: shortcut to send selection to terminal in geany).
Then I change send_selection_unsafe=false to send_selection_unsafe=true in the geany.conf file (according to: Geany: execute line in Terminal)
Everything works well, I can send a line to terminal which is automatically executed. My problem is that at this point the cursor remains on the line after sending it to terminal.
Maybe I miss something, but is it possible to set or configure behaviour that cursor is automatically move to next line after using Send Selection to Terminal so than I can sending lines to terminal one after another (without hitting down arrow) ?
like this:
print("hello world!")
#I want cursor on this line after sending previous line to terminal, so this can be send to terminal immediately

Autohotkey: How to implement an exit sequence

In order to end my custom keyboard mapping for coding, I have placed the following line at the bottom of my script:
Esc::ExitApp
However, a single keystroke (especially the Esc) happens too much for other reasons. Therefore, I would prefer a sequence of single keystrokes (not holding down any key) to exit the Autohotkey mapping, such as "Esc and then LCtrl" or "Esc, LCtrl and then again Esc"
I have tried the idea from AutoHotKey key SEQUENCE, not just single-key hotkey :
Esc::
Input Key, L1
if Key=LCtrl
ExitApp
return
But it doesn't seem to do the trick: on pressing Esc and then LCtrl, the mapping doesn't stop. (Also, if I press Esc and then another key (not LCtrl) that next keystroke is ignored, something I would preferably not have.
Version 1a:
LCtrl & Esc::
ExitApp
return
You can add this code anywhere in your script. Press LCtrl and after Esc for script to exit.
Version 1b:
Esc & LCtrl::
ExitApp
return
You can add this code anywhere in your script. Press Esc and after LCtrl for script to exit.
You can add both Version 1a and Version 1b. In that case, when you press LCtrl and after Esc OR Esc and after LCtrl script will exit.
Version 2 (it uses different method, use it if for some reason you don't like Version 1a or/and Version 1b):
Loop
{
a := GetKeyState("Esc")
if (a=1)
{
b := GetKeyState("LCtrl")
if (b=1)
{
ExitApp
}
}
}
That code should be launch after your other code will remap all keys you need. Press LCtrl and after Esc OR Esc and after LCtrl for script to exit.
EDIT:
Version 3:
Loop
{
a := GetKeyState("Esc")
if (a=1)
{
Loop,60 ;number of 50 milliseconds script will wait to CTRL press. Example 60 means 60*50=3000, so script will wait 3000 milliseconds (1sec=1000 milliseconds) for CTRL press. After that time you have to press Esc again.
{
b := GetKeyState("LCtrl")
if (b=1)
{
ExitApp
}
Sleep, 50
}
}
}
That code should be launch after your other code will remap all keys you need. Press Esc (now you can release Esc ) and within 3000 milliseconds (1sec=1000 milliseconds) press LCtrl. After that time you have to press Esc again for script to exit. If you want to modify time after which you need to press Esc again read the comment in the code.
Also, always use AutoHotkey and its documenatation from http://ahkscript.org/ (current uptodate version, new official website)! AutoHotkey and its documentation from autohotkey.com is outdated and you may have some problems using them!

How to select printer and print with AutoHotKey

I am trying to automate export of Lotus Notes emails to Microsoft XPS documents so that I'll be able to search through them.
Now I would like to automate the export by using AutoHotKey to print and select 'Microsoft XPS document' in the printer name list.
I have the following script:
; F2 is my chosen HotKey that will trigger the script, starting with a CTRL-P
*F2::^p
; type 'm' to choose 'Microsoft XPS Document Printer'
Send m
{enter}
which opens the print view window, but does not select the printer, although manually typing 'm' works. I tried a sleep but did not work either.
First of all, your second command is never executed. When you place a command at the same line as your initiating code. Even when the second command was executed it could be too fast. try this:
F2:: ; Use the F2 key (I would use F1 as I never use the help key)
Send, ^p ; Send Control P to print
Sleep, 1000 ; wait 1 second (or less) for print dialoguebox
Send, m ; Select printer type
Sleep, 100 ; Wait 0.1 sec. before pressing enter
Send, {Enter} ; Press enter
Return ; End this command
Regards,
Robert Ilbrink

Autohotkey Win XP automating small task

I have a small task I would like to automate with Autohotkey and it looks like it is more or less directly transferable to autohotkey syntax:
1. Ctrl+v
2. Alt+tab
3. Click certain link in a window (no combo-key for this but it's always in the same place)
4. Enter (carriage return)
5. Alt+s
6. Ctrl+v
7. Enter
Now it would be nice to map this combo to something else e.g. Windows Key+Space.
What I have got so far is:
0. SetWinDelay 100 (using a connection to an remote computer)
0. SetKeyDelay 0
1. Send, ^c
1. ClipWait, 0.1
2. Send, {Alt down}{tab}
2. Send, {Alt up}
3. ?????
4. Send, {enter}
5. Send, !s
6. Send, ^v
7. Send, {enter}
Is this approximately right? Anyone up for helping me fix it or filling in the holes, so to speak :)
Another alternative to step 3, 4 and 6 would be to simply loop though the contents of the clipboard (a number string) and sending each letter of the string to keypresses? Maybe this would be the easier way
If you want to "click" on a certain position, to open a menu, you can first right click on your AutoHotKey icon and open the "window spy". This window spy will show you the mouse position. Yo can use the mouse positions to perform your actions in the active application.
Example:
SoundBeep 1000, 300 ; Wake up user
SplashTextOn, 200, 100, Script Preparations, Please Click on the person icon link. ; Show new Instructions text
WinMove, Script Preparations,, (A_ScreenWidth/2)+150, (A_ScreenHeight/2)+200 ; Move the window with the name "Script Preparations" Down and Right on the main screen
KeyWait, LButton, D ; Wait for LeftMouseButton click Down
MouseGetPos, xposE ,yposE ; Store the position where the mouse was clicked (Employee)
MouseClick, left, %xposE% ,%yposE%, 2 ; Perform a double mouse click on the captured mouse location
SplashTextOff ; Remove Text box
In this case, I first ask the user to manually click on the right location. This is only required when the position to click changes WITHIN the active window (variable tiles within the active window). Once you have the position stored, you can re-use it all throughout your script.
b.t.w. instead of using Alt+Tab, I suggest using this:
settitlematchmode, 1 ; Set search in title to start with....
settitlematchmode, Fast ; Slow is not required here. Slow is only required when hidden text needs to be found.
SwitchWindow("Microsoft Excel - 1 QRM Upload and Change Template") ; Activate the
window with the title: Microsoft Excel - 1 QRM Upload and Change Template
You could even use someting like this:
SetTitleMatchMode, 2 ; Ensure that the Title Match mode is set to 2: Find anywhere in the title
SetTitleMatchMode, Fast ; Ensure that the Title Match mode is set to FAST
winactivate, %WindowName% ; Activate the window with the title stored in the variable WindowName
WinWaitActive, %WindowName%, , 5 ; Wait up to five seconds for the screen
if ErrorLevel ; Execute this when the window is not activated within 5 seconds
{ ; Start-If Wait failed
SoundBeep 1000 , 1000 ; Warn the user
MsgBox,4097,Time Out, Script timed out while waiting for %WindowName%.`n`rYou Must manually activate %WindowName% and then continue the script by pressing OK. ; Message to user
IfMsgBox, Cancel ; Do when the user clicked on Cancel
{ ; Start-If User clicked Cancel
ExitApp ; Exit this program when the user clicked on Cancel
} ; End-If User clicked Cancel
WinWaitActive, %WindowName%, , 5 ; Try to activate the window AGAIN
if ErrorLevel ; If window can't be found
{ ; Start-If window can't be found
MsgBox,4096,Exit, %WindowName% still NOT Active. ; Warn user
ExitApp ; Exit this program when the expected window is still not found
} ; End-If window can't be found
} ; End-If Wait failed
Regards,
Robert Ilbrink