0
0

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 1 year has passed since last update.

Safari でピクチャインピクチャする AppleScript

Last updated at Posted at 2023-02-21

そもそもピクチャインピクチャとは?

パソコンやテレビの画面の一部に、小さな画面を別表示すること。パソコンの操作画面上にテレビ、動画、ウェブカメラの映像を表示したり、テレビの画面上に別のチャンネルの映像を流したりすることを指す。(デジタル大辞泉)

つまり PC で、他の作業しながら右下とかには動画を流し続けるアレです。

Mac の Safari でも動画を常に最前面に表示させたまま、Safari の別ページに切り替えたり、Safari は非表示にして別のアプリを操作したりできます。なんなら仮想デスクトップを切り替えたりしても最前面で表示してくれます。

Safari でピクチャインピクチャする AppleScript

AppleScript は以下の通りです。

AppleScript
tell application "System Events" to tell process "Safari" to tell first window to tell first toolbar
	tell (first group whose role description of first UI element is "tab group")
		
		-- メニューを出す
		perform action "AXShowMenu" of button 2 of (radio button 1 whose value is true) of UI element 1
		
		delay 0.1
		
		-- メニュー項目に「Picture in Picture」が存在するかどうかで場合分け
		if name of menu item 2 of menu 1 contains "Picture in Picture" then
			
			-- 存在する場合はそのメニュー項目をクリック
			click menu item 2 of menu 1
			
		else
			
			-- 存在しない場合は ESC キーを押す
			key code 53
			
		end if
	end tell
end tell

※「Picture in Picture」の部分は言語設定が英語の場合。

簡略的な解説

この AppleScript は、

  1. ツールバーのタブグループという UI 要素の、いま開いているタブの右端の「・・・」の部分をクリックしてメニューを出す(perform action "AXShowMenu" of button ~
  2. メニュー項目に「Picture in Picture」が存在する場合はそれをクリックし、存在しない場合は ESC キーを押す(click menu item ~

という2つのことをしているだけです(にもかかわらずやたら長いのは、それぞれの UI 要素にたどり着くためのパスが長いからです)。

質問があればコメントにどうぞ。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?