以之前写的过滤图片的 脚本 为例,把其封装成 App。
首先来看下最终的 App 的结构:
1 2 3 4 5 6 7
| ImageFilter.app |____Contents | |____MacOS | | |____run.sh | |____Resources | | |____shortcut.icns | |____Info.plist
|
其中,shortcut.icns
为 App 图标,run.sh
为要封装的脚本,Info.plist
为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleExecutable</key> <string>run.sh</string> <key>CFBundleIconFile</key> <string>shortcut.icns</string> <key>CFBundleInfoDictionaryVersion</key> <string>1.0</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>1.0</string> </dict> </plist>
|
run.sh
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #!/bin/bash
dir="/Users/wy/Downloads/temp"
img_filter() { for file in `ls $1`; do if [ -f "$1/$file" ]; then if [ ${file##*.} == "png" ] || [ ${file##*.} == "PNG" ] || [ ${file##*.} == "jpg" ]; then imgH=`sips -g pixelHeight "$1/$file" | awk -F ':' '{print $2}'` imgW=`sips -g pixelWidth "$1/$file" | awk -F ':' '{print $2}'` if [ $imgH -gt $imgW ]; then rm "$1/$file" fi fi else select_img "$1/$file" fi done }
osascript<<EOF tell application "Finder" display notification with title "Start filtering images..." end tell EOF
img_filter "$dir"
osascript<<EOF tell application "Finder" display notification with title "Done !" sound name "Glass" end tell EOF
|
其中,用到了 AppleScript,来显示任务开始和结束的通知。AppleScript 功能也非常强大,可以用来做很多小程序(Applet),做一些自动化的工具,后面有时间要学习下。
至此,ImageFilter.app 就完成了。✌️
相关链接