AutoHotKey 调用 Python 代码——有时有效有时无效
AutoHotKey calling Python code--sometimes works sometimes doesn't
我正在尝试从 AHK 调用一个 Python 代码来处理我剪贴板上的 YouTube 记录,删除那些时间戳,将它们融合回一个字符串,然后用新的文本替换原始文本处理过的字符串,以便我可以将其粘贴出来。
AHK代码:
^x::
clipboard =
Send, ^c
Run "directory\try.py"
Return
Python代码(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))
此系统有时有效,但有时无效。有时,当它不起作用时,如果我返回 YouTube 页面并再次按 ctrl + x,它就会起作用。我很确定问题出在 AHK 部分,因为我已经手动使用 Python 代码几个月没有任何错误。感谢任何人都可以提供帮助。
是的。 AHK 太快了。剪贴板的东西需要时间。看看这个。这是如何做到的:
; 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"
您甚至可能需要增加一些睡眠时间:
; 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"
或者更好的是,像这样尝试(使用 OnClipboardChange
函数):
OnClipboardChange("ClipChanged")
return
^x::
Send, ^c
return
ClipChanged(Type) {
MsgBox "%Clipboard%" ; comment out if working well
run "directory\try.py"
ExitApp
}
你可以注释掉结尾 ExitApp
但这样不仅 ^x
会触发它,而且每次剪贴板改变时(所以如果你自己点击 control+c
)你会想要一些退出命令的方式,比如^{esc}::ExitApp
之类的
Hth!!
我正在尝试从 AHK 调用一个 Python 代码来处理我剪贴板上的 YouTube 记录,删除那些时间戳,将它们融合回一个字符串,然后用新的文本替换原始文本处理过的字符串,以便我可以将其粘贴出来。
AHK代码:
^x::
clipboard =
Send, ^c
Run "directory\try.py"
Return
Python代码(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))
此系统有时有效,但有时无效。有时,当它不起作用时,如果我返回 YouTube 页面并再次按 ctrl + x,它就会起作用。我很确定问题出在 AHK 部分,因为我已经手动使用 Python 代码几个月没有任何错误。感谢任何人都可以提供帮助。
是的。 AHK 太快了。剪贴板的东西需要时间。看看这个。这是如何做到的:
; 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"
您甚至可能需要增加一些睡眠时间:
; 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"
或者更好的是,像这样尝试(使用 OnClipboardChange
函数):
OnClipboardChange("ClipChanged")
return
^x::
Send, ^c
return
ClipChanged(Type) {
MsgBox "%Clipboard%" ; comment out if working well
run "directory\try.py"
ExitApp
}
你可以注释掉结尾 ExitApp
但这样不仅 ^x
会触发它,而且每次剪贴板改变时(所以如果你自己点击 control+c
)你会想要一些退出命令的方式,比如^{esc}::ExitApp
之类的
Hth!!