はじめに
最近ラムダさんの動画でAppleScriptの存在を知って少しイジって見たのですが、少し調べるのが大変だったので忘れないように記事にして残します。
※この記事には半沢直樹要素が出てきます。
AppleScriptの始め方
AppleScriptには特に環境構築は必要ありません。プログラムの作成と実行はスクリプトエディタを使います。アプリの開き方はcommand + スペースを押してスポット検索を開き"AppleScript"ぐらいまで入力するとアプリが出てくるので開いて下さい。開くとこんな感じのウィンドウが開きます。
AppleScriptを適当にイジってみる
ディスプレイダイアログを表示される
display dialog "Hello World"
次はsafariを自動で立ち上げて見ましょう!
tell application "Safari"
activate
open location "https://qiita.com/"
end tell
画像の様にsafariを開き特定のサイトを開かせる事に成功しました。
AppleScriptで半沢直樹
repeat
display dialog "" buttons {"親が自分の部屋に来た", "面倒な事を頼まれた", "閉じる"} default button 3 with icon note
copy the result as list to {ret}
if the ret is "親が自分の部屋に来た" then
tell application "Safari"
open location "https://www.youtube.com/watch?v=QTkop65FLYM"
delay 2.5
close tab 1 of window 1
open location "https://www.youtube.com/watch?v=hJBZkO0YFws"
delay 4.0
close tab 1 of window 1
quit
end tell
else if the ret is "面倒な事を頼まれた" then
tell application "Safari"
open location "https://www.youtube.com/watch?v=IXUGjCl_Xvs"
delay 5
quit
end tell
else if the ret is "面倒な事を頼まれた" then
tell application "Safari"
open location "https://www.youtube.com/watch?v=IXUGjCl_Xvs"
delay 5
quit
end tell
else if the ret is "閉じる" then
exit repeat
end if
end repeat
このプログラムを起動すると上のスクショの様なディスプレイダイアログが表示されます。親が自分の部屋に来たや面倒な事を頼まれたなどを押すとYouTubeのリンクに飛んでセリフを再生させたのちに自動でsafariを閉じてくれます。またYouTubeのリンクを変えると違う動画も再生できるので、変えてみると面白いかも知れません。