create shortcut in cmd prompt using autohotkey - autohotkey

I am using python and virutal environment in windows7. Every time I need to go to the project folder, shift+right click to open command prompt and activate virtualenv.
Instead I can hit win+R them type cmd to open a command prompt.
Then type
C:\cd D:\path\to\project
D:
workon projectEnvironment
Can this be done to create a shortchut like 'work' from autohotkey ??

Your windows-R shortcut would work like this:
Start a cmd window and wait until it is active.
Then begin to send your commands.
#r::
Run, %comspec% /c cmd.exe
SetTitleMatchMode, 2
WinWaitActive, cmd.exe
SendInput, cd D:\projects\folder{enter}
SendInput, D:{enter}
SendInput, workon projectEnvironment{enter}
return

Although I was unable to create shortcut key combination in cmd, I created a shortcut key, i.e. windows button + space
#space::Send cd D:\projects\folder{enter} D:{enter} workon projectEnvironment {enter}
Edit
here's the real solution
::work::cd D:\projects\folder{enter} D:{enter} workon projectEnvironment {enter}

After an hour of researching why my script would write " instead of \ when Sending a path in cmd prompt, I realized that sometimes the cmd prompt will interpret some keys differently for AutoHotkey.
I used to write Send, cd C:\File\Path {Enter} and it returned:
C:"File"Path
You can use ASC codes for / = {ASC 47} and \ = {ASC 92}, which in the end you write:
Send, cd C:{ASC 92}File{ASC 92}Path {Enter}

Related

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 put a "/" in AutoHotKey?

I am trying to write a AHK script that sends text to the command prompt. When I send "ipconfig /all" this is what shows up in the command prompt "ipconfig `all". I cant get it to put the "/" in. I have tried the COMSPEC and escape characters. But still cant figure it out.
Below is my current code.
^!d:: ; Ctrl, Alt, d
Run, C:\Users\Alex Chapman\AppData\Local\Microsoft\Windows\WinX\Group3\02
sleep 500
Clipboard = ipconfig /all
send, ^v
send, {Enter}
return
These 4 ways to do it work for me:
Normal: Send ipconfig /all
Escaped: Send ipconfig `/all
Raw: SendRaw ipconfig /all
and the workaround:
clipboard=ipconfig /all
Send ^v
If you want to preserve your clipboard using the workaround you can do the following:
cbbackup:=clipboard
clipboard=ipconfig /all
Send ^v
clipboard:=cbbackup
cbbackup=

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
}

What's the difference between Run command and commands in cmd?

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

Substitute AltTab in one key

Is it possible to substitute AltTab with only one key press ?
i tried this one but it doesn't work
`::AltTab
I use this, you may need to change the Sleep delay.
`::
Send {Alt Down}{Tab}
Sleep 100
Send {Alt Up}
return
I am running Windows 8.1 64-bit and AutoHotkey v1.1.16.05. And my C:\Program Files\AutoHotkey\AutoHotkeyU64.exe is digitally signed by running the script described here (EnableUIAccess.zip) so that Windows allows it to simulate Alt+Tab. The digital sign is required if you are using Windows Vista and onwards.
Download the zip file and extract it. Then run EnableUIAccess.ahk:
It will ask which AutoHotkey executable to sign. Pick one that you need (AutoHotkeyA32.exe, AutoHotkeyU32.exe, AutoHotkeyU64.exe or AutoHotkey.exe).
Then it will ask to save the new executable. You can choose to overwrite the original file or save as another executable.
Finally it will ask to create a "Run Script with UI Access" context menu item. If you choose 'Yes', then you can right-click a .ahk file and choose "Run Script with UI Access", which will use the digitally signed executable to run the .ahk file. But if you choose to overwrite the original file in step 2, then it is not necessary to create this context menu item.
From the docs:
Each Alt-Tab hotkey must be a combination of two keys, which is typically achieved via the ampersand symbol (&).
http://ahkscript.org/docs/Hotkeys.htm#AltTabDetail
You might even be able to avoid messing around with UIAccess and administrator privileges. This works for me on Windows 8.1:
`::Run "C:\Users\Default\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Window Switcher.lnk"
And for others who are struggling with getting a two-key combination working on Windows 8 or 10, here's an example using Ctrl-Tab to trigger the window switcher:
^Tab::
Run "C:\Users\Default\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Window Switcher.lnk"
SetTimer EnterOnKeyUp, -1
return
EnterOnKeyUp:
WinWaitActive ahk_class TaskSwitcherWnd
KeyWait Ctrl
Send {Enter}
SetTimer EnterOnKeyUp, Off
return
* Inspired by: Fully Working Alt Tab Win 8