Here is my final script that will batch process the text documents I drop on it:
on open theDroppedItems
set countOne to 1
set listCount to 1
repeat until listCount > (count theDroppedItems)
tell application "TextWrangler"
activate
set Doc_to_open to item countOne of theDroppedItems
open Doc_to_open
end tell
deleteHeader()
tell application "TextWrangler"
activate
set searchOptions to {search mode:grep, starting at top:true, case sensitive:true}
set noFind to false
repeat until (noFind)
set findResult to (find "\\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]\\]" searching in text 1 of text document 1 options searchOptions)
if (found of findResult) then
set theTime to found text of findResult -- Get the text directly from the returned record.
set {theTimePlusFour, lastTimeInSeconds} to my addSecondsToTimeCode(4, text 2 thru -2 of theTime)
set theTimePlusFour to "[" & theTimePlusFour & "]"
set contents of found object of findResult to theTimePlusFour -- Replace the text directly in the document.
set starting at top of searchOptions to false -- Neutralise this option for subsequent searches.
else
set noFind to true
save text document 1
close text window 1
end if
end repeat
end tell
set countOne to countOne + 1
set listCount to listCount + 1
end repeat
end open
on deleteHeader()
tell application "TextWrangler"
activate
find "{QTtext}" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:true} with selecting match
find "[00:00:00.00]" searching in text 1 of text document 1 options {search mode:literal, starting at top:false, wrap around:true, extend selection:true} with selecting match
delete selection
end tell
end deleteHeader
on addSecondsToTimeCode(s, timeCode)
-- Turn the time code into seconds and add the specified amount.
tell timeCode to set t to (text 1 thru 2) * hours + (text 4 thru 5) * minutes + (text 7 thru 8) + s
-- Convert back to HH:MM:SS.
tell (1000000 + t div hours * 10000 + t mod hours div minutes * 100 + t mod minutes) as string
set newTimeCode to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7 & (text -3 thru -1 of timeCode)
end tell
return {newTimeCode, t} -- The text of the new time code and the number of seconds involved.
end addSecondsToTimeCode
I know it is a bit cobbled together but I works great. Thanks again Nigel and Dominik.
Here is a link to the the type of text documents the script processes if you want to try it out:
http://www.sieducation.com/downloads/cc_txt.zip
The only thing I could not figure out is how call “deleteHeader()” within “tell application “TextWrangler””. I tried removing the “tell application “TextWrangler”” from the handler since it was within one already when I tried to call it, but is still would not work. So I had to put it between two “tell application “TextWrangler””.
I had another challenge ahead of me. I had another set of captioned files that I needed to not only add four seconds to but combine them all into one caption file and shift the timecode for each one by the total amount of seconds of the previous captioned file (plus four). I had to do this because we took some shorter videos (with new titles added) and combined them all together into one video. Here is the script I came up with to do that:
global Doc_to_open
on open theDroppedItems
set countOne to 1
set listCount to 1
set numberToAdd to 4
repeat until listCount > (count theDroppedItems)
tell application "TextWrangler"
activate
set Doc_to_open to item countOne of theDroppedItems
open Doc_to_open
end tell
deleteHeader()
tell application "TextWrangler"
activate
set searchOptions to {search mode:grep, starting at top:true, case sensitive:true}
set noFind to false
repeat until (noFind)
set findResult to (find "\\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]\\]" searching in text 1 of text document 1 options searchOptions)
if (found of findResult) then
set theTime to found text of findResult -- Get the text directly from the returned record.
set theTimePlusFour to "[" & (my addToTimeString(numberToAdd, text 2 thru -2 of theTime)) & "]"
set timeCode to theTimePlusFour
set contents of found object of findResult to theTimePlusFour -- Replace the text directly in the document.
set starting at top of searchOptions to false -- Neutralise this option for subsequent searches.
else
set noFind to true
set numberToAdd to 4
cut selection
paste
set lastTimeCodePlusFour to "[" & (my addToTimeString(numberToAdd, text 2 thru -2 of timeCode)) & "]"
set numberToAdd to (my timeCodeToSeconds(lastTimeCodePlusFour))
if countOne > 1 then
tell application "TextWrangler"
select text 1 of text window 1
copy selection
close text window 1 saving no
paste
end tell
end if
end if
end repeat
end tell
set countOne to countOne + 1
set listCount to listCount + 1
end repeat
end open
on deleteHeader()
tell application "TextWrangler"
activate
find "{QTtext}" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
find "[00:00:00.00]" searching in text 1 of text document 1 options {search mode:literal, starting at top:false, wrap around:true, backwards:false, case sensitive:false, match words:false, extend selection:true} with selecting match
delete selection
set the clipboard to "
Title"
paste
end tell
end deleteHeader
on timeCodeToSeconds(timeCode)
return ((text 1 thru 2 of (word 3 of timeCode)) + 60 * (word 2 of timeCode) + 3600 * (word 1 of timeCode))
end timeCodeToSeconds
on addToTimeString(nSeconds, timeString)
set {tid, text item delimiters} to {text item delimiters, ":"}
set timeElements to reverse of (text items of timeString)
set milliSeconds to text -2 thru -1 of (item 1 of timeElements)
set item 1 of timeElements to text 1 thru 2 of (item 1 of timeElements)
set lastItem to length of timeElements
set additionInProgress to true
set currentElementIndex to 1
set currentAdditionValue to nSeconds
repeat while additionInProgress
set (item currentElementIndex of timeElements) to (item currentElementIndex of timeElements) + currentAdditionValue
if (item currentElementIndex of timeElements) ≥ 60 then
if currentElementIndex = lastItem then return "Overflow Error"
set currentAdditionValue to 0
repeat while (item currentElementIndex of timeElements) ≥ 60
set (item currentElementIndex of timeElements) to (item currentElementIndex of timeElements) - 60
set currentAdditionValue to currentAdditionValue + 1
end repeat
else
set additionInProgress to false
end if
set (item currentElementIndex of timeElements) to my formatTwoDigitNumber(item currentElementIndex of timeElements)
set currentElementIndex to currentElementIndex + 1
end repeat
set retval to ((reverse of timeElements) as string) & "." & milliSeconds
set text item delimiters to tid
return retval
end addToTimeString
on formatTwoDigitNumber(intNumber)
if intNumber > 9 then return (intNumber as string)
return ("0" & intNumber)
end formatTwoDigitNumber
It too is a bit cobbled together but it works as well. Nigel I tried to add your handler “addSecondsToTimeCode(s, timeCode)” to it but I was to far into it and it was working and I did not want to break it.
In order to finish this script, I need to have a hadler that will convert the timecode from this format:
[00:00:00.15]
Let’s simplify this expression. Notice we are
adding two numbers with unlike signs.
[00:00:08.00]
So what do we do? We subtract their absolute values.
[00:00:11.00]
Let me go ahead and find the absolute value
of each number.
[00:00:14.00]
The absolute value of negative five is five.
And the absolute value of eight is eight.
[00:00:20.00]
to this format:
00:00:00.150,00:00:08.000
Let’s simplify this expression. Notice we are
adding two numbers with unlike signs.
00:00:08.001,00:00:11.000
So what do we do? We subtract their absolute values.
00:00:11.001,00:00:14.000
Let me go ahead and find the absolute value
of each number.
00:00:14.001,00:00:20.000
The absolute value of negative five is five.
And the absolute value of eight is eight.
This handler would run after all the dropped documents have been processed.
I hope I am not being to greedy. I never dreamed that I would get this far in a few days and I see the light at the end of the tunnel. I really cannot thank you all enough for all your help.