Hotkey to run program or activate/toggle - autohotkey

I am fairly new to the autohotkey program but I love the functionality of it all.
I am looking to create a hotkey to open a program (if it's not already open) but it's open to activate the window.
I've tried the below but it doesn't seem to work.
^+e::
#IfWinExist, ahk_class XLMAIN ahk_exe EXCEL.EXE
{
WinActivate ahk_class XLMAIN ahk_exe EXCEL.EXE
}
return
#IfWinExist
#IfWinNotExist, ahk_class ahk_class XLMAIN ahk_exe EXCEL.EXE
{
run C:\Program Files\Microsoft Office\Office16\EXCEL.EXE
}
#IfWinNotExist
return
Also, could you confirm if mouse positions do not work on dual screen or windows 10?
Any help would be highly appreciated, thank you so much!

Three things:
I fixed the syntax and logic for the code. Please try to avoid using the #If statements (especially if you are new) as those tend to break scripts.
Don't use both ahk_class and ahk_exe at the same time for IfWinActive, as that would be redundant (and nonfunctional as far as I am aware).
Double check that the location of your EXCEL.EXE is where you say it is in your script (C:\Program Files\Microsoft Office\Office16\EXCEL.EXE), because on my computer it was located at C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE.
Here is the code that worked for me:
^+e::
IfWinExist, ahk_exe EXCEL.EXE
{
WinActivate ; Automatically uses the window found above.
WinMaximize ; same
Send, Some text.{Enter}
return
}else{
run C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE
}
return

Related

Controlsend to Visual Studio Code?

I have tried a number of things to make this work and all have failed:
ControlSend,, ^f, ahk_class Chrome_WidgetWin_1
ControlSend,, ^f, ahk_exe Code.exe
Both of the above failed to open the find bar in VSCode.
I had a similar issue once, when I was trying to use ControlSend with Firefox and adding ahk_parent worked then:
ControlSend, ahk_parent, ^f, ahk_exe Code.exe
ControlSend, ahk_parent, ^f, ahk_class Chrome_WidgetWin_1
Sadly, both of the above also failed to trigger anything.
I searched around but was not able to find anything for ControlSend with VSCode.
Has anyone had any success using ControlSend with VSCode?
Any help would be really wellcome
I tried ControlSending various commands to VSC. This seems to be a problem only to certain commands (another one is ^d), while other commands such as ^n work with no hitches.
Some things I noticed:
VSC must be activated (i.e. in focus) so ControlSend can work at all.
When debugging your script from VSC, sending only one ^f doesn't seem to work. However, funnily, when you send ^f twice, VSC always registers it.
Here's a "crude but it works" solution:
; Improves reliability - the script holds down Shift for 50 ms before sending f
SetKeyDelay,, 50
WinActivate, ahk_exe Code.exe
ControlSend,, ^f, ahk_exe Code.exe
ControlSend,, ^f, ahk_exe Code.exe
If you plan to launch your script as an .exe or outside VSC, then the second ControlSend can be omitted. But sending it twice doesn't cause any harm. The main drawback is that VSC will be activated (brought to front and then focused).

Photoshop picks up input before AHK

Few years ago a have write following script:
#IfWinActive ahk_class Photoshop
#Wheelup::
Send, {vkDD}
return
#IfWinActive ahk_class Photoshop
#WheelDown::
Send, {vkDB}
return
This script generate input '[' or ']' when I wheel mouse up or down while win-key is pressed. This script worked good, but now, when I have installed photoshop 2020, it's not working. I thought that ahk_class has been changed , but it's not the case. When I removed ifWinActive line, the script sent characters to the notepad, but not sent them to the photoshop. In addition, my other bingings, are not working while Photoshop is active too.
What should I do to solve this problem?
If Photoshop is running with admin privileges, then AHK won't intercept the key presses, and that could very well be the reason behind this problem.
If that is the case, try to run the AHK script as administrator by adding this to the auto-execute section (top of the script):
; If the script is not elevated, relaunch as administrator and kill current instance:
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try ; leads to having the script re-launching itself as administrator
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
For more details read https://autohotkey.com/docs/commands/Run.htm#RunAs.

ControleSend send weird output Autohotkey

I wrote a script that sends some keystroke to my command line. However, the values are coming out totally messed up. What could be the reasons?
BlockInput, on
WinGetActiveTitle, Title
DetectHiddenWindows, on
ControlClick, x660 y391, Notepad
ControlSend, , {Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}, Notepad
Sleep 30
ControlSend, , 2, Notepad
Sleep 30
ControlSend, , echo stat_id: 78 : SETTING PRICE TO 2 toactivate: %Title% >> logs.txt{Enter} , ahk_exe cmd.exe
ControlClick, x668 y640, Notepad
ControlSend, , echo ACTION NEXT (after SETTING PRICE) >> logs.txt{Enter} , ahk_exe cmd.exe
WinActivate, %Title%
Send {Ctrl down}+{Ctrl up}
Send {Ctrl down}+{Ctrl up}
Send {Ctrl down}+{Ctrl up}
Send {Ctrl down}+{Ctrl up}
Send {Ctrl down}+{Ctrl up}
BlockInput, off
ExitApp
Return
This is one example of output:
at-idec; ho78 B: EFSOETRTEI SNGCR PERENIC ESH TOOOT 2 DOTNOEAC >T>I valotges;. tixqt
echo ACTION NEXT (after SETTING PREICCEHO) >A>CT lioogn s.ntexxtt
Quite a few things to mention
1) You shouldn't be using controlsend to send text to a cmd prompt. Instead, you should be using Run, % Compsec. ComSpec is just a shortcut to cmd.exe. So when you do Run, % Compsec you're really just passing whatever comes next to the cmd prompt. Example for running ipconfig: Run, % ComSpec " /k ipconfig"
To easily remember which switch to use, remember that /k will "keep the command prompt open when it's done" and /c will "close it after the commands have run".
Also, make sure you understand the difference between Expression and non-expression.
The following lines of code look different, but each line does the exact same thing:
Run, % ComSpec " /k ipconfig"
Run, %ComSpec% /k ipconfig
2) Instead of writing {Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}, instead, include how many times you want the key repated after the key name but before the closing curly brace. {Backspace 6}. This works for any key.
3) BlockInput can be a dangerous command and, usually, I advise others to avoid it when possible. By using Run ComSpec instead of control send, you remove any need to use BlockInput.
4) The problem you were initially having most likely had to do with the text being sent too fast. Lower the send speed with SetKeyDelay.
Try replacing this:
ControlSend, , echo stat_id: 78 : SETTING PRICE TO 2 toactivate: %Title% >> logs.txt{Enter} , ahk_exe cmd.exe
With this:
Run, % ComSpec " /k echo stat_id: 78 : SETTING PRICE TO 2 toactivate: " Title " >> logs.txt"
If you do want to send to the console there are alternatives:
ConsoleSend() https://autohotkey.com/board/topic/25446-consolesend/
Windows 10: You could try pasting text and then send an Enter. Older versions of Windows - Requires additional software: I find Clink to be reliable as you can just Send ^v after installing it https://mridgers.github.io/clink/

How to write a script that creates hotkeys?

I have a lot of little hot keys defined, such as:
; Open CMD
#c::
Run, cmd.exe
WinWait, ahk_exe cmd.exe
WinActivate
Return
I'd like to build a function that takes the exe and hot key, and it will bind the app with that hot key. Here's what I have so far:
bind_exe_to_hotkey(exe,hotkey)
{
run_label:
Run, %exe%
WinWait, ahk %exe%
WinActivate
Return
HotKey, %hotkey%, run_label
}
bind_exe_to_hotkey("cmd.exe","#c")
However, this just opens a command window. What am I doing wrong? Is there an easier/better way to accomplish this?
Binds key to a function that handles launching an executable:
#c: launch("cmd.exe")
#n: launch("notepad.exe")
launch(exe)
{
Run, %exe%
WinWait, ahk %exe%
WinActivate
}

AutoHotKey run/activate SQL server management studio

Hi everyone I am struggling with AutoHotKey and simple key binding.
What I am trying to achieve is that if ssms is not running, start it, otherwise set focus/active this program. At the moment I have something like this:
+!s::
StringCaseSense, On
process, exist, Ssms.exe
{
If !errorLevel
Run "C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe"
else
IfWinExist Microsoft SQL Server Management Studio
WinActivate
}
This is working pretty fine until I create/open any SQL script in ssms. Any ideas how to fix this script?
This is a simplified version of what I use for all of my shortcuts. It uses a function (that can be reused). The first parameter is text in the window title. The second is the exe path.
SetTitleMatchMode, 2
+!s::ShowStart("Microsoft SQL Server Management Studio", "C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe")
ShowStart(title, exe)
{
IfWinExist, %title%
WinActivate
else
{
Run, %exe%,, UseErrorLevel
If ErrorLevel
{
Msgbox, Exe not found.
Return
}
WinActivate
}
}
That code should work for you:
+!s::
StringCaseSense, On
Process, Exist, Ssms.exe
if ErrorLevel
{
IfWinExist, Microsoft SQL Server Management Studio
{
WinActivate
}
}
else
{
Run, C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe
}
return
Here is also same code but for notepad.exe , which I have tested and it works fine:
+!s::
StringCaseSense, On
Process, Exist, notepad.exe
if ErrorLevel
{
IfWinExist, Untitled - Notepad
{
WinActivate
}
}
else
{
Run, c:\Windows\notepad.exe
}
return