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

标签 macos shell automation

我正在尝试编写我自己的屏幕截图 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/

相关文章:

node.js - npm 模块实现包装 shell 命令的模式?

linux - 如何将非 UTF-8 格式的 xml 文件转换为符合 UTF-8 格式的 xml

java - Selenium-断言列表中数组的多个关键字的存在

用于测量电池消耗的 iOS 工具

objective-c - 无法关闭窗口 - 为什么?

macos - 没有 JRE 的 Mac 的 JavaFX 自包含应用程序打包

c++ - Xcode中如何使用wstring和wcout输出中文单词?

linux - 如何将本地 shell 脚本变量传递给 expect?

automation - Vagrant 是否可以直接使用操作系统 .ISO 安装镜像/或动态地从 ISO 创建 Vagrant 盒子?

c - Mac OS 中的 ranlib/ar 给我 "has no symbols"警告