# [メタ情報] # 識別子: 字幕文章に時刻を挿入する_exe # システム名: 字幕文章に時刻を挿入する # 技術種別: Misc # 機能名: クイックアクション # 使用言語: AppleScript # 状態: 実行用 # [/メタ情報] クイックアクション 字幕文章に時刻を挿入する.worklflow ワークフローが受け取る現在の項目:ファイルまたはフォルダ AppleScriptを実行 -- 手順1 内容を複製した新たなファイルを作成する。 on run {input, parameters} set vttFilePath to POSIX path of input -- 拡張子をVTTに置き換える set newFilePath to replaceExtension(vttFilePath, "vtt") -- ファイル名に_timeaddedを追加する 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 & "_timeadded.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 次に、手順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 inStartTimeBlock to false repeat with i from 1 to count fileLines set currentLine to item i of fileLines if inStartTimeBlock then -- Start timeが記述された行の、次の行に書かれた文字列をtxtexpへ入れる set txtexp to currentLine -- Start timeが記述された行の、次の行に、文字列[letterstartTime] と文字列txtexpを連結する set newContent to newContent & "(" & letterstartTime & ") " & txtexp & return set inStartTimeBlock to false else if (offset of "-->" in currentLine) > 0 then set startTime to text 1 thru 8 of currentLine set letterstartTime to replace_chars(startTime, ":", ":") set startTimeFormatted to "(" & letterstartTime & ")" set newContent to newContent & currentLine & return set inStartTimeBlock 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 replace_chars(txt, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set txtItems to every text item of txt set AppleScript's text item delimiters to the replacement_string set txt to txtItems as text set AppleScript's text item delimiters to "" return txt end replace_chars