# [メタ情報]
# 識別子: vttから字幕一覧用txtを生成workflow_exe
# システム名: vttから字幕一覧用txtを生成、タイムコードへジャンプする_listJ.txtおよびジャンプしない_list.txtを生成
# 技術種別: Misc
# 機能名: クイックアクション
# 使用言語: AppleScript
# 状態: 実行用
# [/メタ情報]
ワークフローが受け取る現在の項目:ファイルまたはフォルダ
検索対象:すべてのアプリケーション
AppleScriptを実行
on run {input, parameters}
-- ▼ 入力: 右クリックで選んだ .vtt 1件
set vttPosix to POSIX path of (item 1 of input)
set vttText to read (vttPosix as POSIX file) as «class utf8»
set vttLines to paragraphs of vttText
-- ▼ 出力ファイル名(同じフォルダ/上書き)
set {parentDir, baseNoExt} to my splitPath(vttPosix)
set outListPath to parentDir & "/" & baseNoExt & "_list.txt" -- ジャンプなし(本文そのまま)
set outListJPath to parentDir & "/" & baseNoExt & "_listJ.txt" -- ジャンプあり(リンク付き)
-- ▼ 共通テンプレ(Apple 翻訳用に translate="yes" を付与)
set headerHTML to "字幕一覧(クリック)
" & linefeed
set footerHTML to "
" & linefeed
-- ▼ 字幕一覧用スタイル
set styleHTML to "" & linefeed
-- ▼ 本文生成(2種類)
set bodyList to "" -- ジャンプなし(VTT本文をそのまま + )
set bodyListJ to "" -- ジャンプあり(時刻リンク + 本文(先頭の時刻括弧は除去) + )
set i to 1
set maxI to (count of vttLines)
repeat while i ≤ maxI
set rowText to item i of vttLines
if (rowText contains "-->") then
-- 1〜12桁: 開始 "HH:MM:SS.mmm"
set startStr to ""
try
set startStr to text 1 thru 12 of rowText
end try
-- 次行以降: 字幕本文を収集(空行で終了/番号行はスキップ)
set captionTextRaw to ""
set j to (i + 1)
repeat while j ≤ maxI
set t to item j of vttLines
if (t = "") then exit repeat
if (my isIndexLike(t)) then
set j to j + 1
else
if captionTextRaw = "" then
set captionTextRaw to t
else
set captionTextRaw to captionTextRaw & " " & t
end if
set j to j + 1
end if
end repeat
-- ▼ _listJ.txt 用(見やすさのため、先頭の (hh:mm:ss) があれば除去)
set captionForJ to captionTextRaw
if captionForJ starts with "(" then
try
if text 2 thru 9 of captionForJ contains ":" then
-- (hh:mm:ss) を取り除く(閉じ括弧 ')' の後ろから末尾まで)
set captionForJ to my trim(text 11 thru -1 of captionForJ)
end if
end try
end if
-- ▼ ジャンプあり用(_listJ.txt)
if startStr is not "" then
set hh to my toIntSafe(text 1 thru 2 of startStr)
set mm to my toIntSafe(text 4 thru 5 of startStr)
set ss to my toIntSafe(text 7 thru 8 of startStr)
set totalMin to (hh * 60 + mm)
set seekTag to (totalMin as string) & ":" & my z2(ss)
set displayStamp to text 1 thru 8 of startStr
-- ※ 時刻は翻訳させない(translate="no")
set oneJ to "(" & "" & displayStamp & "" & ") " & captionForJ & " " & linefeed
set bodyListJ to bodyListJ & oneJ
end if
-- ▼ ジャンプなし用(_list.txt): VTT本文そのまま
if captionTextRaw is not "" then
set bodyList to bodyList & captionTextRaw & " " & linefeed
end if
set i to j
else
set i to i + 1
end if
end repeat
-- ▼ ファイル保存(本文+footer+style)
my writeUTF8(outListPath, headerHTML & bodyList & footerHTML & styleHTML)
my writeUTF8(outListJPath, headerHTML & bodyListJ & footerHTML & styleHTML)
return {outListPath, outListJPath}
end run
-- ===== ヘルパ =====
on splitPath(p)
set AppleScript's text item delimiters to "/"
set xs to text items of p
set fname to item -1 of xs
set parentDir to (items 1 thru -2 of xs) as text
set AppleScript's text item delimiters to ""
set baseNoExt to my removeExt(fname)
return {parentDir, baseNoExt}
end splitPath
on removeExt(fn)
if fn contains "." then
set AppleScript's text item delimiters to "."
set ys to text items of fn
set AppleScript's text item delimiters to ""
return (items 1 thru -2 of ys) as text
else
return fn
end if
end removeExt
on isIndexLike(s)
set t to my trim(s)
if t = "" then return true
try
if (t as integer is not missing value) then return true
end try
if (text 1 of t = "(" and text -1 of t = ")" and my isDigits(text 2 thru -2 of t)) then return true
if (text -1 of t = ")" and my isDigits(text 1 thru -2 of t)) then return true
if (text -1 of t = "." and my isDigits(text 1 thru -2 of t)) then return true
return false
end isIndexLike
on isDigits(s)
if s = "" then return false
repeat with k from 1 to length of s
set c to character k of s
if c is not in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"} then return false
end repeat
return true
end isDigits
on trim(s)
set txd to AppleScript's text item delimiters
set AppleScript's text item delimiters to {space, tab, return, linefeed}
set xs to text items of s
set AppleScript's text item delimiters to " "
set r to xs as text
set AppleScript's text item delimiters to txd
return r
end trim
on toIntSafe(s)
try
return s as integer
on error
return 0
end try
end toIntSafe
on z2(n)
set nStr to (n as string)
if (length of nStr) = 1 then return "0" & nStr
return nStr
end z2
on writeUTF8(p, txt)
set f to open for access (p as POSIX file) with write permission
try
set eof of f to 0
write txt to f as «class utf8»
end try
close access f
end writeUTF8