I'm trying to create a program that takes your highlighted text then inverses the case of it using a python script. The logic is essentially: get highlighted text into clipboard, do the transformation, then return the text to the clipboard and paste it.
I'm trying to execute this by creating a batch file that runs from an AutoHotKey hotkey, but the thing is the batch file has to run without popping up the cmd window because then it changes the window focus and it can't properly get the highlighted text.
The code works when I run it inside of PyCharm, and I can get the file to run from the hotkey, but the window still pops very briefly thus ruining the process.
The course I'm going through said to put @pyw instead of @py at in the batch file to make it run through python windowless, but still the window pops up very briefly. I can't seem to find a solution that works.
I tried to run it through a .vbs file but that didn't work the way I wanted it to. I checked to make sure pythonw.exe is working, and it is. I tried to change the file name to .pyw in the batch code.
What am I doing wrong? Is it with my python code, or my batch code? I don't know.
My Batch Code:
@pyw "C:\Users\offic\PycharmProjects\test\automate the boring stuff\projects\change case of selected.pyw" %*
My Python Code:
#! python 3
import pyperclip
import pyautogui as pya
import time
import sys
#mystery code I tried to make work as a solution
# import ctypes
# import os
# import pywin32_system32
#
# hwnd = ctypes.windll.kernel32.GetConsoleWindow()
# if hwnd != 0:
# ctypes.windll.user32.ShowWindow(hwnd, 0)
# ctypes.windll.kernel32.CloseHandle(hwnd)
# _, pid = pywin32.GetWindowThreadProcessId(hwnd)
# os.system('taskkill /PID ' + str(pid) + ' /f')
def copy_clipboard():
pyperclip.copy("") # <- This prevents last copy replacing current copy of null.
pya.hotkey('ctrl', 'c')
time.sleep(.01) # ctrl-c is usually very fast but your program may execute faster
return pyperclip.paste()
clipboard = copy_clipboard()
try: #check if there is an argument
# convert clipboard to uppercase if arg is 'u'
if sys.argv[1] is 'u':
pyperclip.copy(clipboard.upper())
# convert clipboard to lowercase if arg is 'l'
elif sys.argv[1] is 'l':
pyperclip.copy(clipboard.lower())
#else just swap the case of every character if the arg is not u or l
else:
pyperclip.copy(clipboard.swapcase())
#if there are no args just swap the case of every charater
except:
# put swapped string to clipboard
pyperclip.copy(clipboard.swapcase())
pya.hotkey('ctrl', 'v')
# hELLO
My AutoHotKey Code (if this somehow matters):
^+q::
Run, "C:\Users\offic\Documents\MY BATCH FILES\swapcase.bat"
return