osascript -e 'display notification "通知內(nèi)容" with title "標(biāo)題" subtitle "子標(biāo)題"'
osascript -e 'tell application "System Events" to keystroke "LOGIN_NAME"'; /
osascript -e 'tell application "System Events" to keystroke tab'; /
osascript -e 'tell application "System Events" to delay 0.5'; /
osascript -e 'tell application "System Events" to keystroke "PASSWORDHERE"'; /
osascript -e 'tell application "System Events" to delay 0.5'; /
osascript -e 'tell application "System Events" to keystroke return'
下面的一些osascript命令比較有用:
sudo osascript -e 'tell app "[name of an open program]" to quit'
sudo osascript -e 'tell app "Finder" to sleep'
sudo osascript -e 'tell app "Finder" to shut down'
sudo osascript -e "set volume 0"
sudo osascript -e "beep"
sudo osascript -e 'display dialog "Did you know that you are annoying?"
buttons "Yes" with icon note'
sudo osascript -e 'tell app "Finder" to quit'
sudo osascript -e 'say "[whatever]" using "Zarvox"'
iTunes Control:
sudo open /Applications/iTunes.app; sudo osascript -e 'say "Play some music.
Go on. I dare you." using "Zarvox"'
sudo osascript -e 'tell app "iTunes" to stop' -e 'say "Please stop playing
your annoying music" using "Zarvox"'
sudo osascript -e 'tell app "iTunes" to next track' -e 'say "I did not like
that song very much" using "Zarvox"'
sudo osascript -e 'tell app "iTunes" to fast forward' -e 'say "This song is
boring" using "Zarvox"'
sudo osascript -e 'tell app "iTunes" to quit'
Mac OS X:遠(yuǎn)程執(zhí)行osascript命令及問題解決
對于系統(tǒng)管理/電腦維護人員,在Applr Remote Desktop的管理機上可以通過圖形方式遠(yuǎn)程控制網(wǎng)絡(luò)上的每臺Mac電腦, 而很多時候需要發(fā)送Unix命令來完成工作杜漠。比如有時需要使用管理員帳戶遠(yuǎn)程登錄到每臺電腦极景,大家普遍知道的命令是:
| osascript -e 'tell application "System Events"' -e 'keystroke "LOGIN_NAME"' -e 'keystroke tab' -e 'delay 0.5' -e 'keystroke "PASSWORDHERE"' -e 'delay 0.5' -e 'keystroke return' -e 'end tell' |
上面的命令可以讓每一個處于登錄狀態(tài)的電腦自動登錄到LOGIN_NAME用戶察净。這樣管理員就可以對每個電腦進(jìn)行操作了.
下面是更加清晰的一個腳本版本
| osascript -e 'tell application "System Events" to keystroke "LOGIN_NAME"'; /
osascript -e 'tell application "System Events" to keystroke tab'; /
osascript -e 'tell application "System Events" to delay 0.5'; /
osascript -e 'tell application "System Events" to keystroke "PASSWORDHERE"'; /
osascript -e 'tell application "System Events" to delay 0.5'; /
osascript -e 'tell application "System Events" to keystroke return' |
然而實際使用中會問題:
1. 遠(yuǎn)程計算機根本不執(zhí)行上面的操作:
這個問題一般是由于被管理的電腦沒有設(shè)置為準(zhǔn)許執(zhí)行UI腳本驾茴,所以要打開它∏饪ǎ可以通過兩種方式:
A. 首先以管理員身份登錄到該電腦锈至,然后進(jìn)入"System Preferences -> Universal Access",開啟在下部的選項 "Enable access for assistive devices"
[圖片上傳失敗...(image-d600d5-1531880690669)]
B. 這個方法更簡單译秦,而且可以遠(yuǎn)程發(fā)送(Unix命令)到目標(biāo)電腦:
| echo a > /var/db/.AccessibilityAPIEnabled |
2. 那個腳本只有在目標(biāo)電腦處于登錄窗口的時候才可以使用. 所以峡捡,需要首先啟動目標(biāo)電腦击碗,這可以通過好多方法啟動目標(biāo)機. 比如可以使用ARD菜單中的Restart命令,也可以通過發(fā)送下面的任何一個命令
| osascript -e 'tell app "Finder" to restart'
|
| shutdown -r TIME "This computer is going to restart."
|
其中TIME可以是now, 或者是時間比如: 10:00am等等
3. 還要注意的是如果登錄不是輸入用戶名的方式们拙,是用戶列表的方式稍途,那么上面的方式都不可用,需要修改為列表方式并禁止自動登錄:
[圖片上傳失敗...(image-851af5-1531880690669)]
4. 可以發(fā)送下面命令來使當(dāng)前用戶退出到登錄狀態(tài):
| osascript -e 'tell app "Finder" to exit'
|
或者使用ARD的菜單命令來使用戶退出登錄.
5. 如果是目標(biāo)機設(shè)置了定時睡眠砚婆,可以使用Wake命令喚醒目標(biāo)機, 要求是目標(biāo)機設(shè)置允許遠(yuǎn)程喚醒: