自動替換剪貼簿內容的 AHK 腳本

自動替換剪貼簿內容的 AHK 腳本

當我寫這樣的 AHK 腳本:

::abc::alphabet

它的作用就像魅力一樣。唯一的事情是,當我想要複製部分文字(包括我想要自動替換的內容)時,它不想替換它。

例如:

!INS::{Ctrl Down}c{Ctrl Up}{Tab 2}{Enter}{Ctrl Down}v{Ctrl Up}

讓我複製abc,但貼上時,我不明白alphabet(如前面所定義)。

有沒有辦法讓它取代複製貼上的文字?就像當我使用send命令發送一行或一些包含我想自動替換的單字的單字時?

答案1

熱字串只影響您實際鍵入的內容。執行搜尋和替換剪貼簿你可以使用正規表示式替換命令。

下面是一個腳本,它複製所選文本,然後貼上修改後的內容(搜尋和替換後)。我相信這就是你的意思:

#x:: ;[Win]+[X]

;Empty the Clipboard.
    Clipboard =
;Copy the select text to the Clipboard.
    SendInput, ^c
;Wait for the Clipboard to fill.
    ClipWait

;Perform the RegEx find and replace operation,
;where "ABC" is the whole-word we want to replace.
    haystack := Clipboard
    needle := "\b" . "ABC" . "\b"
    replacement := "XYZ"
    result := RegExReplace(haystack, needle, replacement)

;Empty the Clipboard
    Clipboard =
;Copy the result to the Clipboard.
    Clipboard := result
;Wait for the Clipboard to fill.
    ClipWait

;-- Optional: --
;Send (paste) the contents of the new Clipboard.
    SendInput, %Clipboard%

;Done!
    return

相關內容