AHK 1.1.30.03版本中转义闭合大括号问题求助
AHK 1.1.30.03 多行文本块闭合大括号被误解析的解决方法
在AHK 1.1.30.03版本脚本中,尝试将一段JS代码存入剪贴板再通过Ctrl+V粘贴时,出现最后一个if语句的闭合大括号被AHK当作代码解析、无法作为纯文本存入剪贴板的问题。试过用反引号}转义、双闭合大括号}}均无效,原脚本如下:
^*!p:: ClipboardBackup:=ClipboardAll Clipboard = ( if (typeof jQuery === 'undefined') { console.error('some error'); } else { function handleComponentClick(event) { if (event.ctrlKey) { event.stopPropagation(); let target = jQuery(event.target); if (!target.is('[data-sap-ui]')) { target = target.closest('[data-sap-ui]'); } if (target.length) { console.log("%c ID clicked: %c" + target.attr('id') || 'ID dont found', "background:MediumAquaMarine; color:black; font-size: 0.8rem; padding: 2px 0; border-radius: 2px;font-weight: bold;", "background:white; color:black; font-size: 0.8rem; padding: 2px 5px; border-radius: 2px;" ); } } } } ;Else close curly brace ) ClipWait, 2 SendInput, ^v sleep, 50 Clipboard:=ClipboardBackup return
问题根源
AHK 1.1的无引号延续段(即Clipboard = ( ... )形式)中,会将与开头(处于同一列的}视为延续段的结束标记,而非文本内容。同时无引号延续段会自动解析%符号为变量引用,也可能干扰代码内容。
解决方法
方法1:使用带引号的表达式延续段
改用表达式赋值(:=),并用双引号包裹整个延续段,AHK会将内部内容全部视为纯文本,不会解析大括号或%符号:
^*!p:: ClipboardBackup := ClipboardAll Clipboard := " ( if (typeof jQuery === 'undefined') { console.error('some error'); } else { function handleComponentClick(event) { if (event.ctrlKey) { event.stopPropagation(); let target = jQuery(event.target); if (!target.is('[data-sap-ui]')) { target = target.closest('[data-sap-ui]'); } if (target.length) { console.log("%c ID clicked: %c" + target.attr('id') || 'ID dont found', "background:MediumAquaMarine; color:black; font-size: 0.8rem; padding: 2px 0; border-radius: 2px;font-weight: bold;", "background:white; color:black; font-size: 0.8rem; padding: 2px 5px; border-radius: 2px;" ); } } } } ;Else close curly brace )" ClipWait, 2 SendInput, ^v Sleep, 50 Clipboard := ClipboardBackup return
方法2:调整无引号延续段的格式
如果坚持使用无引号赋值,需确保作为文本的}不与延续段开头的(对齐,同时用`%转义代码中的%符号(避免AHK解析为变量):
^*!p:: ClipboardBackup := ClipboardAll Clipboard = ( if (typeof jQuery === 'undefined') { console.error('some error'); } else { function handleComponentClick(event) { if (event.ctrlKey) { event.stopPropagation(); let target = jQuery(event.target); if (!target.is('[data-sap-ui]')) { target = target.closest('[data-sap-ui]'); } if (target.length) { console.log("`%c ID clicked: `%c" + target.attr('id') || 'ID dont found', "background:MediumAquaMarine; color:black; font-size: 0.8rem; padding: 2px 0; border-radius: 2px;font-weight: bold;", "background:white; color:black; font-size: 0.8rem; padding: 2px 5px; border-radius: 2px;" ); } } } } ;Else close curly brace ; 增加缩进,避免与开头的(对齐 ) ClipWait, 2 SendInput, ^v Sleep, 50 Clipboard := ClipboardBackup return
内容的提问来源于stack exchange,提问作者HOTOMOL
相关产品推荐
相关产品推荐

