Categories
Programming Python

Run Python Scripts using AutoHotkey and Pass Arguments

So you have a python script that has some code like:

import argparse

# GET ORDER ID
parser = argparse.ArgumentParser(description='For blog.codeonion.com my visitors!')
parser.add_argument("--argument_to_be_passed", required=True, type=str, help="Please pass a value into --argument_to_be_passed")
args = parser.parse_args()

value = args.argument_to_be_passed
print(value)

Now, if we run this script, this is the output:

Execution without passing arguments

But if we pass arguments manually:

Arguments are getting passed manually and then printed on command line.

So you see, it is working. But this is slow and boring.

What if you wanted to automate running a python script in order to QUICKLY test something and development purpose!

I know from experience the process of ALT+TAB into a CMD and then pressing UP arrow and press enter is a repetitive labour. Lets reward ourselves with Autohotkey’s automation and buy some extra minutes into the already tightened day.

ENTER THE AUTOHOTKEY!

Once you have installed AutoHotkey, it is time write this script:

; Autoexecute
    #NoEnv
    #SingleInstance force
return
#^space::
    run cmd.exe
    WinWait, ahk_exe cmd.exe ; Wait for CMD to start
    Send c:{enter} ; Go to C drive
    Send cd C:\python\blog\{enter} ; go to script's folder
    Send python arguments.py --argument_to_be_passed CODEONION{enter}
return

Make sure the path to python script is correct. Run this AutoHotkey script file and then, use CTRL+WINDOWSKEY+SPACE to run the macro. And enjoy the spam show!

See it in action!

https://gist.github.com/codeonion/a348f3cc3e580347266fec8f1de6c0d4

Get code from my Github Gist

I really hope this made your day! Please commend and share if oyu are feeling generous. Thanks! and have a nice day!