0
votes

I'm trying to call a Python code from AHK for processing the YouTube transcript on my clipboard, remove those time stamps, fuse them back into one string, and then substitute the original texts with the new processed string so I can paste it out.

AHK code:

^x::

clipboard =   
Send, ^c

Run "directory\try.py"

Return

Python code (the try.py):

import pyperclip 

content = pyperclip.paste()
lines = content.split('\r\n')

new_lines = []
for line in lines: 
    for i,x in enumerate(line):
        if x.isalpha():
            position = i 
            break 
    new_line = line[position:]
    new_lines.append(new_line)

# print('Preview', '\n', ' '.join(new_lines))
pyperclip.copy(' '.join(new_lines))

Sometimes this system works, but sometimes it doesn't. Sometimes, when it didn't work, if I went back to the YouTube page and pressed ctrl + x again, it worked. I'm pretty sure the issue is in the AHK part, since I have manually been using the Python code for months without any error. Thanks to anyone can help.

1

1 Answers

3
votes

Yes. The AHK is too fast. That clipboard stuff takes time. Check it out. This is how to do it:

; Using ClipWait to improve script reliability:
clipboard =  ; Start off empty to allow ClipWait to detect change
Send, ^c
ClipWait ; Wait for the clipboard to contain text.
Run "directory\try.py"

You might even have to add some sleep times:

; Using ClipWait to improve script reliability:
clipboard =  ; Start off empty to allow ClipWait to detect change
Sleep, 50 ; milliseconds
Send, ^c
ClipWait ; Wait for the clipboard to contain text.
Sleep, 150 ; milliseconds
Run "directory\try.py"

Or better yet, try like so (using OnClipboardChange function):

OnClipboardChange("ClipChanged")
return

^x::
    Send, ^c
return

ClipChanged(Type) {
    MsgBox "%Clipboard%"  ;  comment out if working well
    run "directory\try.py"
    ExitApp
}

You can comment out the ending ExitApp but then not only will ^x trigger it, but everytime the clipboard changes (so if you hit control+c yourself) and you will want some way to exit the command, such as ^{esc}::ExitApp or the like.

Hth!!