gpt4 book ai didi

macos - 操作系统 : Execute bash script when file appears in folder

转载 作者:行者123 更新时间:2023-12-04 16:17:11 24 4
gpt4 key购买 nike

我正在尝试编写我自己的屏幕截图 uploader 的脚本。

我快到了:OSX: Automatically upload screenshot to imageBin and put URL in clipboard

那个脚本让我写:

upload_image foo.png

它会上传它,在我的剪贴板中放置一个指向 URL 的链接,然后发出声音

但我希望它是自动的。只要 foo.png 出现在我的桌面上,我就需要执行 '~/upload_image.sh foo.png'

我该怎么做?

我对 Bash 解决方案很感兴趣,因为它可以让我与目标脚本合并,所以我只会有一个干净的脚本文件。它还将是多平台的。让它在启动时启动将是一个问题。

我也对 AppleScript 解决方案感兴趣;我认为以下脚本可能很接近:

on adding folder items to this_folder after receiving theFiles

-- If you want to do something with each file ...
repeat with aFile in theFiles
do shell script "~/upload_image.sh " &
end repeat

end adding folder items to

但我不知道如何将 'aFile' 传递到下一行的命令中。

编辑:

http://hardc0l2e.wordpress.com/2012/03/13/folder-monitoring-script-using-inotifywait/

In a small script to monitor a folder for new files, the script seems to be finding the wrong files

最佳答案

您可以使用 Automator 创建文件夹操作:

或者例如将此 plist 保存为 ~/Library/LaunchAgents/example.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>example</string>
<key>ProgramArguments</key>
<array>
<string>say</string>
<string>a</string>
</array>
<key>WatchPaths</key>
<array>
<string>~/Desktop/</string>
</array>
</dict>
</plist>

然后运行 ​​launchctl load ~/Library/LaunchAgents/example.plist 加载 plist。要将更改应用于 plist,请卸载并加载它。更多信息:

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html
https://developer.apple.com/library/mac/technotes/tn2083/_index.html
http://osxnotes.net/launchd.html

第三种选择是使用 Hazel :

关于macos - 操作系统 : Execute bash script when file appears in folder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21385105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com