# [メタ情報] # 識別子: 字幕文章の番号を削除するworkflow_exe # システム名: 字幕文章の番号を削除する # 技術種別: Misc # 機能名: クイックアクション # 使用言語: AppleScript # 状態: 実行用 # [/メタ情報] ワークフローが受け取る現在の項目:ファイルまたはフォルダ 検索対象:すべてのアプリケーション 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