Thank you for all of your help. It’s starting to come together.
I have it working to check the attributes of a single file. I’m now trying to merge it back into the application script that can check files as a batch if needed.
It looks like the issue I’m running into is my MediaInfo handler/subroutine is not being called? The error I keep getting is that “PromoCodec” is not defined. (I’m guessing all of my variables are not populating?) Can I not use multiple Handlers in a script?
I’m also having trouble with these two particular lines:
set theNameSpaced to do shell script "basename " & quoted form of thisFile
and
set theName to do shell script ("echo " & theNameSpaced & " | tr -s ' '")
My understanding is the top line should give me the basefile name of the file the script is currently checking. I’m not exactly sure what the echo shell script is doing. This script was written by someone else and I’ve been asked to update it.
Here is the full script as it exists right now:
on open theseFiles
set theErrorCounter to 0
set theNotaNumber to 0
set theNotaNumberCounter to 0
set theNoS4M to 0
set theNoS4MCounter to 0
set counter to 0
set counterTime to 0
set theErrorFiles to 0
set theErrorList to ""
set theList to ""
set theTimeList to ""
set theNoS4MList to ""
set theTimeListNoCheck to ""
set theRightFormats to {"XDCAM HD422", "50.0 Mbps", "29.970 fps"} as list
repeat with thisFile in theseFiles
try
tell application "Finder"
set extension hidden of thisFile to false
end tell
set theName to name of (info for thisFile)
set thePath to the POSIX path of thisFile
set n to the number of characters in theName
set n to (n - 11)
set theRealName to (characters n through end of theName) as string
set theNewPath to quoted form of ("/Users/user/Desktop/Proxies" & theRealName)
--THE ABOVE LINE IS WHERE THE FILE WILL BE POSTED IF IT PASSES ALL TECH CHECKS
set theOldpath to quoted form of thePath
set theErrorCounter to 0
on error
set theErrorCounter to 1
end try
if theErrorCounter is equal to 0 then
try
--------------------------------
set theNameSpaced to do shell script "basename " & quoted form of thisFile
set tracks to the mediainfoFile()'s tracks
set info to trackInfo given type:"Video", key:"Commercial_name"
if the info = false then error "Incorrect Codec"
set PromoCodec to (trackInfo given type:"Video", key:"Commercial_name")
set PromoDuration to (trackInfo given type:"Audio", id:2, key:"Duration")
set theDuration to returnNumbersInString(PromoDuration)
set Duration to SAR(theDuration, ", ", ".")
set theNumber to the first word of theName
set theS4MNumber to the word -2 of theName
set theRealNameNumber to the first word of theRealName
set PromoFrameRate to (trackInfo given type:"Video", key:"Frame_rate")
set FrameRate to returnNumbersInString(PromoFrameRate)
set Frames to SAR(FrameRate, ".", "")
set theFPS to Frames
set theFPSround to round (theFPS / 100)
try
set theCalc to theNumber * theFPSround
on error
set theNotaNumber to 1
end try
if the number of characters of theS4MNumber is equal to 8 then
try
set theS4MCalc to (theS4MNumber + 2)
on error
set theNoS4M to 1
end try
else
set theNoS4M to 1
end if
if the number of characters of theRealNameNumber is not equal to 8 then
set theNoS4M to 1
end if
------------------------------
set theName to do shell script ("echo " & theNameSpaced & " | tr -s ' '")
set theErrorCounter to 0
on error
set theErrorCounter to 1
end try
end if
if theErrorCounter is equal to 0 then
tell application "Finder"
if theNoS4M is not equal to 0 then
set label index of thisFile to 4
set theNoS4MList to theNoS4MList & theName & "
"
set theNoS4MCounter to theNoS4MCounter + 1
set theNoS4M to 0
else
if PromoCodec is not in theRightFormats then
set label index of thisFile to 2
set counter to counter + 1
set theList to theList & theName & "
" & " • codec is " & PromoCodec & "
"
else
if PromoFrameRate is not in theRightFormats then
set label index of thisFile to 7
set theTimeListNoCheck to theTimeListNoCheck & theName & "
"
set theNotaNumberCounter to theNotaNumberCounter + 1
set theNotaNumber to 0
else
if theNotaNumber is equal to 0 then --and (theDuration is greater than (theCalc + 1) or theDuration is less than (theCalc - 1))
set label index of thisFile to 3
set counterTime to counterTime + 1
set theTimeList to theTimeList & theName & "
" & " • length is " & theDuration
else
set label index of thisFile to 6
do shell script "cp " & theOldpath & " " & theNewPath
end if
end if
end if
end if
end tell
set theErrorCounter to 0
end if
if theErrorCounter is not equal to 0 then
tell application "Finder"
set label index of thisFile to 5
set theErrorFiles to theErrorFiles + 1
set theErrorList to theErrorList & (name of (info for thisFile)) & "
"
end tell
end if
end repeat
tell application "Finder"
if theNoS4MList is not equal to "" then
display dialog "..." & theNoS4MCounter & " file(s) do not have a proper S4M number...
" & theNoS4MList & "
(Label color: blue)" buttons "Thanks"
end if
if theTimeListNoCheck is not equal to "" then
display dialog "..." & theNotaNumberCounter & " file(s) were not checked for duration...
" & theTimeListNoCheck & "
(Label color: gray)" buttons "Thanks"
end if
if counter is not equal to 0 then
display dialog "..." & counter & " file(s) were not the proper format...
" & theList & "
(Label color: red)" buttons "Thanks"
end if
if counterTime is not equal to 0 then
display dialog "..." & counterTime & " file(s) were not the proper time...
" & theTimeList & "
(Label color: yellow)" buttons "Thanks"
end if
if theErrorFiles is not equal to 0 then
display dialog "..." & theErrorFiles & " file(s) were not correctly processed...
" & theErrorList & "
(Label color: purple)" buttons "Thanks"
end if
if counter is equal to 0 and theErrorFiles is equal to 0 and counterTime is equal to 0 and theTimeListNoCheck is equal to "" and theNoS4MList is equal to "" then
display dialog "All OK!" & "
(Label color: green)" buttons "Thanks"
end if
end tell
end open
--THIS HANDLER READS THE XML DATA:
on trackInfo given type:type as text, id:|id| as text : -1, key:key as text
local key, |id|, type
global tracks
tell application "System Events"
if |id| = -1 then
set {trackdata} to the |tracks| where ¬
value of the XML attribute "type" = |type|
else
set {trackdata} to the |tracks| where ¬
value of the XML attribute "type" = |type| and ¬
value of the XML attribute "streamid" = |id|
end if
tell (a reference to XML element named key in the trackdata) to ¬
if it exists then
return its value
else
return false
end if
end tell
end trackInfo
-- THIS HANDLER GENERATES THE XML
on mediainfoFile() -- Only call this once per script run
set theFile to quoted form of POSIX path of (thisFile)
set xmltext to do shell script "usr/local/bin/mediainfo " & ¬
theFile & " --Output=XML"
tell application "System Events"
set xml to make new XML data with properties {text:xmltext}
set mediaInfo to XML element "Mediainfo" of xml
set |file| to XML element "File" of mediaInfo
return {|tracks|:a reference to XML elements of |file|}
end tell
end mediainfoFile
-- THESE HANDLERS CONVERTS THE DURATION INFO TO NUMBERS
on returnNumbersInString(inputstring)
set s to quoted form of inputstring
do shell script "sed s/[a-zA-Z\\']//g <<< " & s
set dx to the result
set numlist to {}
repeat with i from 1 to count of words in dx
set this_item to word i of dx
try
set this_item to this_item as number
set the end of numlist to this_item
end try
end repeat
return numlist
end returnNumbersInString
on SAR(main_text, search_text, replace_text)
set old_delims to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to search_text
considering case
set parts to every text item of main_text
end considering
set AppleScript's text item delimiters to replace_text
set newText to (parts as string)
end try
set AppleScript's text item delimiters to old_delims
return newText
end SAR