🍎
【Mac】NotebookLMでダウンロードした音声ファイルを修復するコマンド実行を自動化するAppleScript
Macユーザー向け。コマンド実行を AppleScript 書いて自動化。
はじめに
前回の記事で NotebookLM からダウンロードした M4A ファイル
が壊れている問題と、afconvert
コマンドで修復する方法を書いたのだ。
コマンド例:
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
スクリプト実行設定方法
簡単に流れだけ書くのだ
- Script Editor というアプリケーションを開く
- Apple Script を貼り付けて、ファイルを保存(ファイル名の例:M4A_Repair.scpt[2])
- 自動化したいフォルダ(M4Aファイルをドロップするフォルダ)を右クリック
- サービス > フォルダアクション設定... を選択
- 左側で自分のフォルダが選択されていることを確認し、右側の「+」ボタンを押して、先ほど保存したスクリプトファイルを選択
Discussion