OSX:当文件出现在文件夹中时执行bash脚本

时间:2014-01-27 15:42:41

标签: macos shell automation

我正在尝试编写自己的屏幕截图上传程序。

我快到了: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

2 个答案:

答案 0 :(得分:3)

您可以使用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

答案 1 :(得分:1)

将屏幕截图保存到〜/ screens /

defaults write com.apple.screencapture location ~/screens/
killall SystemUIServer

将ss2CB.plist保存到〜/ Library / LaunchAgents /

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <false/>
    <key>Label</key>
    <string>copy Screen Shots to ClipBoard</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/python</string>
        <string>~/ss2cb.py</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>~/screens/</string>
    </array>
</dict>
</plist>