- 平臺:Mac OS
- 工具:腳本編輯器
- AppleScript 幫助
- Introduction to AppleScript Language Guide
常用語句
hello world
say "hello world"
beep 2
變量
set a to {1, 2}
# 此處報錯够话,a不能轉(zhuǎn)成字符串
say a
dialog
set message to "Dialog Message"
set temp to display dialog message buttons {"OK", "Cancel"}
set selectedTitle to button returned of temp
display dialog "You pressed the following button " & selectedTitle
清空廢紙簍
tell application "Finder"
empty trash
end tell
列表選擇器
choose from list {"dawei", "libai", "lilei"} with title "名字選擇器" with prompt "請選擇名稱" default items {"lilei"} with empty selection allowed and multiple selections allowed
文件選擇器
# choose folder 選擇文件夾
choose file of type ("txt") with prompt "選取文件"
AppleScript 和 shell 交互
相互調(diào)用
AppleScript 調(diào)用shell
do shell script "cd ~;ls"
shell 調(diào)用 AppleScript
# osascript -e "語句"
finder=`osascript -e "set dir to POSIX path of (choose folder with prompt \"選擇一個文件夾\")"`
# 輸出結(jié)果/Users/xiaoniu/Documents/XXXX/
echo $finder
/**
osascript <<EOF
語句
EOF
*/
echo "shell 調(diào)用 AppleScript start"
path=`pwd`
osascript <<EOF
set a to POSIX file "$path"
tell application "Finder"
open folder a
end tell
EOF
# 這里注意一下傳值問題贴彼,path是shell里面的變量,作為參數(shù)傳個osascript語句羡洛,加了"".
echo "
傳值
AppleScript -> shell
如果是字符串,可以直接使用 & 進行拼接砾层。
set hostname to "www.baidu.com"
do shell script "ping -c1 " & hostname
更通用的是 quoted form of
# quoted form of
set hostname to "www.baidu.com"
do shell script "ping -c1 " & quoted form of hostname
shell -> AppleScript
path=`pwd`
osascript <<EOF
set a to POSIX file "$path"
很多時候宠页,shell 需要獲取Apple Script的執(zhí)行結(jié)果,獲取方式如下
finder=`osascript -e "set dir to POSIX path of (choose folder with prompt \"選擇一個文件夾\")"`
# 輸出結(jié)果/Users/xiaoniu/Documents/XXXX/
echo $finder
路徑轉(zhuǎn)換
set p to "/usr/local/bin/"
set a to POSIX file p
-- file "Macintosh HD:usr:local:bin:"
To translate an AppleScript path (file or directory, valid or not) into a POSIX path use POSIX path of.
set a to "Macintosh HD:usr:local:bin:"
set p to POSIX path of a
-- "/usr/local/bin/"
定時執(zhí)行腳本
單純的腳本雖然功能強大担钮,但是有時候的確不是那么方便。配合定時任務(wù)尤仍,腳本可以發(fā)揮更大的功能箫津。
<?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>Label</key>
<string>com.feng.record</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>~/Documents/record.scpt</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>0</integer>
<key>Hour</key>
<integer>18</integer>
</dict>
<key>StandardInPath</key>
<string>~/Documents/MyAppleScript/in.log</string>
<key>StandardOutPath</key>
<string>~/Documents/MyAppleScript/out.log</string>
<key>StandardErrorPath</key>
<string>~/Documents/MyAppleScript/err.log</string>
</dict>
</plist>
$ plutil -lint xxx.plist
$ launchctl load ~/Library/LaunchAgents/18.plist
每天18:00執(zhí)行腳本~/Documents/record.scpt,至于這個腳本里寫什么就可以自由發(fā)揮了宰啦。
# record.scpt
# 語音 + 彈窗 提醒打卡
say "快去打卡苏遥,忘記打卡就不好了!"
display dialog "快去打卡赡模,忘記打卡就不好了田炭!" buttons {"了解"}