# [メタ情報] # 識別子: 字幕文章の時刻を削除するworkflow_exe # システム名: 字幕文章の時刻を削除する # 技術種別: Misc # 機能名: クイックアクション # 使用言語: AppleScript # 状態: 実行用 env終了 # [/メタ情報] 要約: このクイックアクションは、VTT形式の字幕ファイルから時刻情報を削除するAppleScriptワークフローです。 処理は二段階です。まず、入力されたVTTファイルを複製し、ファイル名に「_timeremoved」を付加した新たなファイルを作成します。元のファイルを保護し、編集用複製を作成します。 次に、複製ファイルの内容を読み込み、各行を検査します。行の先頭に「(時:分:秒)」形式の時刻情報がある場合、その時刻部分を削除し、残りの字幕文章を左詰めに整形します。 最終的に、時刻削除・整形済みの内容は複製ファイルに上書き保存されます。これにより、元のVTTファイルに影響を与えることなく、時刻情報のない新しい字幕ファイルを生成します。 :ここまで要約 クイックアクション 字幕文章の時刻を削除する.worklflow ワークフローが受け取る現在の項目:ファイルまたはフォルダ AppleScriptを実行 ``` -- 手順1 内容を複製した新たなファイルを作成する。 on run {input, parameters} set vttFilePath to POSIX path of input -- 拡張子をVTTに置き換える set newFilePath to replaceExtension(vttFilePath, "vtt") -- ファイル名に_timeremovedを追加する set fileName to do shell script "basename " & quoted form of vttFilePath set baseName to text 1 thru ((offset of "." in fileName) - 1) of fileName set newFileName to baseName & "_timeremoved.vtt" -- 新しいファイルのパスを生成する set newFilePath to (text 1 thru -((count fileName) + 1) of vttFilePath) & newFileName -- ファイルをコピーして新しいファイルを作成する do shell script "cp " & quoted form of vttFilePath & " " & quoted form of newFilePath -- return {newFilePath} end run -- 拡張子を置き換える補助関数 on replaceExtension(filePath, newExtension) set AppleScript's text item delimiters to "." set pathItems to text items of filePath set lastItem to last item of pathItems set last item of pathItems to newExtension set AppleScript's text item delimiters to "" set newPath to pathItems as text return newPath end replaceExtension ``` ``` -- 手順2 次に、手順1で新たに作ったVTTファイルに対して、以下のアクションを実行する。 on run {input, parameters} set vttFilePath to POSIX path of (first item of input) -- ファイル内容を読み込む set fileContent to read vttFilePath as «class utf8» set fileLines to paragraphs of fileContent -- 行単位で処理する set newContent to "" set paragraphsList to paragraphs of fileContent repeat with aParagraph in paragraphsList -- 行の先頭に "(00:00:00)" という形式がある場合に処理する if (aParagraph starts with "(" and (length of aParagraph ≥ 10) and (character 4 of aParagraph = ":" and character 7 of aParagraph = ":" and character 10 of aParagraph = ")")) then -- 時刻情報を削除し、文章を左詰にする set newParagraph to text 11 thru -1 of aParagraph else set newParagraph to aParagraph end if -- 行の左詰処理 if newParagraph is not "" then set newParagraph to (do shell script "echo " & quoted form of newParagraph & " | awk '{$1=$1;print}'") end if set newContent to newContent & newParagraph & linefeed end repeat -- ファイルに新しい内容を書き込む set fileHandle to open for access vttFilePath with write permission set eof of fileHandle to 0 write newContent to fileHandle as «class utf8» close access fileHandle return input end run ```