Autohotkey 如何使用自动热键对文件夹中创建的新文件进行操作?

Autohotkey 如何使用自动热键对文件夹中创建的新文件进行操作?,autohotkey,Autohotkey,我想让AutoHotKey监听/观看磁盘上的文件夹(比如c:\folder),以查找在那里创建的任何新文件。如果出现新文件,脚本应执行一组命令: Send{Esc} 睡吧,100 发送{F5} 睡吧,100 发送{Home} 睡吧,100 发送s 睡吧,100 发送{Enter} 返回 ,之后,它应该返回到查看文件夹中的新更改等。我知道它应该使用WatchDirectory()函数,但不知道如何实现它。有什么帮助吗?谢谢 您可以使用该功能,下载它并将其放在与脚本相同的位置,然后您可以执行以下操作

我想让AutoHotKey监听/观看磁盘上的文件夹(比如c:\folder),以查找在那里创建的任何新文件。如果出现新文件,脚本应执行一组命令:

Send{Esc}
睡吧,100
发送{F5}
睡吧,100
发送{Home}
睡吧,100
发送s
睡吧,100
发送{Enter}
返回

,之后,它应该返回到查看文件夹中的新更改等。我知道它应该使用WatchDirectory()函数,但不知道如何实现它。有什么帮助吗?谢谢

您可以使用该功能,下载它并将其放在与脚本相同的位置,然后您可以执行以下操作:

#Include %A_ScriptDir%\WatchFolder.ahk

WatchFolder("c:\folder", "myFunc", , Watch := 1)
return

myFunc(path, changes) {
    for k, change in changes
        ; 1 means new file was added
        if (change.action = 1) {
            gosub doStuff
            return
        }
}

doStuff:
; commands you want to execute
return

谢谢你的回答。我添加了你的代码,但它不想工作,脚本似乎无法加载(任务栏中没有图标)。我的最终代码如下所示:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


#Include %A_ScriptDir%\WatchFolder.ahk

WatchFolder("C:\Users\kris\Desktop\temp", "myFunc", , Watch := 1)
return

myFunc(path, changes) {
for k, change in changes
    ; 1 means new file was added
    if (change.action = 1) {
        gosub doStuff
        return
    }
}

doStuff:
  Send {Esc}
  Sleep, 500
  Send {F5}
  Sleep, 100
  Send {Home}
  Sleep, 100
  Send s
  Sleep, 100
  Send {Enter}
return

我已将WatchFolder.ahk插入脚本所在的文件夹。

这是因为您没有任何热键,它只是退出。在开头添加
#Persistent
,它将继续运行。Thx!工作就像一个符咒:)