我今天刚刚开始使用 AutoHotKeys,仍然不熟悉它的大部分工作原理,但我已经能够成功创建大量自动替换热键。我现在正在尝试解决第一个更复杂的问题:如何不仅自动替换文本或 URL,还自动替换带有 URL 超链接的文本。
我在 StackOverflow 上找到了函数 SetClipboardHTML,以及 this Reddit 引用了它,但它们之间仍然无法解决问题。我可能没有使用足够新的 AHK 版本(我下载了 1.1,但我不知道如何查看比这更具体的细节,SetClipboardHTML 说它需要 1.1.33。
但如果不是更新问题,我会非常感谢一个没有额外复杂细节的简单示例(这是我第一次使用“发送”和“返回”,我所有其他热键都仅在 ::call::替换格式)。我尝试了多种选择,但无法使其发挥作用。对于 Google.com,这是我想到的最好的:
#SingleInstance Force
Hotstring(":*X:.gl", "GoogleLink")
Return
GoogleLink:
SetClipboardHTML(<"a href='https://google.com'>Google.com</a>",, "Google.com")
Send, ^v
Return
我还不确定如何调试 AHK(我在 VSCode 中运行它),所以我发现它不起作用的唯一方法是尝试重新加载脚本,然后它告诉我有一个错误。
感谢您的帮助!
我有这段代码可以满足您的需要。
#Persistent
OnClipboardChange("Clipboard_to_html") ; call the function "clipboard_to_html" anytime your clipboard changes
Return
Clipboard_to_html() {
FileDelete,% "a.c" ; if the temp file used to get all chars exist, it'll be deleted, avoiding getting previous data with the current one
Sleep, 100
FileAppend,% ClipboardAll, a.c ; input the new data to a temp file
Loop
if FileExist("a.c") ; wait the all the clipboard data be inputed in the file
Break
html := bin2str("a.c") ; store in "html" var the data
RegExMatch(html, "<html>(.*?)<\/html>", match) ; remove unnecessary stuff from the data
html := match
if InStr(html, "Retrieving data") ; some times it'll not be able to retrieve all the data(dunno why), so it end the function without updating the clipboard.
Return
FileDelete,% "a.c" ; delete the temp file
Clipboard := html ; set the html content to your clipboard
}
bin2str(file_path) { ; convert the raw binary data from the clipboardAll to chars, byte by byte(yeah, could be a bit slow, specially if it contains pictures)
FileEncoding, UTF-8
FileRead, content,% file_path
Loop,% VarSetCapacity( content ) {
byte := NumGet( content, A_Index-1, "UChar" )
If( byte < 32 )
Continue
text .= Chr( byte )
}
Return text
}
您可以将剪贴板粘贴到扩展名为 .html 的文件中并运行它进行测试。
P.S.:抱歉有任何语法错误,我的英语仍在进步中。