【发布时间】:2016-10-08 07:28:33
【问题描述】:
我们正在使用一个解析器程序(我无权访问它)来解析一堆计算机生成的邮件,但需要一些帮助来决定它必须具体做什么。因此,员工 kann 将主题行用于附加命令。由于每天我们向程序提供超过 500 封邮件,并且命令看起来都类似于:Ba,Vi;#TD*; x0003,因此不可能手动编写它们。所以我写了一个小的 C# 脚本,它创建了一个自动热键脚本,它完成了 90% 的工作。理论上。它有效,但前提是我不使用任何特殊字符,例如, : & % etc。
我试过了:
clipboard := Ba{,}Vi{;}{#}TD{*}{;} x0003
clipboard := "Ba,Vi;#TD*; x0003"
clipboard := Ba',Vi';'#TD'*'; x0003
clipboard := {raw} Ba,Vi;#TD*; x0003
(plus some others that I probably forgot here)
这是带有注释的整个 AHK 脚本。在 Outlook 中选择电子邮件时启动它:
;Win+z -> start script
#z::
;Currently only one iteration
loop,1
{
;CTRL+F to forward selected mail,
;which then automatically selects the "To:" line
Send, {CTRLDOWN}f{CTRUP}
Sleep, 500
Send, someemail@adress
Sleep, 500
;Initialize GUI
Gui, +AlwaysOnTop
Gui, Font, S5, Verdana, bold
Gui, Add, Text,, SCANNING-BOOSTER:
Gui, Color, F4A460
;Example for the C# generated buttons below (they all do the same thing):
;Clicking the Button "Google" will run the following script
;Start:
;clipboard := www.Google.com
;return
;This is the part where AHK fails because instead
;of www.Google.com I have codes like "Ba,Vi;#TD*; x0003" which crash AHK
Gui,add,Button,gLabel,Google
Gui,add,Button, ......
Gui,add,Button, ......
Gui,add,Button, ......
Gui,add,Button, ......
Gui,add,Button, ......
..... (around 60 more auto-generated buttons)
Gui,show
Return
Label:
;run the script that has the same name as the Button
;in this case it would be Google.ahk
Run, % A_GuiControl ".ahk"
GuiClose:
Gui, Hide
Sleep, 1000
;after the user has pressed a button and the according code
;has been copied to the clipboard, the GUI closes, the
;mail window becomes active again and we can continue to paste
;the code into the subject line
;Go to subject line
Send, {ALTDOWN}r{ALTUP}
Sleep, 500
;CTRL+a
Send, {CTRLDOWN}a{CTRUP}
Sleep, 500
;Write text from your clipboard to the subject line
Send, %clipboard%
Sleep, 500
return
}
【问题讨论】:
-
您的脚本做什么以及它应该做什么?请提供您的代码并指出什么不起作用。无论如何,看看#EscapeChar 并转义每个具有特殊含义的字符。
-
已添加整个脚本,并带有注释。这显然不是最终的,但我需要先得到剪贴板部分,然后才能继续其余部分。转义字符的问题是我不确切知道将使用哪些字符,因为将来会添加和删除参数。
-
有什么问题?您是否收到错误或不需要的行为?请解释什么解释什么不起作用以及它应该如何起作用。
-
我都得到了,这取决于字符串中的字符。一旦它只是移动了我的鼠标,一旦它继续永远写入,但大多数时候它会抛出错误“最左边的字符在表达式中是非法的”。它只是执行代码而不是将其复制到剪贴板。稍后我将运行一个测试,用相应的扫描码替换每个字符。
-
我想我终于明白了。因此,您的 C# 程序会生成由于特殊字符而在语法上不正确的剪贴板分配。正确的?所以问题实际上在于你的 C# 代码而不是 AHK。如果是这样,请在您的问题中添加
C#标签并提供代码。您需要确保每个特殊字符(如#EscapeChar文档中所列)都被转义,这可以通过简单的字符串替换来实现。
标签: string autohotkey