如何设置 AppleScript 在给定文件被修改时运行?
我知道 applescripts 可以设置为作为文件夹操作运行,但是有没有办法让它们作为 文件 操作运行?是的,我知道我可以通过创建一个新文件夹,将脚本放入其中并作为文件夹操作运行来解决此问题,但我想知道这是否可以完成。
I know that applescripts can be set to run as folder actions, but is there a way to get them to run as file actions? Yes, I know I can work around this by just making a new folder, putting the script in it, and running as a folder action, but I want to know if this can be done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将这个问题交叉发布到 Apple 的 Applescript 讨论板,那里的一些用户给出了一些仅使用系统资源,不使用第三方应用程序的解决方案。点击链接即可查看答案。
基本上,您需要使用
launchd
来监视文件并在文件更改时启动脚本。然后,您需要制作一个.plist
来描述要运行的脚本以及要监视的文件或目录。I cross-posted this question to Apple's Applescript discussion board, and a few users there gave some solutions that use only system resources, with no third-party applications. Follow the link to see the answers.
Basically you need to use
launchd
to watch a file and launch a script when the file changes. Then you'd need to craft a.plist
that describes the script to run and the file(s) or directories to watch.没有简单的内置方法可以做到这一点。然而,有很多方法可以设置要运行的 Applescript。但一般来说,您必须进行一些非 Applescript 编码。
查看此提示和 OSXHints 中的信息:
http://hints.macworld.com/article .php?story=20060817044149264
他们有一个 Python 脚本,演示如何使用 fsevents.so 来监视文件更改。然后,您只需修改 Python 来调用您的 Applescript(或者简单地使用 Appscript 在 Python 中执行脚本)。
或者,您可以让程序以特定的时间间隔检查文件,然后在文件发生更改时运行。此提示在 Perl 中描述:
http://hints.macworld.com/article.php?故事=20031001073403810
There's no simple built in way to do this. However there's lots of ways to set up an Applescript to run. However in general you'll have to do some non-Applescript coding.
Check out the info in this hint and OSXHints:
http://hints.macworld.com/article.php?story=20060817044149264
They have a Python script demonstrating using fsevents.so to monitor file changes. You would then just modify the Python to call your Applescript (or simply use Appscript to do the scripting from within Python).
Alternatively you can just have a program check a file at a particular interval and then run if the file changes. This hint describes that in Perl:
http://hints.macworld.com/article.php?story=20031001073403810
我找到了一种行之有效的方法。
Shell 脚本:
在 Automator 中创建一个日历事件来运行此 shell 脚本,该脚本将检查文件。如果在过去 24 小时内进行了修改,则会将其复制到监视文件夹中。然后在 Automator 中创建一个操作将其移至垃圾箱(否则下次 shell 脚本运行时它将无法工作)。
在监视的文件夹上设置文件夹操作以监视添加的新项目,然后它会通知您。我创建了一个脚本来向我发送一封电子邮件,并将其放在“库/脚本”中的“文件夹操作”文件夹中。它就像一种享受。
I found a way to do this that works.
Shell script:
Create a Calendar Event in Automator to run this shell script which will check the file. If it has been modified in the last 24 hours, then it will be copied to the watched folder. Then create an action in Automator to move it to trash (otherwise the next time the shell script runs it won't work).
Set up a Folder Action on the watched folder to watch for an added new item and then it will let you know. I have created a script to send me an email and put that in the Folder Actions folder in Library/Scripts. It works like a treat.
这是一个老问题了,但 MacOS 中的 Apple Automator 工具正是做到了这一点。您可以将其设置为监视文件夹并执行 Applescript、Shell 脚本等。
This is an old question, but the Apple Automator tool in MacOS does exactly this. You can set it to watch a folder and execute an Applescript, Shell Script, etc.