如何让Autohotkey“监听"文件更改? [英] How to have Autohotkey 'listen' for a change to a file?

查看:175
本文介绍了如何让Autohotkey“监听"文件更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,让我们称之为C:\to_run.txt.我希望我的Autohotkey脚本以这种方式侦听"该文件,以使其在检测到更改时立即根据文件的内容执行操作,然后再次将该文件设为空白.

I have a text file, lets call it C:\to_run.txt. I'd like my Autohotkey script to 'listen' to this file in such a way that when it detects a change, it performs an action immediately based on the contents of the file, and then make the file blank again.

我可以处理最后的部分,所以我真的要求一种有效的方法来检测文件更改吗?我之所以说有效率,是因为我的Autohotkey脚本越来越长,而且我不希望此侦听功能以任何方式挂断脚本的其余部分.

I can handle the last parts, so really I'm asking for an efficient way to detect file changes? I say efficient because my Autohotkey script is getting rather long and I don't want this listening function to hang up the rest of the script in any way.

推荐答案

查看只需确保您正在运行 AHK的最新版本.

要使其启动并运行,请首先下载这三个脚本并将其保存到您的/lib文件夹中.

To get it up and running, first download these three scripts and save them to your /lib folder.

  • WatchDirectory.ahk
  • _Struct.ahk
  • sizeof.ahk

在那之后,只需指向您要查找的位置,提供一个回调函数,并根据需要提供第三个参数来监视特定的变化.请参阅此论坛帖子以获得完整文档.

After that, simply point to to wherever you want to look, provide a callback function and if you want, a third param to watch for specific changes. Refer to this forum post for full documentation.

如果将此脚本放置在其自己的文件夹中,请保存并运行该脚本,然后再次保存,它将检测到对该脚本文件的更改.

If you drop this script in its own folder, save it and run it, then save it again, it should detect changes to that script file.

#Persistent

WatchDirectory(A_ScriptDir "\|.ahk\", "Callback", 0x10)
return

Callback(param1, param2) {
    msgBox % param1 "`n" param2
}

但是请注意,只要更改文件,它将触发两次.据我所知,这似乎是Windows的行为.

Note however, it will fire twice whenever the file is changed. This seems to be a Windows behaviour from what I can gather.

这篇关于如何让Autohotkey“监听"文件更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆