# [メタ情報] # 識別子: srtからvttを生成するworkflow_exe # システム名: srtからvttを生成する # 技術種別: Misc # 機能名: クイックアクション # 使用言語: AppleScript # 状態: 実行用 # [/メタ情報] クイックアクション srtからvttを生成する.worklflow ワークフローが受け取る現在の項目:ファイルまたはフォルダ AppleScriptを実行 -- 手順1 内容を複製した新たなvttファイルを作成する。 on run {input, parameters} set srtFilePath to POSIX path of input -- 拡張子をVTTに置き換える set newFilePath to replaceExtension(srtFilePath, "vtt") -- ファイル名を作成する set fileName to do shell script "basename " & quoted form of srtFilePath set baseName to text 1 thru ((offset of "." in fileName) - 1) of fileName set newFileName to baseName & ".vtt" -- 新しいファイルのパスを生成する set newFilePath to (text 1 thru -((count fileName) + 1) of srtFilePath) & newFileName -- ファイルをコピーして新しいファイルを作成する do shell script "cp " & quoted form of srtFilePath & " " & quoted form of newFilePath -- 手順2に新しいファイルのパスを渡す 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ファイルに対して、 -- (1) 先頭行に文字列"WEBVTT"を含む行を挿入する。 -- (2) 2行目に空白の行を挿入する。 -- (3) 3行目以降から、読み込んだデータを書き込む。 -- (4) "-->" がある行は、行の中の","を"."に置き換える。 on run {input, parameters} set newFilePath to POSIX path of (first item of input) -- ファイル内容を読み込む set fileContent to read newFilePath as «class utf8» set fileLines to paragraphs of fileContent set newContent to "WEBVTT" & linefeed & linefeed -- 条件に基づいてファイル内容を更新 repeat with i from 1 to count fileLines set lineText to item i of fileLines if i > 1 then -- 読み込みデータの2行目以降 if lineText contains "-->" then -- (4) set lineText to textReplace(lineText, ",", ".") end if end if set newContent to newContent & lineText & linefeed end repeat -- ファイルに新しい内容を書き込む set fileHandle to open for access newFilePath with write permission set eof of fileHandle to 0 write newContent to fileHandle as «class utf8» close access fileHandle return input end run -- 文字列中の特定の文字を置き換える on textReplace(inputText, findText, replaceText) set AppleScript's text item delimiters to findText set textItems to text items of inputText set AppleScript's text item delimiters to replaceText set newText to textItems as text set AppleScript's text item delimiters to "" return newText end textReplace