AppleScript LRC

[SRT]SRTファイルをLRCファイルに変換する

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableString : a reference to refMe's NSMutableString
property refNSArray : a reference to refMe's NSArray
property refNSMutableArray : a reference to refMe's NSMutableArray
property refNSURL : a reference to refMe's NSURL


##############################################
## ファイルパス関連
##############################################
####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
####UTIリスト PDFのみ
set listUTI to {"org.niltsh.mplayerx-subrip"}
####プロンプトテキスト
set strPromptText to "SRTファイルを選んでください" as text
####ダイアログを出す
set aliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

####ファイルパス
set strFilePath to POSIX path of aliasFilePath as text
####ドキュメントのパスをNSString
set ocidFilePath to refNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidFilePathURL to refNSURL's fileURLWithPath:ocidFilePath

###ファイル名
set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
set strBaseFileName to (ocidBaseFilePathURL's lastPathComponent()) as text

####################################
#####LRCファイル保存先
####################################

set aliasDefaultLocation to (path to desktop folder from user domain) as alias
set strDefaultName to (strBaseFileName & ".lrc") as text
set strPromptText to "名前を決めてください"

set aliasSaveFilePath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
set strSaveFilePath to POSIX path of aliasSaveFilePath as text

####ドキュメントのパスをNSString
set ocidSaveFilePath to refNSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURL
set ocidSaveFilePathURL to refNSURL's fileURLWithPath:ocidSaveFilePath
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not "lrc" then
set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:"lrc"
end if


####################################
#####ファイル読み込み
####################################
####LRCデータ読み取り
set listReadDataString to refNSString's stringWithContentsOfURL:ocidFilePathURL encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
#####データ部
set ocidReadString to item 1 of listReadDataString
####エラー部
set ocidNSErrorData to item 2 of listReadDataString
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if
######Arrayの初期化
set ocidReadArray to refNSMutableArray's alloc()'s initWithCapacity:0
######改行毎のリストにして格納
set ocidReadArray to (ocidReadString's componentsSeparatedByString:"\n\n")
###行数
set numCntArrayItem to (count of ocidReadArray) as integer

##############################################
## 本処理
##############################################
####出力用のテキスト
set ocidOutPutString to refNSMutableString's alloc()'s initWithCapacity:0
set numCntLineNO to 0 as number
#####本処理
repeat (numCntArrayItem - 1) times
set objArrayItem to ocidReadArray's objectAtIndex:numCntLineNO
######Arrayの初期化
set ocidLineArray to refNSMutableArray's alloc()'s initWithCapacity:0
######改行毎のリストにして格納
set ocidLineArray to (objArrayItem's componentsSeparatedByString:"\n")
set ocidStartAndEndTime to (ocidLineArray's objectAtIndex:1)

######Arrayの初期化
set ocidTimeArray to refNSMutableArray's alloc()'s initWithCapacity:0
######リストにして格納
set ocidTimeArray to (ocidStartAndEndTime's componentsSeparatedByString:" ")
set strStartTime to (ocidTimeArray's objectAtIndex:0) as text
####出力用に整形
set strOutPutTime to ("[" & strStartTime & "]") as text
####時間挿入
(ocidOutPutString's appendString:strOutPutTime)
####テキスト取得
set strStringValue to (ocidLineArray's objectAtIndex:2) as text

####テキスト挿入
(ocidOutPutString's appendString:strStringValue)
#####改行
(ocidOutPutString's appendString:"\n")
###初期化
set ocidLineArray to {}
set ocidLineArray to {}
####カウントアップ
set numCntLineNO to numCntLineNO + 1 as number
end repeat

(ocidOutPutString's appendString:"\n")


##############################################
##ファイル出力
##############################################
###別名出力
set listWritetoUrlArray to ocidOutPutString's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
#####データ部
set boolDoneOK to item 1 of listWritetoUrlArray
log boolDoneOK
####エラー部
set ocidNSErrorData to item 2 of listWritetoUrlArray
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if

##############################################
## エラー発生時のログ用
##############################################


to doGetErrorData(ocidNSErrorData)
#####個別のエラー情報
log "エラーコード:" & ocidNSErrorData's code() as text
log "エラードメイン:" & ocidNSErrorData's domain() as text
log "Description:" & ocidNSErrorData's localizedDescription() as text
log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
log ocidNSErrorData's localizedRecoverySuggestion() as text
log ocidNSErrorData's localizedRecoveryOptions() as text
log ocidNSErrorData's recoveryAttempter() as text
log ocidNSErrorData's helpAnchor() as text
set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()
set ocidAllValues to ocidNSErrorUserInfo's allValues() as list
set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list
repeat with ocidKeys in ocidAllKeys
if (ocidKeys as text) is "NSUnderlyingError" then
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text
else
####それ以外の値はそのままテキストで読める
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end if
end repeat

end doGetErrorData

|

[TTML]TTMLをLRCファイルに変換する

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableString : a reference to refMe's NSMutableString

property refNSURL : a reference to refMe's NSURL

property refNSXMLElement : a reference to refMe's NSXMLElement
property refNSXMLParser : a reference to refMe's NSXMLParser
property refNSXMLDocument : a reference to refMe's NSXMLDocument
property refNSXMLNodePreserveWhitespace : a reference to refMe's NSXMLNodePreserveWhitespace


##############################################
## ファイルパス関連
##############################################
####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
####UTIリスト PDFのみ
set listUTI to {"dyn.ah62d4rv4ge81k7drru", "public.xml"}
####プロンプトテキスト
set strPromptText to "TTMLファイルを選んでください" as text
####ダイアログを出す
set aliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

####ファイルパス
set strFilePath to POSIX path of aliasFilePath as text
####ドキュメントのパスをNSString
set ocidFilePath to refNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidFilePathURL to refNSURL's fileURLWithPath:ocidFilePath

###ファイル名
set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
set strBaseFileName to (ocidBaseFilePathURL's lastPathComponent()) as text

####################################
#####LRCファイル保存先
####################################

set aliasDefaultLocation to (path to desktop folder from user domain) as alias
set strDefaultName to (strBaseFileName & ".lrc") as text
set strPromptText to "名前を決めてください"

set aliasSaveFilePath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
set strSaveFilePath to POSIX path of aliasSaveFilePath as text

####ドキュメントのパスをNSString
set ocidSaveFilePath to refNSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURL
set ocidSaveFilePathURL to refNSURL's fileURLWithPath:ocidSaveFilePath
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not "lrc" then
set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:"lrc"
end if


##############################################
## XML関連
##############################################
####テキストで読み込み
set listReadDataString to refNSString's stringWithContentsOfURL:ocidFilePathURL encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
#####データ部
set ocidReadString to item 1 of listReadDataString
####エラー部
set ocidNSErrorData to item 2 of listReadDataString
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if
#####XML定義
set listReadXML to refNSXMLDocument's alloc()'s initWithXMLString:ocidReadString options:(refNSXMLNodePreserveWhitespace) |error|:(reference)
#####データ部
set ocidReadXML to item 1 of listReadXML
####エラー部
set ocidNSErrorData to item 2 of listReadXML
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if
####ROOTエレメント
set ocidRootElement to ocidReadXML's rootElement()
####################
#####headエレメント
set ocidHeadElement to ocidRootElement's childAtIndex:0
#####stylingエレメント
set ocidStylingElement to ocidHeadElement's childAtIndex:0
#####layoutエレメント
set ocidLayoutElement to ocidHeadElement's childAtIndex:1
####################
#####Bodyエレメント
set ocidBodyElement to ocidRootElement's childAtIndex:1
#####Bodyに2要素-->複数レイアウトある場合は停止
set numCntDiv to ocidBodyElement's childCount() as number

if numCntDiv > 1 then
return "処理停止 複数レイアウト非対応"
end if

##############################################
## R0データ取得
##############################################
#####BODYR0エレメント
set ocidR0Element to ocidBodyElement's childAtIndex:0
#####R0に何行あるか
log ocidR0Element's childCount() as number

####出力用のテキスト
set ocidOutPutString to refNSMutableString's alloc()'s initWithCapacity:0
####項番用
set numCntLineNo to 1 as number
#####本処理
repeat with objR0Element in ocidR0Element's children()
####時間部分の処理
set ocidR0ObjectValue to objR0Element's XMLString()
log ocidR0ObjectValue as text
set ocidXMLStringArray to (ocidR0ObjectValue's componentsSeparatedByString:"\"")
set strStartTIme to (item 2 of ocidXMLStringArray) as text
####出力用に整形
set strOutPutTime to ("[" & strStartTIme & "]") as text
####時間挿入
(ocidOutPutString's appendString:strOutPutTime)
####テキスト取得
set strStringValue to objR0Element's stringValue()
####テキスト挿入
(ocidOutPutString's appendString:strStringValue)
#####改行
(ocidOutPutString's appendString:"\n")


set numCntLineNo to numCntLineNo + 1 as number
end repeat

(ocidOutPutString's appendString:"\n")


##############################################
##ファイル出力
##############################################
###別名出力
set listWritetoUrlArray to ocidOutPutString's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
#####データ部
set boolDoneOK to item 1 of listWritetoUrlArray
log boolDoneOK
####エラー部
set ocidNSErrorData to item 2 of listWritetoUrlArray
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if

##############################################
## エラー発生時のログ用
##############################################


to doGetErrorData(ocidNSErrorData)
#####個別のエラー情報
log "エラーコード:" & ocidNSErrorData's code() as text
log "エラードメイン:" & ocidNSErrorData's domain() as text
log "Description:" & ocidNSErrorData's localizedDescription() as text
log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
log ocidNSErrorData's localizedRecoverySuggestion() as text
log ocidNSErrorData's localizedRecoveryOptions() as text
log ocidNSErrorData's recoveryAttempter() as text
log ocidNSErrorData's helpAnchor() as text
set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()
set ocidAllValues to ocidNSErrorUserInfo's allValues() as list
set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list
repeat with ocidKeys in ocidAllKeys
if (ocidKeys as text) is "NSUnderlyingError" then
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text
else
####それ以外の値はそのままテキストで読める
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end if
end repeat

end doGetErrorData

|

[LRC]LRCファイルをTTMLファイルに変換する(DaVinci Resolve対応修正版)


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSArray : a reference to refMe's NSArray
property refNSMutableString : a reference to refMe's NSMutableString
property refNSMutableArray : a reference to refMe's NSMutableArray

property refNSURL : a reference to refMe's NSURL

##############################################
## ファイルパス関連
##############################################
####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
####UTIリスト PDFのみ
set listUTI to {"dyn.ah62d4rv4ge8026xd", "public.lrc", "public.text"}
####プロンプトテキスト
set strPromptText to "TTMLファイルを選んでください" as text
####ダイアログを出す
set aliasLRCFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

####ファイルパス
set strFilePath to POSIX path of aliasLRCFilePath as text
####ドキュメントのパスをNSString
set ocidFilePath to refNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidFilePathURL to refNSURL's fileURLWithPath:ocidFilePath

###ファイル名
set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
set strBaseFileName to (ocidBaseFilePathURL's lastPathComponent()) as text

####################################
#####SRTファイル保存先
####################################

set aliasDefaultLocation to (path to desktop folder from user domain) as alias
set strDefaultName to (strBaseFileName & ".ttml") as text
set strPromptText to "名前を決めてください"

set aliasSaveFilePath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
set strSaveFilePath to POSIX path of aliasSaveFilePath as text

####ドキュメントのパスをNSString
set ocidSaveFilePath to refNSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURL
set ocidSaveFilePathURL to refNSURL's fileURLWithPath:ocidSaveFilePath
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not "ttml" then
set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:"ttml"
end if



####################################
#####LRCファイル処理
####################################
####LRCデータ読み取り
set listReadDataString to refNSString's stringWithContentsOfURL:ocidFilePathURL encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
#####データ部
set ocidReadString to item 1 of listReadDataString
####エラー部
set ocidNSErrorData to item 2 of listReadDataString
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if
######Arrayの初期化
set ocidReadArray to refNSMutableArray's alloc()'s initWithCapacity:0
######改行毎のリストにして格納
set ocidReadArray to (ocidReadString's componentsSeparatedByString:"\n")


####################################
#####出力用テキスト
####################################
#####出力用テキストの初期化
set ocidOutPutString to refNSMutableString's alloc()'s initWithCapacity:0

###TTML
set strTTMLinit to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tt xml:lang=\"en\" xmlns=\"http://www.w3.org/ns/ttml\" xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\" xmlns:tts=\"http://www.w3.org/ns/ttml#styling\" xmlns:ttp=\"http://www.w3.org/ns/ttml#parameter\" xmlns:ittp=\"http://www.w3.org/ns/ttml/profile/imsc1#parameter\" xmlns:itts=\"http://www.w3.org/ns/ttml/profile/imsc1#styling\" ttp:profile=\"http://www.w3.org/ns/ttml/profile/imsc1/text\" ttp:frameRate=\"50\" ttp:timeBase=\"media\">\n" as text
(ocidOutPutString's appendString:strTTMLinit)

###styling
set strStylinkg to "<head>\n<styling>\n<style xml:id=\"S0\" tts:color=\"#ffffff\" tts:opacity=\"1\" tts:fontSize=\"100%\" tts:fontFamily=\"default\" tts:fontStyle=\"italic\" tts:textAlign=\"start\"/>\n</styling>\n" as text
(ocidOutPutString's appendString:strStylinkg)

###layout
set strLayout to "<layout>\n<region xml:id=\"R0\" tts:origin=\"0.000% 80%\" tts:extent=\"100.000% 10%\" tts:displayAlign=\"center\"/>\n</layout>\n</head>\n" as text
(ocidOutPutString's appendString:strLayout)

####body
set strStartBody to "<body>\n<div xml:id=\"D0\" region=\"R0\" style=\"S0\">" as text
(ocidOutPutString's appendString:strStartBody)

####################################
#####本処理
####################################
#####読み取ったリストの個数を確認
set numCntReadLineNo to (count of ocidReadArray) as integer
set numCntLineNo to 0 as integer
#####行数1回繰り返す
repeat (numCntReadLineNo - 1) times

##############スタート時間
####リストから1行分取り出し
set objArrayItem to ocidReadArray's objectAtIndex:numCntLineNo
set strArrayItem to objArrayItem as text
###時間部の終わり記号でリスト
set AppleScript's text item delimiters to "]"
set listReadArrayItem to every text item of strArrayItem
set AppleScript's text item delimiters to ""
####最初のアイテムが時間部 後のアイテムがテキスト部
set strArrayItemTime to text item 1 of listReadArrayItem as text
set strPtext to text item 2 of listReadArrayItem as text

###タイムコードの終わりまでの文字数
set numOffset to (count character of strArrayItemTime) as integer
###タイムコード部取り出し
set strStartArrayItemTime to (text 1 through numOffset of strArrayItemTime) as text
set strStartArrayItemTime to doReplace(strStartArrayItemTime, "[", "") as text
set strStartArrayItemTime to doReplace(strStartArrayItemTime, "]", "") as text
set strStartTime to "00:" & strStartArrayItemTime as text
##############エンド時間
###次の行を取り出し
set objNextArrayItem to ocidReadArray's objectAtIndex:(numCntLineNo + 1)
set strNextReadArrayItem to objNextArrayItem as text
if strNextReadArrayItem is "" then
###次の行が無い(最後の行)はスタートタイムに3秒足す
set AppleScript's text item delimiters to ":"
set listNextTime to every text item of strStartTime
set AppleScript's text item delimiters to ""
set strSec to text item 3 of listNextTime as text
set numSec to strSec as number
set numSec to strSec + 3 as number
if numSec < 10 then
set strSec to (text 1 through 2 of ("0" & numSec) as text) & ".00" as text
else
set strSec to numSec as text
end if
set strNextReadArrayItem to (text item 2 of listNextTime as text) & ":" & strSec as text
set strEndTime to "00:" & strNextReadArrayItem as text
else
####次の行の時間を読み取って終わり時間にする
set numOffset to (offset in strNextReadArrayItem of "]") as integer
###タイムコード部取り出し
set strEndArrayItemTime to (text 1 through numOffset of strNextReadArrayItem) as text
set strEndArrayItemTime to doReplace(strEndArrayItemTime, "[", "") as text
set strEndArrayItemTime to doReplace(strEndArrayItemTime, "]", "") as text
set strEndTime to "00:" & strEndArrayItemTime as text
end if



set strDivLine to "<p begin=\"" & strStartTime & "\" end=\"" & strEndTime & "\">" & strPtext & "</p>\n"
(ocidOutPutString's appendString:strDivLine)
####カウントアップ
set numCntLineNo to numCntLineNo + 1 as integer
end repeat

####終了部
set strEndBody to "</div>\n</body>\n</tt>\n" as text
(ocidOutPutString's appendString:strEndBody)

####################################
#####保存
####################################
set boolWritetoUrlArray to ocidOutPutString's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
log boolWritetoUrlArray

####################################
#####文字の置き換え
####################################

to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace

|

[LRC]LRCファイルをSRTファイルに変換する(DaVinci Resolve対応修正版)

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSArray : a reference to refMe's NSArray
property refNSMutableString : a reference to refMe's NSMutableString
property refNSMutableArray : a reference to refMe's NSMutableArray

property refNSURL : a reference to refMe's NSURL

##############################################
## ファイルパス関連
##############################################
####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
####UTIリスト PDFのみ
set listUTI to {"dyn.ah62d4rv4ge8026xd", "public.lrc", "public.text"}
####プロンプトテキスト
set strPromptText to "TTMLファイルを選んでください" as text
####ダイアログを出す
set aliasLRCFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

####ファイルパス
set strFilePath to POSIX path of aliasLRCFilePath as text
####ドキュメントのパスをNSString
set ocidFilePath to refNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidFilePathURL to refNSURL's fileURLWithPath:ocidFilePath
###ファイル名
set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
set strBaseFileName to (ocidBaseFilePathURL's lastPathComponent()) as text

####################################
#####SRTファイル保存先
####################################

set aliasDefaultLocation to (path to desktop folder from user domain) as alias
set strDefaultName to (strBaseFileName & ".srt") as text
set strPromptText to "名前を決めてください"

set aliasSaveFilePath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
set strSaveFilePath to POSIX path of aliasSaveFilePath as text

####ドキュメントのパスをNSString
set ocidSaveFilePath to refNSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURL
set ocidSaveFilePathURL to refNSURL's fileURLWithPath:ocidSaveFilePath


####################################
#####LRCファイル処理
####################################
####LRCデータ読み取り
set listReadDataString to refNSString's stringWithContentsOfURL:ocidFilePathURL encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
#####データ部
set ocidReadString to item 1 of listReadDataString
####エラー部
set ocidNSErrorData to item 2 of listReadDataString
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if
######Arrayの初期化
set ocidReadArray to refNSMutableArray's alloc()'s initWithCapacity:0
######改行毎のリストにして格納
set ocidReadArray to (ocidReadString's componentsSeparatedByString:"\n")

####################################
#####出力用テキスト
####################################
#####出力用テキストの初期化
set ocidOutPutString to refNSMutableString's alloc()'s initWithCapacity:0
#####読み取ったリストの個数を確認
set numCntReadLineNo to (count of ocidReadArray) as integer
set numCntLineNo to 0 as integer
set strSRTlineNo to 0 as integer
repeat (numCntReadLineNo - 1) times
###行番号

set objArrayItem to ocidReadArray's objectAtIndex:numCntLineNo

set strReadArrayItem to objArrayItem as text
if (text 1 through 11 of strReadArrayItem) is "[00:00.000]" then
log "情報部"

else if (text 1 through 10 of strReadArrayItem) is "[00:00.00]" then
log "タイトル部"

else
set strSRTlineNo to strSRTlineNo + 1 as text
(ocidOutPutString's appendString:strSRTlineNo)
(ocidOutPutString's appendString:"\n")
set AppleScript's text item delimiters to "]"
set listReadArrayItem to every text item of strReadArrayItem
set AppleScript's text item delimiters to ""
set strLyric to text item 2 of listReadArrayItem as text

set numOffset to (offset in strReadArrayItem of "]") as integer

set strReadArrayItemTime to (text 1 through numOffset of strReadArrayItem) as text

###########################
set strReadArrayItemTime to doReplace(strReadArrayItemTime, "[", "")
set strReadArrayItemTime to doReplace(strReadArrayItemTime, "]", "")
###########################
set strReadArrayItemTimeAdd to "00:" & strReadArrayItemTime as text
set strReadArrayItemTimeAdd to doReplace(strReadArrayItemTimeAdd, ".", ",") as text
(ocidOutPutString's appendString:strReadArrayItemTimeAdd)


(ocidOutPutString's appendString:" --> ")
set objNextArrayItem to ocidReadArray's objectAtIndex:(numCntLineNo + 1)
set strNextReadArrayItem to objNextArrayItem as text
if strNextReadArrayItem is "" then
set strNextReadArrayItem to doReplace(strReadArrayItemTime, "[", "") as text
set strNextReadArrayItem to doReplace(strNextReadArrayItem, "]", "") as text
set AppleScript's text item delimiters to ":"
set listNextTime to every text item of strNextReadArrayItem
set AppleScript's text item delimiters to ""
set strSec to text item 2 of listNextTime as text
set numSec to strSec as number
set numSec to strSec + 3 as number
if numSec < 10 then
set strSec to (text 1 through 2 of ("0" & numSec) as text) & ".00" as text
else
set strSec to numSec as text
end if
set strNextReadArrayItem to (text item 1 of listNextTime as text) & ":" & strSec as text
set strNextReadArrayItem to "00:" & strNextReadArrayItem as text
set strNextReadArrayItem to doReplace(strNextReadArrayItem, ".", ",") as text
(ocidOutPutString's appendString:strNextReadArrayItem)
(ocidOutPutString's appendString:"\n")
(ocidOutPutString's appendString:strLyric)
(ocidOutPutString's appendString:"\n")
(ocidOutPutString's appendString:"\n")
exit repeat
else
set numOffset to (offset in strNextReadArrayItem of "]") as integer
set strNextReadArrayItem to (text 1 through numOffset of strNextReadArrayItem) as text
###########################
set strNextReadArrayItem to doReplace(strNextReadArrayItem, "[", "") as text
set strNextReadArrayItem to doReplace(strNextReadArrayItem, "]", "") as text
set strNextReadArrayItem to "00:" & strNextReadArrayItem as text
set strNextReadArrayItem to doReplace(strNextReadArrayItem, ".", ",") as text
###########################
(ocidOutPutString's appendString:strNextReadArrayItem)
(ocidOutPutString's appendString:"\n")
(ocidOutPutString's appendString:strLyric)
(ocidOutPutString's appendString:"\n")
(ocidOutPutString's appendString:"\n")
end if
end if

set numCntLineNo to numCntLineNo + 1 as number
end repeat
(ocidOutPutString's appendString:"\n")


log ocidOutPutString as text

####################################
#####保存
####################################
set boolWritetoUrlArray to ocidOutPutString's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)


####################################
#####文字の置き換え
####################################

to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace

|

[LRC]LRCファイルをSRTファイルに変換する

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSArray : a reference to refMe's NSArray
property refNSMutableString : a reference to refMe's NSMutableString
property refNSMutableArray : a reference to refMe's NSMutableArray

property refNSURL : a reference to refMe's NSURL

##############################################
## ファイルパス関連
##############################################
####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
####UTIリスト PDFのみ
set listUTI to {"dyn.ah62d4rv4ge8026xd", "public.lrc", "public.text"}
####プロンプトテキスト
set strPromptText to "TTMLファイルを選んでください" as text
####ダイアログを出す
set aliasLRCFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

####ファイルパス
set strFilePath to POSIX path of aliasLRCFilePath as text
####ドキュメントのパスをNSString
set ocidFilePath to refNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidFilePathURL to refNSURL's fileURLWithPath:ocidFilePath
###ファイル名
set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
set strBaseFileName to (ocidBaseFilePathURL's lastPathComponent()) as text

####################################
#####SRTファイル保存先
####################################

set aliasDefaultLocation to (path to desktop folder from user domain) as alias
set strDefaultName to (strBaseFileName & ".srt") as text
set strPromptText to "名前を決めてください"

set aliasSaveFilePath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
set strSaveFilePath to POSIX path of aliasSaveFilePath as text

####ドキュメントのパスをNSString
set ocidSaveFilePath to refNSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURL
set ocidSaveFilePathURL to refNSURL's fileURLWithPath:ocidSaveFilePath


####################################
#####LRCファイル処理
####################################
####LRCデータ読み取り
set listReadDataString to refNSString's stringWithContentsOfURL:ocidFilePathURL encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
#####データ部
set ocidReadString to item 1 of listReadDataString
####エラー部
set ocidNSErrorData to item 2 of listReadDataString
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if
######Arrayの初期化
set ocidReadArray to refNSMutableArray's alloc()'s initWithCapacity:0
######改行毎のリストにして格納
set ocidReadArray to (ocidReadString's componentsSeparatedByString:"\n")

####################################
#####出力用テキスト
####################################
#####出力用テキストの初期化
set ocidOutPutString to refNSMutableString's alloc()'s initWithCapacity:0
#####読み取ったリストの個数を確認
set numCntReadLineNo to (count of ocidReadArray) as integer
set numCntLineNo to 0 as integer
set strSRTlineNo to 0 as integer
repeat (numCntReadLineNo - 1) times
###行番号

set objArrayItem to ocidReadArray's objectAtIndex:numCntLineNo

set strReadArrayItem to objArrayItem as text
if (text 1 through 11 of strReadArrayItem) is "[00:00.000]" then
log "情報部"

else if (text 1 through 10 of strReadArrayItem) is "[00:00.00]" then
log "タイトル部"

else
set strSRTlineNo to strSRTlineNo + 1 as text
(ocidOutPutString's appendString:strSRTlineNo)
(ocidOutPutString's appendString:"\n")
set AppleScript's text item delimiters to "]"
set listReadArrayItem to every text item of strReadArrayItem
set AppleScript's text item delimiters to ""
set strLyric to text item 2 of listReadArrayItem as text

set strReadArrayItemTime to (text 1 through 11 of strReadArrayItem) as text

###########################
set strReadArrayItemTime to doReplace(strReadArrayItemTime, "[", "")
set strReadArrayItemTime to doReplace(strReadArrayItemTime, "]", "")
set strReadArrayItemTime to doReplace(strReadArrayItemTime, "L", "0")
###########################
(ocidOutPutString's appendString:strReadArrayItemTime)
(ocidOutPutString's appendString:" --> ")
set objNextArrayItem to ocidReadArray's objectAtIndex:(numCntLineNo + 1)
set strNextReadArrayItem to objNextArrayItem as text
if strNextReadArrayItem is "" then
set strNextReadArrayItem to doReplace(strReadArrayItemTime, "[", "") as text
set strNextReadArrayItem to doReplace(strNextReadArrayItem, "]", "") as text
set strNextReadArrayItem to doReplace(strNextReadArrayItem, "L", "0") as text
set AppleScript's text item delimiters to ":"
set listNextTime to every text item of strNextReadArrayItem
set AppleScript's text item delimiters to ""
set strSec to text item 2 of listNextTime as text
set numSec to strSec as number
set numSec to strSec + 3 as number
set strSec to (text -1 through -6 of ("0" & numSec) as text) as text
set strNextReadArrayItem to (text item 1 of listNextTime as text) & ":" & strSec as text
(ocidOutPutString's appendString:strNextReadArrayItem)
(ocidOutPutString's appendString:"\n")
(ocidOutPutString's appendString:strLyric)
(ocidOutPutString's appendString:"\n")
(ocidOutPutString's appendString:"\n")
exit repeat
else
set strNextReadArrayItem to (text 1 through 11 of strNextReadArrayItem) as text
###########################
set strNextReadArrayItem to doReplace(strNextReadArrayItem, "[", "") as text
set strNextReadArrayItem to doReplace(strNextReadArrayItem, "]", "") as text
set strNextReadArrayItem to doReplace(strNextReadArrayItem, "L", "0") as text
###########################
(ocidOutPutString's appendString:strNextReadArrayItem)
(ocidOutPutString's appendString:"\n")
(ocidOutPutString's appendString:strLyric)
(ocidOutPutString's appendString:"\n")
(ocidOutPutString's appendString:"\n")
end if
end if

set numCntLineNo to numCntLineNo + 1 as number
end repeat
(ocidOutPutString's appendString:"\n")


log ocidOutPutString as text

####################################
#####保存
####################################
set boolWritetoUrlArray to ocidOutPutString's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)


####################################
#####文字の置き換え
####################################

to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom