# [メタ情報] # 識別子: 字幕文章の番号を削除するworkflow_exe # システム名: 字幕文章の番号を削除する # 技術種別: Misc # 機能名: クイックアクション # 使用言語: AppleScript # 状態: 実行用 env終了 # [/メタ情報] 要約: このAppleScriptワークフローは、入力ファイルを受け取り、VTT形式の字幕ファイルを整形する二段階の処理を行います。 まず「手順1」では、入力されたファイルを「\_numremoved.vtt」という命名規則で新たなVTTファイルとして複製します。具体的には、元のファイルパスから拡張子を「.vtt」に変更し、ファイル名の末尾に「\_numremoved」を追加した新規ファイルを生成し、元の内容をコピーします。 次に「手順2」では、この新たに作成されたVTTファイルの内容を修正します。ファイルの各行を読み込み、時刻情報を示す行(「00:」で始まる行)は変更せずそのまま維持します。それ以外の行については、先頭に「数字 スペース」というパターンがある場合、その部分を削除します。さらに、処理後の行が空でなければ、テキスト全体を左詰めに整形します。この一連の処理によって、VTTファイル内の番号と余分な空白が除去され、テキストが整然と配置されます。修正された内容は、元のVTTファイルに上書き保存されます。 :ここまで要約 ワークフローが受け取る現在の項目:ファイルまたはフォルダ 検索対象:すべてのアプリケーション AppleScriptを実行 ``` -- 手順1: 内容を複製した新たなファイルを作成する。 on run {input, parameters} set vttFilePath to POSIX path of input -- 拡張子をVTTに置き換える set newFilePath to replaceExtension(vttFilePath, "vtt") -- ファイル名に_numremovedを追加する 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 & "_numremoved.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 ``` AppleScriptを実行 ``` -- 手順2: 新たに作った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 "" repeat with currentLine in fileLines -- 時刻行の場合はそのまま追加 if currentLine starts with "00:" then set newParagraph to currentLine else -- 行の先頭に「数字 」という形式がある場合に処理する set numEndPos to (offset of " " in currentLine) if numEndPos > 0 then -- 先頭の番号部分を削除する set newParagraph to text (numEndPos + 1) thru -1 of currentLine else set newParagraph to currentLine end if -- 行の左詰処理 if newParagraph is not "" then set newParagraph to (do shell script "echo " & quoted form of newParagraph & " | awk '{$1=$1;print}'") end if end if set newContent to newContent & newParagraph & return 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 ```