AHK:如何在 Window/File 资源管理器中选择后使用 FileSelectFile 函数将选定文件存储在变量中以执行脚本?

AHK: How to store a selected file in a variable to execute an script using FileSelectFile function after selected in Window/File explorer?

抱歉,我对 AHK 还很陌生。

上下文:我正在尝试构建一个小程序(最终 UI),它将清理 .XLF 文件中的数据,以便 CAT 工具解释器正确处理(导入其中)。

我所说的“干净”是指找到 HTML 属性并将它们替换为各自的 Char 实体。这是一个单一的脚本;在脚本中写入文件名或路径名是完美的。

问题:我想 运行 我的 .ahk/.exe 允许用户打开文件 manager/explorer 和 select 需要由脚本处理的文件(find/replace html 具有字符实体的属性)select 文件无法正常工作。什么都没有填充(最后的 file/result 是空的)我试图用 FileSelectFile 函数解决这个问题,并在第一条指令“fileread,selectedfile.

但它不起作用!如果我不这样做,我只在默认目录“A_ScriptDir”中提供一个特定的文件名 .xf -> 这很好用。

到目前为止,这是我的代码 w/comments:


SetWorkingDir, %A_ScriptDir%

FileEncoding, UTF-8
;NoEnv


;Open Window File Manager/Explorer and select a file .xlf file

FileSelectFile, SelectedFile, 8, , Open a file, , ,(*.xlf)



;---  > HTML attribute '&' must be replaced by its char entity first/overall otherwise this instruction will overwrite the amp entities from the rest of char entities corrupting the file;


;"SelectedFile" can be any filename such as "example.xlf" but this is not my scope

fileread, text, SelectedFile  ;previously "text.xlf" with random html content to do tests

replace := "&"
newtext := strreplace(text, "&", replace, all)

sleep, 200

filedelete, newtest.xlf 
fileappend, %newtext%, newtest.xlf

;--------------------------------- <b>
;here "fileread" must read the final appended file as solution to use "streplace" function multiple times (replace more than one desired string) running the script at the same time. (due to my lack of exp. with loop function)

fileread, text, newtest.xlf 

replace := "&gt;b&lt;"
newtext := strreplace(text, "<b>", replace, all)

filedelete, newtest.xlf 
fileappend, %newtext%, newtest.xlf


我一直在想其他解决方案可以是:

我对应用拖放 GUI 还是个新手,但我不确定如何修改我的代码以便 drag/drop 将文件放到 ahk.exe

感谢您花时间阅读本文!任何提示 and/or 帮助将不胜感激:)

FileRead 命令需要文本,而不是输入变量。 所以如果你像这样在 SelectedFile 周围添加 %,它应该可以工作:

FileRead, text, %SelectedFile%

如果还是不行,说明文件不存在或者发生了错误。在那里,您需要查看 FileReaderror handling section