🍎

【Mac】NotebookLMでダウンロードした音声ファイルを修復するコマンド実行を自動化するAppleScript

に公開

Macユーザー向け。コマンド実行を AppleScript 書いて自動化。

はじめに

前回の記事で NotebookLM からダウンロードした M4A ファイルが壊れている問題と、afconvertコマンドで修復する方法を書いたのだ。

https://zenn.dev/hamuziro/articles/388aed11482c23

コマンド例:

afconvert -f m4af -d aac "~/Downloads/input.m4a" "~/Downloads/output.m4a"

実行したいコマンドはわかったけれど、毎回ターミナルを開いてファイルパスを指定して…ってやるのは正直面倒なので、このコマンド実行を含めたscript AppleScript[1] で書いて、変換したい音楽ファイルをフォルダにファイルを放り込むだけでやりたい処理が実行されるようにしたのだ。

AppleScript のコード

ぼくの使っている AppleScript を参考までに載せておくのだ。

on adding folder items to this_folder after receiving added_items
	repeat with this_item in added_items
		try
			set item_info to info for this_item
			set file_name to name of item_info

            -- 現在(2025/09/09)のダウンロードファイル形式にも対応(M4Aは将来消してもいいかも)
			if (file_name ends with ".m4a") or (file_name ends with ".mp4") then
				
				set input_path to POSIX path of this_item
				set temp_output_path to "/tmp/" & file_name
				
				set convert_command to "afconvert -f m4af -d aac " & quoted form of input_path & " " & quoted form of temp_output_path
				do shell script convert_command
				
				set delete_command to "rm " & quoted form of input_path
				do shell script delete_command
				
				set rename_command to "mv " & quoted form of temp_output_path & " " & quoted form of input_path
				do shell script rename_command
			end if
			
		on error error_message
			display dialog "エラーが発生しました: " & error_message
		end try
	end repeat
end adding folder items to

スクリプト実行設定方法

簡単に流れだけ書くのだ

  1. Script Editor というアプリケーションを開く
  2. Apple Script を貼り付けて、ファイルを保存(ファイル名の例:M4A_Repair.scpt[2]
  3. 自動化したいフォルダ(M4Aファイルをドロップするフォルダ)を右クリック
  4. サービス > フォルダアクション設定... を選択
  5. 左側で自分のフォルダが選択されていることを確認し、右側の「+」ボタンを押して、先ほど保存したスクリプトファイルを選択
脚注
  1. AppleScript は、特定の用途、つまり macOS 上のアプリケーション操作の自動化に特化して設計されているのでいわゆる DSL(Domain-specific language)なのだ。ぼくは自動化したい時に結構使っていて便利なのだ。 ↩︎

  2. ダウンロードしたファイルの拡張子が現在は MP4 になっているので、このファイル名は変更した方がいいのだ ↩︎

Discussion