# [メタ情報] # 識別子: 字幕文章の時刻を削除するworkflow_exe # システム名: 字幕文章の時刻を削除する # 技術種別: Misc # 機能名: クイックアクション # 使用言語: AppleScript # 状態: 実行用 # [/メタ情報] クイックアクション 字幕文章の時刻を削除する.worklflow ワークフローが受け取る現在の項目:ファイルまたはフォルダ AppleScriptを実行 -- 手順1 内容を複製した新たなファイルを作成する。 on run {input, parameters} set vttFilePath to POSIX path of input -- 拡張子をVTTに置き換える set newFilePath to replaceExtension(vttFilePath, "vtt") -- ファイル名に_timeremovedを追加する 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 & "_timeremoved.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 paragraphsList to paragraphs of fileContent repeat with aParagraph in paragraphsList -- 行の先頭に "(00:00:00)" という形式がある場合に処理する if (aParagraph starts with "(" and (length of aParagraph ≥ 10) and (character 4 of aParagraph = ":" and character 7 of aParagraph = ":" and character 10 of aParagraph = ")")) then -- 時刻情報を削除し、文章を左詰にする set newParagraph to text 11 thru -1 of aParagraph else set newParagraph to aParagraph end if -- 行の左詰処理 if newParagraph is not "" then set newParagraph to (do shell script "echo " & quoted form of newParagraph & " | awk '{$1=$1;print}'") end if set newContent to newContent & newParagraph & linefeed 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