AppleScriptでQuickTimeとYoutubeを操作

個人的メモ

# やりたいこと
1. QuickTime 起動＆ムービー収録
2. 録画開始
3. 音源リピート最後少し手前まで移動して開始

失敗したら
1. 録画破棄
2.撮影1.からやり直し


# AppleScript
- 前提
    - QuickTimeで最後に録画した設定を使う（自分の場合はiPhone&UR22C)
    - QuickTimeのカレントディレクトリが保存したい場所になっていること
    - macOS Ventura ver13.5.2
- script
```
-- 録画ファイルの保存パス
set savePath to ""

-- QuickTime Playerを開く
tell application "QuickTime Player"
	activate
	repeat
		-- 新しい録画ウィンドウを作成
		set newRecording to new movie recording
		-- 録画を開始
		start newRecording
		delay 3
		--- YoutubeURL
		--- set youtubeURL to "https://www.youtube.com/watch?v=krZalIjl0SI&list=PLgvJWgax2A6kUsgEXEUGlyWqtiSGdAOAN&index=8"
		set youtubeURL to "https://www.youtube.com/watch?v=SiNlrdQg_IA&list=OLAK5uy_lpcKOexUOSZFwkAA4GVLPoKlWxSfrGMsI&index=5"
		my doOpen(youtubeURL)
		
		-- ダイアログのボタンを設定
		set dialogButtons to {"保存", "再録画", "キャンセル"}
		
		-- タイムアウトを無効にしてダイアログを表示
		with timeout of (3600 * 24) seconds -- タイムアウトを無効にし、1日間表示
			set userChoice to display dialog "録画中... 録画を保存するか、再録画するか、キャンセルするかを選択してください。" buttons dialogButtons default button "保存"
		end timeout
		
		-- ユーザーの選択に応じて処理を実行
		activate
		if button returned of userChoice is "保存" then
			-- 録画を停止
			my doClickWithTimeout(4)
			-- 閉じる
			my doCloseWithTimeout()
			-- 録画ファイルを保存
			set timeoutSeconds to 2.0
			set uiScript to "click text field 1 of sheet 1 of window 1 of application process \"QuickTime Player\""
			my doWithTimeout(uiScript, timeoutSeconds)
			set inputText to "take_" & (do shell script "date '+%Y%m%d%H%M%S'") & ".mov"
			my doTextWithTimeout(inputText)
			my doSheetClickWithTimeout(14)
			exit repeat
			
		else if button returned of userChoice is "再録画" then
			-- 録画を停止
			my doClickWithTimeout(4)
			-- 閉じる
			my doCloseWithTimeout()
			-- 削除
			my doSheetClickWithTimeout(12)
			
		else if button returned of userChoice is "キャンセル" then
			-- 録画を停止して保存せずに終了
			stop newRecording
			exit repeat
		end if
	end repeat
end tell

return

on doOpen(youtubeURL)
	-- Safariを開く
	tell application "Safari"
		activate
		delay 2 -- Safariの起動を待つ (必要に応じて調整)
		
		-- YouTubeのURLを開く
		set URL of current tab of window 1 to youtubeURL
	end tell
	
	tell application "QuickTime Player"
		activate
	end tell
end doOpen

on doPlay()
	-- Safariを開く
	tell application "Safari"
		activate
		
		-- ビデオ再生
		do JavaScript "document.querySelector('video').play();"
	end tell
	
	tell application "QuickTime Player"
		activate
	end tell
end doPlay

on doWithTimeout(uiScript, timeoutSeconds)
	set endDate to (current date) + timeoutSeconds
	repeat
		try
			run script "tell application \"System Events\"
" & uiScript & "
end tell"
			exit repeat
		on error errorMessage
			if ((current date) > endDate) then
				error "Can not " & uiScript
			end if
		end try
	end repeat
end doWithTimeout

on doTextWithTimeout(inputText)
	set uiScript to "keystroke \"" & inputText & "\""
	set timeoutSeconds to 2.0
	set endDate to (current date) + timeoutSeconds
	repeat
		try
			run script "tell application \"System Events\"
" & uiScript & "
end tell"
			exit repeat
		on error errorMessage
			if ((current date) > endDate) then
				error "Can not " & uiScript
			end if
		end try
	end repeat
end doTextWithTimeout

on doCloseWithTimeout()
	set uiScript to "keystroke \"w\" using command down"
	set timeoutSeconds to 2.0
	set endDate to (current date) + timeoutSeconds
	repeat
		try
			run script "tell application \"System Events\"
" & uiScript & "
end tell"
			exit repeat
		on error errorMessage
			if ((current date) > endDate) then
				error "Can not " & uiScript
			end if
		end try
	end repeat
end doCloseWithTimeout

on doSheetClickWithTimeout(elementNumber)
	set uiScript to "click UI Element " & elementNumber & " of sheet 1 of window 1 of application process \"QuickTime Player\""
	set timeoutSeconds to 2.0
	set endDate to (current date) + timeoutSeconds
	repeat
		try
			run script "tell application \"System Events\"
" & uiScript & "
end tell"
			exit repeat
		on error errorMessage
			if ((current date) > endDate) then
				error "Can not " & uiScript
			end if
		end try
	end repeat
end doSheetClickWithTimeout

on doClickWithTimeout(elementNumber)
	set uiScript to "click UI Element " & elementNumber & " of window 1 of application process \"QuickTime Player\""
	set timeoutSeconds to 2.0
	set endDate to (current date) + timeoutSeconds
	repeat
		try
			run script "tell application \"System Events\"
" & uiScript & "
end tell"
			exit repeat
		on error errorMessage
			if ((current date) > endDate) then
				error "Can not " & uiScript
			end if
		end try
	end repeat
end doClickWithTimeout

on enterFileName(elementNumber, fileName)
	-- UI Elementの番号を指定してクリック
	doClickWithTimeout(elementNumber)
	
	-- ファイル名を入力
	set uiScript to "tell application \"System Events\"
        tell process \"QuickTime Player\"
            set value of text field 1 of sheet 1 of window 1 to \"" & fileName & "\"
        end tell
    end tell"
	
	do shell script "/usr/bin/env python -c 'import subprocess as sp;sp.run([\"/usr/bin/osascript\", \"-e\", \"" & uiScript & "\"])'"
end enterFileName
```