Hi JQ, I’ve edited my script in the original post accordingly. Hopefully, you should no longer get this error.
I edited my script so that it use set theFiles to choose file of type {“public.xml”} with multiple selections allowed
The result is something like :
set plandates to {{"datas for /Users/yvankoenig/Desktop/mediainfo copie.xml
"}, {Format:"MXF", Duration:"5s 5ms", Encoded_date:"2019-04-03 15:32:46.000", Format_settings:"Closed / Complete", Writing_application:"Adobe Systems Incorporated Adobe Media Encoder 1.0.0.0.1", Overall_bit_rate:"52.0 Mbps", Commercial_name:"XDCAM HD422", Complete_name:"/Users/jquimby/Desktop/test.mxf", Writing_library:"Adobe Systems Incorporated Adobe Media Encoder 1.0.0.0.1", Format_profile:"OP-1a", File_size:"31.0 MiB"}, {Color_space:"YUV", |standard|:"Component", Scan_order:"Top Field First", Scan_type:"Interlaced", Bits__Pixel_Frame_:"0.805", Color_primaries:"BT.709", Frame_rate:"29.970 fps", Chroma_subsampling:"4:2:2", Commercial_name:"XDCAM HD422", Format:"MPEG Video", Format_profile:"4:2:2@High", Format_Settings_Wrapping:"Frame", Height:"1 080 pixels", Duration:"5s 5ms", Bit_depth:"8 bits", Format_settings__GOP:"M=3, N=15", Format_version:"Version 2", Transfer_characteristics:"BT.709", |id|:"512", Stream_size:"29.8 MiB (96%)", Format_settings__BVOP:"Yes", Width:"1 920 pixels", Bit_rate:"50.0 Mbps", Format_settings__Matrix:"Custom", Delay_SDTI:"0", Compression_mode:"Lossy"}, {Bit_rate:"1 152 Kbps", Duration:"5s 5ms", Stream_size:"704 KiB (2%)", Delay_SDTI:"0", Format:"PCM", Format_settings__Endianness:"Little", Bit_depth:"24 bits", Channel_s_:"1 channel", Bit_rate_mode:"Constant", |id|:"768", Format_Settings_Wrapping:"Frame (AES)"}, {Bit_rate:"1 152 Kbps", Duration:"5s 5ms", Stream_size:"704 KiB (2%)", Delay_SDTI:"0", Format:"PCM", Format_settings__Endianness:"Little", Bit_depth:"24 bits", Channel_s_:"1 channel", Bit_rate_mode:"Constant", |id|:"1024", Format_Settings_Wrapping:"Frame (AES)"}, {Format:"EIA-608", Duration:"5s 5ms", Delay_SDTI:"0", Stream_size:"0.00 Byte (0%)", Bit_rate_mode:"Constant", |id|:"1280-CC1", Muxing_mode:"Ancillary data / CDP"}, {"
"}, {"datas for /Users/yvankoenig/Desktop/mediainfo.xml
"}, {Format:"MXF", Duration:"5s 5ms", Encoded_date:"2019-04-03 15:32:46.000", Format_settings:"Closed / Complete", Writing_application:"Adobe Systems Incorporated Adobe Media Encoder 1.0.0.0.1", Overall_bit_rate:"52.0 Mbps", Commercial_name:"XDCAM HD422", Complete_name:"/Users/jquimby/Desktop/test.mxf", Writing_library:"Adobe Systems Incorporated Adobe Media Encoder 1.0.0.0.1", Format_profile:"OP-1a", File_size:"31.0 MiB"}, {Color_space:"YUV", |standard|:"Component", Scan_order:"Top Field First", Scan_type:"Interlaced", Bits__Pixel_Frame_:"0.805", Color_primaries:"BT.709", Frame_rate:"29.970 fps", Chroma_subsampling:"4:2:2", Commercial_name:"XDCAM HD422", Format:"MPEG Video", Format_profile:"4:2:2@High", Format_Settings_Wrapping:"Frame", Height:"1 080 pixels", Duration:"5s 5ms", Bit_depth:"8 bits", Format_settings__GOP:"M=3, N=15", Format_version:"Version 2", Transfer_characteristics:"BT.709", |id|:"512", Stream_size:"29.8 MiB (96%)", Format_settings__BVOP:"Yes", Width:"1 920 pixels", Bit_rate:"50.0 Mbps", Format_settings__Matrix:"Custom", Delay_SDTI:"0", Compression_mode:"Lossy"}, {Bit_rate:"1 152 Kbps", Duration:"5s 5ms", Stream_size:"704 KiB (2%)", Delay_SDTI:"0", Format:"PCM", Format_settings__Endianness:"Little", Bit_depth:"24 bits", Channel_s_:"1 channel", Bit_rate_mode:"Constant", |id|:"768", Format_Settings_Wrapping:"Frame (AES)"}, {Bit_rate:"1 152 Kbps", Duration:"5s 5ms", Stream_size:"704 KiB (2%)", Delay_SDTI:"0", Format:"PCM", Format_settings__Endianness:"Little", Bit_depth:"24 bits", Channel_s_:"1 channel", Bit_rate_mode:"Constant", |id|:"1024", Format_Settings_Wrapping:"Frame (AES)"}, {Format:"EIA-608", Duration:"5s 5ms", Delay_SDTI:"0", Stream_size:"0.00 Byte (0%)", Bit_rate_mode:"Constant", |id|:"1280-CC1", Muxing_mode:"Ancillary data / CDP"}, {"
"}}
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 30 avril 2019 20:00:55
What I am trying to do is update the following script that runs a bunch of tech spec checks on .mxf files to ensure they meet a certain criteria. The script I currently have uses Quicktime Player 7 to read the info and is here:
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 {"MPEG IMX 525/60 (30 Mb/s)", "XDCAM HD422 1080i60 (50 Mb/s VBR)"} 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 ("/Volumes/iMac_HD/Promo/" & theRealName)
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
tell application "QuickTime Player 7"
launch
repeat
if document 1 exists then
close document 1
else
exit repeat
end if
end repeat
open thisFile
set visible of window of document 1 to false
set theNameSpaced to name of document 1
set theFormat to the data format of track "Video Track" of document 1
set theDuration to the (number of frames of the track "Video Track" of document 1)
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 theFPS to the time scale of document 1
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
close document 1
if the number of characters of theRealNameNumber is not equal to 8 then
set theNoS4M to 1
end if
end tell
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 theFormat is not in theRightFormats then
set label index of thisFile to 2
set counter to counter + 1
set theList to theList & theName & "
" & " • codec is " & theFormat & "
"
else
if theNotaNumber is not equal to 0 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 and (theDuration is greater than (theCalc + 1) or theDuration is less than (theCalc - 1)) then
set label index of thisFile to 3
set counterTime to counterTime + 1
set theTimeList to theTimeList & theName & "
" & " • length is " & (round (theDuration / 30)) & " seconds
"
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 "QuickTime Player 7"
quit
end tell
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
I’m trying to replace Quicktime Player 7 with a new program because my company is phasing out Quicktime 7. MediaInfo was the software that was recommended we update our script to. I’m just not experienced enough to get it to work with MediaInfo/XML metadata.
Hi CK & Yvan,
Sorry, I posted my reply without refreshing my page to see your recent replies.
I have the script working to read the information from the XML files now, thank you.
All I need to do now is combine the MediaInfo XML reader script with my if/else steps from the original script that uses Quicktime 7.
Thanks.
I’ve started adding in my error messages, but I’ve run into a snag. MediaInfo sometimes uses different identifiers for Codec. If I use a .MXF wrapper with AVC codec, in the Video Track Type it calls it “Format_Info”. If I use a .MXF wrapper that has the XDCAM HD 422 codec that I want it calls the codec “Commercial_name”.
I’m trying to add an If statement to say that if the XML does not contain “Commercial_name” that it should return an error saying “Incorrect Codec” and stop the script. But I’m having trouble figuring out how to do that.
The error I keep getting is that “xmltext” is not defined. I thought that it was because maybe the If statement wasn’t in the right spot. But when I move it down into the “mediainfo()” step and the XML does not contain “Commercial_name” I’ll get an error saying it could not define the “Commercial_name” value.
Here is the script so far:
set theErrorCounter to 0
set CodecError to 0
set BitRateError to 0
set FrameRateError to 0
set tracks to the mediainfoFile()'s tracks
--Is this If statement not in the right spot?
if xmltext does not contain "Commercial_name" then
display dialog "INCORRECT CODEC!" buttons "Thanks!"
return
end if
--
set theRightFormats to {"XDCAM HD422", "50.0 Mbps", "29.970 fps"} as list
--THIS SECTION STORES THE XML DATA INTO VARIABLES
set PromoCodec to (trackinfo given type:"Video", key:"Commercial_name")
set PromoBitRate to (trackinfo given type:"Video", key:"Bit_rate")
set PromoDuration to (trackinfo given type:"Audio", id:2, key:"Duration")
set PromoWidth to (trackinfo given type:"Video", key:"Width")
set PromoHeight to (trackinfo given type:"Video", key:"Height")
set PromoFrameRate to (trackinfo given type:"Video", key:"Frame_rate")
--END
--THIS SECTION PULLS 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
return the value of the XML element named key in the trackdata
end tell
end trackinfo
-- THIS SECTION GENERATES THE XML INFO
on mediainfoFile() -- Only call this once per script run
set theFile to quoted form of POSIX path of (choose file)
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
--END OF DATA
--ERROR MESSAGES START HERE
tell application "Finder"
if PromoCodec is not in theRightFormats then
set CodecError to 1
display dialog "INCORRECT CODEC! • codec is " & PromoCodec & " " buttons "Thanks"
end if
if PromoBitRate is not in theRightFormats then
set BitRateError to 1
display dialog "INCORRECT BIT RATE! • bit rate is " & PromoBitRate & " " buttons "Thanks"
end if
if PromoFrameRate is not in theRightFormats then
set FrameRateError to 1
display dialog "INCORRECT FRAME RATE! • frame rate is " & PromoFrameRate & " " buttons "Thanks"
end if
end tell
if CodecError is equal to 0 and BitRateError is equal to 0 and FrameRateError is equal to 0 then
display dialog "All OK!" & "(Label color: green)" buttons "Thanks"
end if
You may want to have a look at this article. (In Japanese but use Google Translate on it)
http://piyocast.com/as/archives/816
It uses the framework XmlToDictKit
- original code on GIT : https://github.com/nicklockwood/XMLDictionary
- framework reworked by PyioCast : http://piyocast.com/as/wp-content/uploads/2018/02/XmlToDictKit.framework.zip
I’ve played around with as many XML libraries, methods in AppleScript and found
this the handiest. It can convert a xmlDoc, or XML String to a Dictionary (Record).
Which I find is much easier to the find the elements and attributes that I’m looking for
using NSArray’s valueForKeyPath methods
Or use Apple’s theXMLDoc’s nodesForXPath:… to get an array of xmlElements
then convert that to a XMLDoc or XMLString and then convert to a Dictionary.
I find it involves less code and is way easier to manage.
It’s simple enough to include the framework in your AppleScript.
In the handler trackInfo, replace this line:
return the value of the XML element named key in the trackdata
with this:
tell (a reference to XML element named key in the trackdata) to ¬
if it exists then
return its value
else
return false
end if
Then, when you do a call like this:
trackInfo given type:"Video", key:"blahblah"
(where the XML key “blah blah” clearly doesn’t exist), it will return false. From here, you can decide how you want to handle a false return value, e.g.
set info to trackInfo given type:"Video", key:"blahblah"
if the info = false then error "Incorrect Codec"
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
The biggest roadblock I’m hitting is how do I select a file to be run through one of my handlers, but also run through the rest of the script as well?
That’s a big script to try and go through, and one I have no means of really testing myself. But I do have two observations having had a skim through it:
① The variable thisFile is not defined in the scope of this handler. It’s a variable that gets instantiated for the repeat loop in your (implicit) run handler that loops through the batch of files; however, handlers (and other objects that contain code, such as script objects) have their own domain for storing and referencing variables that are fenced off from one another. So the variables in the main part of the script are only accessible from the main part of the script; and variables in the mediainfoFile() handler are only accessible from within the mediainfoFile() handler; likewise for the returnNumbersInString handler; and so on…
You may notice, however, that one of the handlers does access a variable outside of its domain:
--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
.
.
.
It does this by declaring the variable tracks with global scope, meaning that the trackInfo handler gains access to that variable, even though it was originally declared outside the handler, in the main part of the script. I won’t go into this in much more detail other than to make you aware of this. Generally speaking, I wouldn’t suggest declaring variables as global all willy-nilly, because it makes it very difficult to keep track of them in a script, and very easy to alter their values in another part of the script without meaning to (the reason it suffices to do this for tracks is because I specifically created that variable to have one, and only one purpose, and a singular instance of being seen in the main part of the script—the moment it is first declared. It couldn’t be declared inside the trackinfo handler otherwise mediainfoFile() would be called every time you go to retrieve a piece of information from the same file).
What I recommend doing is declaring a parameter (or argument) in the mediainfoFile() handler, which will allow you to pass the value of the variable thisFile from the main part of the script to the handler at the point where you call it. So this line:
on mediainfoFile()
will become:
on mediainfoFile(thisFile)
Note: Don’t be confused by the fact I’ve used the same name for the argument as the variable in your repeat loop; you can give this argument any name you wish, as long as make sure to change it in the line that comes immediately after, which currently uses thisFile. Although the variable in the repeat loop and the argument plus variable used in the handler have the same name, they are distinct entities, and have no relation to one another.
Then call the handler passing thisFile as an argument like so:
set tracks to the mediainfoFile(theFile)'s tracks
② The second observation I’m going to make is a tendency I see a lot in novice scripters, which is to use error-catching try blocks a) unnecessarily; b) in large quantities; and c) that encompass many, many lines of code within a single block. The consequence of all of this is that you have no idea where your script is encountering problems; what the nature of those problems are; what is causing those problems; and, therefore, have no means to know how to debug/fix the script.
Don’t get me wrong: effective, thoughtful error-catching is a good, sensible facet of scripting and programming, but it should always have a specific purpose, and be used to catch an error that you can predict will happen, under a specific set of circumstances you know will arise in your script, and that you know and understand why the error gets thrown. If the try block is there purely to ensure execution of the script isn’t interrupted by some inconvenient error that comes out of nowhere when the try block is removed, then all it is doing is masking a sick, broken script. Chances are, after one line throws an error that gets caught and swept under the rug, the lines of code that follow are at risk of doing the same.
I would suggest deleting every single try block in your script; running it; and painstakingly going through each error that arises, finding out the nature of the error in Script Debugger’s event log, or at the time of alert, and fixing them one by one. If you can’t fix them, the script is damaged and unreliable; if you can fix them, you won’t need the try block. If there are instances where you do need to use one for a purpose, try and have it cover only one or two lines of code, specifically targetting where the error arises, and having an error handling block that does something useful to cater for the dysfunction so that it doesn’t have a ripple effect when the script exits the block.
Thank you for your help. I’m now able to get the variable to pass through in my script that only processes one at a time. For some reason in the larger script it still won’t define my PromoCodec variable. I think you are right about all the error checks in the original script. I understand why they are there (I was originally and end user of this script/application), but you are right in that it is making it impossible for me to debug.
What I’m going to try to do is rebuild the checks one piece at a time so that I can properly debug each step. I really appreciate your help.
Here is the working script that reads an individual file and returns it’s specs using MediaInfo CLI:
set TEST to (choose file)
set tracks to the mediainfoFile(TEST)'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 PromoBitRate to (trackInfo given type:"Video", key:"Bit_rate")
set PromoDuration to (trackInfo given type:"Audio", id:2, key:"Duration")
set PromoWidth to (trackInfo given type:"Video", key:"Width")
set PromoHeight to (trackInfo given type:"Video", key:"Height")
set PromoFrameRate to (trackInfo given type:"Video", key:"Frame_rate")
set FrameRate to returnNumbersInString(PromoFrameRate)
set Frames to SAR(FrameRate, ".", "")
set theDuration to returnNumbersInString(PromoDuration)
set Duration to SAR(theDuration, ", ", ".")
set PromoSpecs to {PromoCodec, PromoBitRate, FrameRate, Duration}
--HANDLERS:
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
on mediainfoFile(inputstring) -- Only call this once per script run
set theFile to quoted form of POSIX path of (inputstring)
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
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
return PromoSpecs
I ran your script using the original XML data you provided, and it seemed to work okay.
The result returned was:
{"XDCAM HD422", "50.0 Mbps", {29.97}, "5.5"}
indicating the variable PromoCodec contained the value “XDCAM HD422”. Also, I note that variables trackInfo and PromoCodec both get assigned the same value as you make the same handler call twice (not a problem, just an observation I felt was odd).
So, currently, I cannot explain why your PromoCodec variable isn’t getting assigned a value. But you neglect to say what happens, i.e. does the script throw an error when it’s asked to set PromoCodec to …; and, if not, what result does the script return (specifically, what is the first item of PromoSpecs) ?
The MediaInfo/XML reader script I posted works to pull all the data properly.
It’s only when I try to shoehorn the working script into the larger application script with all the try blocks that I get the error. The error message is “The Variable PromoCodec is not defined”.
I’m now taking your suggestion and adding the checks and balances to the MediaInfo/XML reader script one at a time so that if/when I run into an error I can actually see where it is.
One question though. Once I have it working the way I want I’ll need to modify it so that I can process files as a batch.
Currently I choose the file to process like this:
set TEST to (choose file)
set tracks to the mediainfoFile(TEST)'s tracks
The mediainfoFile handler is set to:
on mediainfoFile(inputstring) -- Only call this once per script run
set theFile to quoted form of POSIX path of (inputstring)
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
This works perfectly with one file. If I wanted to process a batch of files, would this be the way to do it?:
on open theseFiles
repeat with thisFile in theseFiles
set TEST to thisFile
set tracks to the mediainfoFile(TEST)'s tracks
--mediainfoFile handler/subroutine
end repeat
Very almost…
on open theseFiles
repeat with thisFile in theseFiles
set TEST to thisFile
set tracks to the mediainfoFile(TEST)'s tracks
.
.
.
end repeat
end open
on mediainfoFile(inputstring)
.
.
.
end mediainfoFile
(* Other handlers *)
The mediainfoFilehandler (and all the other handlers in your script) belong outside the repeat loop, and outside the on open handler. But, otherwise, yes, the principle is correct.
I’ve re-written the entire script step by step. But I seem to still have an issue with this line:
repeat with thisFile in theseFiles -- STARTS CHECKING FILES IN SEQUENCE
set thePromo to thisFile
set tracks to the mediainfoFile(thePromo)'s tracks -- THIS LINE
These are the handlers I’m using that you gave me:
--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 DOCUMENT
on mediainfoFile(inputstring) -- Only call this once per script run
set theFile to quoted form of POSIX path of (inputstring)
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
For some reason when I try to use it as an application to check a batch of files, it doesn’t seem to activate the mediainfoFile handler and generate the XML. It works perfectly fine in my script that only does one file at a time. I thought I was following your instructions from a few posts ago properly, but I’m not sure what part I’m missing.
I really appreciate all your help, and I’m very close to finishing this and wouldn’t be this far without you.
Thanks.
I need more information to go on. Forget trying to get it to work as an application for now; focus on trying to get it to work as a script that contains a repeat loop. What goes wrong ? Where does it go wrong ? What’s the error message ? What is happening that shouldn’t happen ? What isn’t happening that should ?
Population one open handler with multiply selection:
set theseFiles to (choose file of type {"public.movie"} with multiple selections allowed)
on open theseFiles
repeat with thisFile in theseFiles
set TEST to thisFile
set tracks to the mediainfoFile(TEST)'s tracks
--mediainfoFile handler/subroutine
end repeat
This is a stripped down version of the script that I’m working with to try and figure out the problem:
set theseFiles to (choose file with multiple selections allowed)
repeat with thisFile in theseFiles
set tracks to the mediainfoFile(thisFile)'s tracks
set PromoCodec to (trackInfo given type:"Video", key:"Commercial_name")
set thename to name of (info for the thisFile) & " " & PromoCodec & " "
return thename
end repeat
--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 DOCUMENT
on mediainfoFile(inputstring) -- Only call this once per script run
set theFile to quoted form of POSIX path of (inputstring)
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
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
So far this only seems to process the first file in the list, no matter how many I choose. I don’t think I’m using the repeat block properly.
Ok, I’ve got the repeat step working with this script:
set theseFiles to (choose file with multiple selections allowed)
set thelist to ""
repeat with x from 1 to count of theseFiles
set thisFile to item x of theseFiles
set tracks to the mediainfoFile(thisFile)'s tracks
set PromoCodec to (trackInfo given type:"Video", key:"Commercial_name")
set thename to name of (info for the thisFile) & " " & PromoCodec & " "
set thelist to thelist & " " & thename & " "
end repeat
return thelist
--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 DOCUMENT
on mediainfoFile(inputstring) -- Only call this once per script run
set theFile to quoted form of POSIX path of (inputstring)
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
But for some reason once I add the “on open” command to try and make it into an application that’s where my variables stop populating properly. Here’s the same script as above, but with the added “on open” step to try and make it an application:
set theseFiles to (choose file with multiple selections allowed)
set thelist to ""
on open theseFiles
repeat with x from 1 to count of theseFiles
set thisFile to item x of theseFiles
set tracks to the mediainfoFile(thisFile)'s tracks
set PromoCodec to (trackInfo given type:"Video", key:"Commercial_name")
set thename to name of (info for the thisFile) & " " & PromoCodec & " "
set thelist to thelist & " " & thename & " "
end repeat
end open
display dialog "..." & thelist
--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 DOCUMENT
on mediainfoFile(inputstring) -- Only call this once per script run
set theFile to quoted form of POSIX path of (inputstring)
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
Is the code different when trying to make an application? I’m obviously missing something.
What you get is logical.
When your script execute return theName it exits the loop.
Try with:
set theseFiles to (choose file with multiple selections allowed)
set theNames to {}
repeat with thisFile in theseFiles
set tracks to the mediainfoFile(thisFile)'s tracks
set PromoCodec to (trackInfo given type:"Video", key:"Commercial_name")
set thename to name of (info for the thisFile) & " " & PromoCodec & " "
set end of theNames to thename
end repeat
return theNames
# Here are the handlers
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 13 mai 2019 17:41:53