1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AppleScriptを使ってSafariをイジって見よう

Last updated at Posted at 2020-10-03

はじめに

最近ラムダさんの動画でAppleScriptの存在を知って少しイジって見たのですが、少し調べるのが大変だったので忘れないように記事にして残します。
※この記事には半沢直樹要素が出てきます。

AppleScriptの始め方

AppleScriptには特に環境構築は必要ありません。プログラムの作成と実行はスクリプトエディタを使います。アプリの開き方はcommand + スペースを押してスポット検索を開き"AppleScript"ぐらいまで入力するとアプリが出てくるので開いて下さい。開くとこんな感じのウィンドウが開きます。スクリーンショット 2020-10-02 17.39.52.png

AppleScriptを適当にイジってみる

ディスプレイダイアログを表示される

display dialog "Hello World"

これを▶️を押すと次の写真の様に表示する事ができます。
スクリーンショット 2020-10-03 20.43.20.png

次はsafariを自動で立ち上げて見ましょう!

tell application "Safari"
	activate
	open location "https://qiita.com/"
end tell

画像の様にsafariを開き特定のサイトを開かせる事に成功しました。
スクリーンショット 2020-10-03 20.50.40.png

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

スクリーンショット 2020-10-03 20.56.34.png

このプログラムを起動すると上のスクショの様なディスプレイダイアログが表示されます。親が自分の部屋に来たや面倒な事を頼まれたなどを押すとYouTubeのリンクに飛んでセリフを再生させたのちに自動でsafariを閉じてくれます。またYouTubeのリンクを変えると違う動画も再生できるので、変えてみると面白いかも知れません。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?