0

Full Disclaimer: I DO NOT KNOW PYTHON.

Hi Guys,
I have made an AutoHotKey Script for my volume keys. I would like to create a batch file which runs a python file (so if I change computers, I can easily create this scripts) which would do the following

  1. Check if volume_keys.ahk exists in the D Drive
  2. If it exists, run that;
  3. If it doesn't exist, then create a file named volume_keys.ahk and add my script to it.

My script is:

^!NumpadMult::Send  {Volume_Mute}
^!NumpadAdd::Send   {Volume_Up}
^!NumpadSub::Send   {Volume_Down} 

I know how to code the .bat file and just need help for the python point-of-view, but I request the community to check it:

@ECHO OFF
ECHO This script will run an AHK Script. If you want to stop this process from happening, then cross this window off.If you want to continye:
pause
cd d:
D:\run_volume_keys_ahk_script.py

I really appreciate any help by the community.
Thanks in advance

2 Answers2

0

You can use the os library for this. Here's what the python program would look like.

import os

if os.path.isfile('D:\\volume_keys.ahk'): # check if it exists
    os.system('D:\\volume_keys.ahk') # execute it
else:
    with open('D:\\volume_keys.ahk', 'w') as f: # open it in w (write) mode
        f.write('^!NumpadMult::Send  {Volume_Mute} \
^!NumpadAdd::Send   {Volume_Up} \
^!NumpadSub::Send   {Volume_Down}') # Write to file
    os.system('D:\\volume_keys.ahk') # execute
0

To activate the ahk script, you might want to use the subprocess module, of which I took the example from here

import subprocess
subprocess.call(["path/to/ahk.exe", "script.ahk"])

Note that you'll have to find the ahk executable on a computer before you can use the script, maybe you want to automatically check that too.

You can set the path you want to check for scripts in one string, and then add the filenames of your scripts as strings to a list. You can use listdir() from the os module to see any files and directories at a given path, then iterate over your scriptnames and check if it exists in that list of files. If it does, run it.

In this example I copy-pasted your script into a string as value for the key 'scriptname' in a dictionary, so that python can actually create the script file. This isn't really a neat way to do it though, you might want to have your scripts prepared in a directory next to your python script and copy them from there. See an example of how here

from os import listdir
from os.path import isfile, join

CHECK_PATH = "D:"
AHK_EXECUTABLE_PATH = "path/to/ahk.exe"
SCRIPTS_TO_CHECK = {'script1.ahk':"""^!NumpadMult::Send  {Volume_Mute}
^!NumpadAdd::Send   {Volume_Up}
^!NumpadSub::Send   {Volume_Down} """, 'script2.ahk':" some other script here"}

files_to_check = set(listdir(CHECK_PATH)) # using a set for fast lookup later

for scriptname, script in SCRIPTS_TO_CHECK.items():
    if not scriptname in files_to_check:
        print(f"script {scriptname} not found, creating it.")
        with open(scriptname, 'w') as file:
            file.write(script)
    # else
    subprocess.call(AHK_EXECUTABLE_PATH, scriptname)

Queuebee
  • 651
  • 1
  • 6
  • 24