# [メタ情報] # 識別子: 字幕文章に番号を挿入する_exe # システム名: 字幕文章に番号を挿入する # 技術種別: Misc # 機能名: クイックアクション # 使用言語: AppleScript # 状態: 実行用 # [/メタ情報] ワークフローが受け取る現在の項目:ファイルまたはフォルダ 検索対象:すべてのアプリケーション AppleScriptを実行 -- 手順1: ファイルを複製して、_numaddedをファイル名に追加する on run {input, parameters} set vttFilePath to POSIX path of input -- 拡張子をVTTに置き換える set newFilePath to replaceExtension(vttFilePath, "vtt") -- ファイル名に_numaddedを追加する 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 & "_numadded.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: 番号を文章行に挿入する 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 currentNumber to "" set inTextBlock to false repeat with i from 1 to count fileLines set currentLine to item i of fileLines -- 番号行(数字のみ)をチェック if isNumeric(currentLine) then set currentNumber to currentLine -- 番号を保持する set newContent to newContent & currentLine & return -- 文章行の場合、番号を挿入する else if inTextBlock then set newContent to newContent & currentNumber & ") " & currentLine & return set inTextBlock to false -- 文章行が完了したので、フラグをリセットする -- 時刻行の場合はそのまま処理を続ける else if (offset of "-->" in currentLine) > 0 then set newContent to newContent & currentLine & return set inTextBlock to true -- 次の行は文章行になる可能性があるのでフラグを立てる -- その他の行(空白行など)はそのまま処理 else set newContent to newContent & currentLine & return end if 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 -- 数値かどうかを判定する補助関数 on isNumeric(value) try set value to value as number return true on error return false end try end isNumeric