1

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
user4157124
  • 2,809
  • 13
  • 27
  • 42
kloc.b
  • 51
  • 1
  • 8

2 Answers2

1
^+q::
Run, "C:\Users\offic\Documents\MY BATCH FILES\swapcase.bat",, Hide
return

AutoHotkey Run Options: Hide.

It would be the running of swapcase.bat causing the console window to show. Doubt it is pyw running pythonw.exe as both being Graphical User Interfaces (GUI) instead of a Console User Interface (CUI) that cmd.exe is, as it interprets swapcase.bat.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • this works for not making the window pop up! thanks! My code still doesn't actually work outside of pycharm for a reason I still need to figure out but this has solved the window problem, Thank you!!! – kloc.b Apr 14 '20 at 17:35
  • 1
    Suggest you change the shebang line in your Python code from `#! python 3` to `#! python3` as I get an error `C:\...\python.exe: can't open file '3': [Errno 2] No such file or directory`. `py` and `pyw` launcher will try to run `python` with argument of `3` which does not exist. PyCharm would not be reading the shebang so the script would work with it. – michael_heath Apr 15 '20 at 00:30
  • I'm now running into the problem that it can't find my module of 'pyperclip', I think because I am running it from PyCharm, and the instance of python that I use in my PyCharm project is different from the one that the cmd uses. I think I have to add that library to the one that the main python in my computer uses but it seems complicated. Thanks for your help! – kloc.b Apr 16 '20 at 18:18
  • 1
    In CMD, type `ftype Python.File`. I see `Python.File=py.exe "%L" %*`, so I can use the command `py -m pip install pyperclip`. If you get a full path from `ftype`, use the full path. If `python.exe` is in PATH (*check with command* `where python`), use `python -m pip install pyperclip` If Python is installed in a folder without modify permissions, then run [CMD as Admin](https://superuser.com/a/968225) initially. See [Installing Packages](https://packaging.python.org/tutorials/installing-packages/) documentation for help. – michael_heath Apr 17 '20 at 00:37
1

You can run your Python script (with .pyw extension) from AHK:

Run script.pyw, c:\mydir  

c:\mydir is the location of the Python script. This will work if Python is associated with .pyw extension of course.

Mikhail V
  • 1,416
  • 1
  • 14
  • 23