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
Related
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
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.
I've encountered a problem I am having troubles to formulate, therefore I can't google it properly.
I have a script:
WheelLeft::
Run, "C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE", "C:\Program Files\Microsoft Office\root\Office16"
Return
+XButton1::
Send, !{Right}
Return
WheelRight::
Run, "C:\Users\pavel\AppData\Local\GPMDP_3\app-4.4.0\Google Play Music Desktop Player.exe"
Return
which works fine, but the RUN commands do not start from everywhere. For instance, it does not start when I am using Chrome, but works fine from FileExplorer.
Could anybody please point me to a resource about this kind of problem?
Thanks in advance
Assuming AutoHotkey is up to date, I'd suspect it's simply Chrome hijacking your forward/back commands or fighting with AHK over it.
Before delving in further, try a tilde, which will pass through the command instead of consuming it after AHK acts on it.
~WheelLeft::
Run, Notepad.exe
Return
~WheelRight::
Run, Notepad.exe
Return
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
}
I have the following script:
^!c::
Run stop
Return
Stop is configured to run a program via environment variables.
So if I open up cmd and type “stop” and hit enter the program opens as intended, even if I push winkey + R it does the same thing. However if I use the script with ctrl+alt+c. I do not get the same result.
Why is the script doing something different?
How can I change my script to behave the same way as if it was typed into cmd or winkey + R?
Simple:
run, %comspec% /c stop
Or if this doesn't work you could just start a cmd window and send it directly
run, %comspec% /k
WinWait, %comspec%
WinActivate
Send stop{Enter}
/c tells the console window to close after execution, /k lets it stay open
or you could use an COM object and even get the output.
objShell := ComObjCreate("WScript.Shell")
objExec := objShell.Exec(ComSpec " /c stop")
strStdOut := ""
while, !objExec.StdOut.AtEndOfStream
{
strStdOut := objExec.StdOut.ReadAll()
}
Update:
Without the run command at all:
SetTitleMatchMode, 2
send #r
WinWait, TITLE_OF_THE_RUN_WINDOW
WinActivate
send cmd{Enter}
WinWait, cmd.exe
WinActivate
WinGetTitle, title
Send stop{Enter}
WinWait, %title%,,, stop
WinClose,
TITLE_OF_THE_RUN_WINDOW replace this with the title of the window, which opens on Win+r. A windows cmd window has the command in its title while it gets executed. So we save the title of the command window, and wait for it to drop the command ("stop") and close it then.
UPDATE: Cmd window close added to solution 4