Friday, July 19, 2019

#1 2019-04-26 12:14:10 pm

JQ
Member
Registered: 2019-02-04
Posts: 36

Can Applescript read an XML file stored as a variable?

Hello,

I'm using MediaInfo CLI to generate an XML file of metadata that I want to then sort through and pick out specific pieces of metadata.  I'm able to generate the XML file easily enough with a shell script:

Applescript:


set theFile to quoted form of POSIX path of (choose file)
set XMLtext to do shell script "usr/local/bin/mediainfo " & theFile & " --Output=XML"

I've been able to find lots of scripts that read information from XML files that exist as a stored file. 

I found this one:

Applescript:


on run -- example
   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" to set XMLdata to make new XML data with properties {text:XMLtext}
   set codec to getXMLElement from {XMLdata, "File", "track type=\"Video\"", "Commercial_Name"}
   codec
end run
to getXMLElement from XMLItems
   (*
get the specified element from a simple XML structure by name or index
the number of items is not fixed, but must be at least 2 (the XML data and an XML element)
parameters: XMLItems [list] -
item 1 [record]: the XML data
item 2 [varies]: the XML element name or index (names are case sensitive)
item(s) 3+ [varies]: sub item(s)
returns [varies]: contents of the element
*)

   try
       if (count XMLItems) is less than 2 then error "getXMLElement handler: item list contains too few items"
       tell application "System Events"
           set myXMLdata to (the first item of XMLItems)
           if class of myXMLdata is not XML data then error "getXMLElement handler: not XML data"
           set myXMLelement to XML element 1 of myXMLdata -- start at the root element
           
           --repeat with anItem in rest of XMLItems -- add on the sub items
           --    set anItem to contents of anItem
           --    try
           --        set anItem to anItem as integer -- index number?
           --    end try
           --    set myXMLelement to (get XML element anItem of myXMLelement)
           --end repeat
           
           try -- return the element(s)
               set theResult to value of myXMLelement
               if theResult is missing value then error
               return theResult
           on error errorMessage number errorNumber -- the element is empty or a list of items
               log errorMessage
               return value of XML elements of myXMLelement
           end try
       end tell
   on error errorMessage number errorNumber
       log errorMessage
       return missing value
       -- error "getXMLElement handler: element not found (" & errorNumber & ")"
       
   end try
end getXMLElement

But it just keeps returning the error message "missing value".

For reference this is the XML that was generated from my test file via mediainfo:

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Mediainfo version=\"0.7.60\">
<File>
<track type=\"General\">
<Complete_name>/Users/jquimby/Desktop/test.mxf</Complete_name>
<Format>MXF</Format>
<Commercial_name>XDCAM HD422</Commercial_name>
<Format_profile>OP-1a</Format_profile>
<Format_settings>Closed / Complete</Format_settings>
<File_size>31.0 MiB</File_size>
<Duration>5s 5ms</Duration>
<Overall_bit_rate>52.0 Mbps</Overall_bit_rate>
<Encoded_date>2019-04-03 15:32:46.000</Encoded_date>
<Writing_application>Adobe Systems Incorporated Adobe Media Encoder 1.0.0.0.1</Writing_application>
<Writing_library>Adobe Systems Incorporated Adobe Media Encoder 1.0.0.0.1</Writing_library>
</track>

<track type=\"Video\">
<ID>512</ID>
<Format>MPEG Video</Format>
<Commercial_name>XDCAM HD422</Commercial_name>
<Format_version>Version 2</Format_version>
<Format_profile>4:2:2@High</Format_profile>
<Format_settings__BVOP>Yes</Format_settings__BVOP>
<Format_settings__Matrix>Custom</Format_settings__Matrix>
<Format_settings__GOP>M=3, N=15</Format_settings__GOP>
<Format_Settings_Wrapping>Frame</Format_Settings_Wrapping>
<Duration>5s 5ms</Duration>
<Bit_rate>50.0 Mbps</Bit_rate>
<Width>1 920 pixels</Width>
<Height>1 080 pixels</Height>
<Display_aspect_ratio>16:9</Display_aspect_ratio>
<Frame_rate>29.970 fps</Frame_rate>
<Standard>Component</Standard>
<Color_space>YUV</Color_space>
<Chroma_subsampling>4:2:2</Chroma_subsampling>
<Bit_depth>8 bits</Bit_depth>
<Scan_type>Interlaced</Scan_type>
<Scan_order>Top Field First</Scan_order>
<Compression_mode>Lossy</Compression_mode>
<Bits__Pixel_Frame_>0.805</Bits__Pixel_Frame_>
<Stream_size>29.8 MiB (96%)</Stream_size>
<Delay_SDTI>0</Delay_SDTI>
<Color_primaries>BT.709</Color_primaries>
<Transfer_characteristics>BT.709</Transfer_characteristics>
</track>

<track type=\"Audio\" streamid=\"1\">
<ID>768</ID>
<Format>PCM</Format>
<Format_settings__Endianness>Little</Format_settings__Endianness>
<Format_Settings_Wrapping>Frame (AES)</Format_Settings_Wrapping>
<Duration>5s 5ms</Duration>
<Bit_rate_mode>Constant</Bit_rate_mode>
<Bit_rate>1 152 Kbps</Bit_rate>
<Channel_s_>1 channel</Channel_s_>
<Sampling_rate>48.0 KHz</Sampling_rate>
<Bit_depth>24 bits</Bit_depth>
<Stream_size>704 KiB (2%)</Stream_size>
<Delay_SDTI>0</Delay_SDTI>
</track>

<track type=\"Audio\" streamid=\"2\">
<ID>1024</ID>
<Format>PCM</Format>
<Format_settings__Endianness>Little</Format_settings__Endianness>
<Format_Settings_Wrapping>Frame (AES)</Format_Settings_Wrapping>
<Duration>5s 5ms</Duration>
<Bit_rate_mode>Constant</Bit_rate_mode>
<Bit_rate>1 152 Kbps</Bit_rate>
<Channel_s_>1 channel</Channel_s_>
<Sampling_rate>48.0 KHz</Sampling_rate>
<Bit_depth>24 bits</Bit_depth>
<Stream_size>704 KiB (2%)</Stream_size>
<Delay_SDTI>0</Delay_SDTI>
</track>

<track type=\"Text\">
<ID>1280-CC1</ID>
<Format>EIA-608</Format>
<Muxing_mode>Ancillary data / CDP</Muxing_mode>
<Duration>5s 5ms</Duration>
<Bit_rate_mode>Constant</Bit_rate_mode>
<Stream_size>0.00 Byte (0%)</Stream_size>
<Delay_SDTI>0</Delay_SDTI>
</track>

</File>
</Mediainfo>
"
I can't seem to figure out what part of the script I need to modify to read the XML generated by MediaInfo.

Offline

 

#2 2019-04-27 07:32:16 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 91

Re: Can Applescript read an XML file stored as a variable?

I think part (or all) of the issue may be that the handler you found only processes XML elements ("Mediainfo", "File", "track"); you need something that processes XML element attributes ("type", "streamid") as well.

Applescript:

set tracks to the mediainfoFile()'s tracks

log (trackInfo given type:"Video", key:"Commercial_name") --> "XDCAM HD422"
log (trackInfo given type:"Video", key:"Bit_rate") --> "50.0 Mbps"
log (trackInfo given type:"Audio", id:2, key:"Duration") --> "5s 5ms"


--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
               
               return the value of the XML element named key in the trackdata
       end tell
end trackInfo

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

Last edited by CK (2019-04-30 10:12:49 am)

Offline

 

#3 2019-04-27 03:07:00 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3471

Re: Can Applescript read an XML file stored as a variable?

Here are two versions which may help.


Version 1. It build a list of lists of sub-records

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXMLFile to POSIX path of ((path to desktop as text) & "mediainfo.xml")
set theURL to current application's |NSURL|'s fileURLWithPath:theXMLFile
set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)
if theXMLDoc = missing value then error (theError's localizedDescription() as text)
set {planEvents, theError} to theXMLDoc's nodesForXPath:"//Mediainfo/File/track" |error|:(reference)
if planEvents = missing value then error (theError's localizedDescription() as text)
set planDates to {}

repeat with aNode in planEvents
   set aRecord to {}
   
   try
       set aValue to (aNode's elementsForName:"Complete_name")'s firstObject()'s stringValue() as text
       set end of aRecord to {Complete_name:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Format")'s firstObject()'s stringValue() as text
       set end of aRecord to {Format:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Commercial_name")'s firstObject()'s stringValue() as text
       set end of aRecord to {Commercial_name:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Format_profile")'s firstObject()'s stringValue() as text
       set end of aRecord to {Format_profile:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Format_settings")'s firstObject()'s stringValue() as text
       set end of aRecord to {Format_settings:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"File_size")'s firstObject()'s stringValue() as text
       set end of aRecord to {File_size:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Duration")'s firstObject()'s stringValue() as text
       set end of aRecord to {Duration:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Overall_bit_rate")'s firstObject()'s stringValue() as text
       set end of aRecord to {Overall_bit_rate:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Encoded_date")'s firstObject()'s stringValue() as text
       set end of aRecord to {Encoded_date:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Writing_application")'s firstObject()'s stringValue() as text
       set end of aRecord to {Writing_application:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Writing_library")'s firstObject()'s stringValue() as text
       set end of aRecord to {Writing_library:aValue}
   end try
   
   #======
   
   try
       set aValue to (aNode's elementsForName:"ID")'s firstObject()'s stringValue() as text
       set end of aRecord to {|ID|:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Format_version")'s firstObject()'s stringValue() as text
       set end of aRecord to {Format_version:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Format_settings__BVOP")'s firstObject()'s stringValue() as text
       set end of aRecord to {Format_settings__BVOP:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Format_settings__Matrix")'s firstObject()'s stringValue() as text
       set end of aRecord to {Format_settings__Matrix:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Format_settings__GOP")'s firstObject()'s stringValue() as text
       set end of aRecord to {Format_settings__GOP:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Format_Settings_Wrapping")'s firstObject()'s stringValue() as text
       set end of aRecord to {Format_Settings_Wrapping:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Bit_rate")'s firstObject()'s stringValue() as text
       set end of aRecord to {Bit_rate:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Width")'s firstObject()'s stringValue() as text
       set end of aRecord to {Width:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Height")'s firstObject()'s stringValue() as text
       set end of aRecord to {Height:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Frame_rate")'s firstObject()'s stringValue() as text
       set end of aRecord to {Frame_rate:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Standard")'s firstObject()'s stringValue() as text
       set end of aRecord to {|Standard|:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Color_space")'s firstObject()'s stringValue() as text
       set end of aRecord to {Color_space:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Chroma_subsampling")'s firstObject()'s stringValue() as text
       set end of aRecord to {Chroma_subsampling:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Bit_depth")'s firstObject()'s stringValue() as text
       set end of aRecord to {Bit_depth:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Scan_type")'s firstObject()'s stringValue() as text
       set end of aRecord to {Scan_type:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Scan_order")'s firstObject()'s stringValue() as text
       set end of aRecord to {Scan_order:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Compression_mode")'s firstObject()'s stringValue() as text
       set end of aRecord to {Compression_mode:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Bits__Pixel_Frame_")'s firstObject()'s stringValue() as text
       set end of aRecord to {Bits__Pixel_Frame_:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Stream_size")'s firstObject()'s stringValue() as text
       set end of aRecord to {Stream_size:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Color_primaries")'s firstObject()'s stringValue() as text
       set end of aRecord to {Color_primaries:aValue}
   end try
   try
       set aValue to (aNode's elementsForName:"Transfer_characteristics")'s firstObject()'s stringValue() as text
       set end of aRecord to {Transfer_characteristics:aValue}
   end try
   set end of planDates to aRecord
end repeat
planDates

Version 2 :  It build a list of records

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXMLFile to POSIX path of ((path to desktop as text) & "mediainfo.xml")
set theURL to current application's |NSURL|'s fileURLWithPath:theXMLFile
set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)
if theXMLDoc = missing value then error (theError's localizedDescription() as text)
set {planEvents, theError} to theXMLDoc's nodesForXPath:"//Mediainfo/File/track" |error|:(reference)
if planEvents = missing value then error (theError's localizedDescription() as text)
set planDates to {}

repeat with aNode in planEvents
   set aRecord to {Complete_name:missing value, Format:missing value, Commercial_name:missing value, Format_profile:missing value, Format_settings:missing value, File_size:missing value, Duration:missing value, Overall_bit_rate:missing value, Encoded_date:missing value, Writing_application:missing value, Writing_library:missing value, |ID|:missing value, Format_version:missing value, Format_settings__BVOP:missing value, Format_settings__Matrix:missing value, Format_settings__GOP:missing value, Format_Settings_Wrapping:missing value, Bit_rate:missing value, Width:missing value, Height:missing value, Frame_rate:missing value, |Standard|:missing value, Color_space:missing value, Chroma_subsampling:missing value, Bit_depth:missing value, Scan_type:missing value, Scan_order:missing value, Compression_mode:missing value, Bits__Pixel_Frame_:missing value, Stream_size:missing value, Color_primaries:missing value, Transfer_characteristics:missing value}
   try
       set aValue to (aNode's elementsForName:"Complete_name")'s firstObject()'s stringValue() as text
       set Complete_name of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Format")'s firstObject()'s stringValue() as text
       set Format of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Commercial_name")'s firstObject()'s stringValue() as text
       set Commercial_name of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Format_profile")'s firstObject()'s stringValue() as text
       set Format_profile of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Format_settings")'s firstObject()'s stringValue() as text
       set Format_settings of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"File_size")'s firstObject()'s stringValue() as text
       set File_size of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Duration")'s firstObject()'s stringValue() as text
       set Duration of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Overall_bit_rate")'s firstObject()'s stringValue() as text
       set Overall_bit_rate of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Encoded_date")'s firstObject()'s stringValue() as text
       set Encoded_date of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Writing_application")'s firstObject()'s stringValue() as text
       set Writing_application of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Writing_library")'s firstObject()'s stringValue() as text
       set Writing_library of aRecord to aValue
   end try
   
   #======
   
   try
       set aValue to (aNode's elementsForName:"ID")'s firstObject()'s stringValue() as text
       set |ID| of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Format_version")'s firstObject()'s stringValue() as text
       set Format_version of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Format_settings__BVOP")'s firstObject()'s stringValue() as text
       set Format_settings__BVOP of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Format_settings__Matrix")'s firstObject()'s stringValue() as text
       set Format_settings__Matrix of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Format_settings__GOP")'s firstObject()'s stringValue() as text
       set Format_settings__GOP of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Format_Settings_Wrapping")'s firstObject()'s stringValue() as text
       set Format_Settings_Wrapping of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Bit_rate")'s firstObject()'s stringValue() as text
       set Bit_rate of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Width")'s firstObject()'s stringValue() as text
       set Width of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Height")'s firstObject()'s stringValue() as text
       set Height of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Frame_rate")'s firstObject()'s stringValue() as text
       set Frame_rate of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Standard")'s firstObject()'s stringValue() as text
       set |Standard| of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Color_space")'s firstObject()'s stringValue() as text
       set Color_space of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Chroma_subsampling")'s firstObject()'s stringValue() as text
       set Chroma_subsampling of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Bit_depth")'s firstObject()'s stringValue() as text
       set Bit_depth of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Scan_type")'s firstObject()'s stringValue() as text
       set Scan_type of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Scan_order")'s firstObject()'s stringValue() as text
       set Scan_order of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Compression_mode")'s firstObject()'s stringValue() as text
       set Compression_mode of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Bits__Pixel_Frame_")'s firstObject()'s stringValue() as text
       set Bits__Pixel_Frame_ of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Stream_size")'s firstObject()'s stringValue() as text
       set Stream_size of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Stream_size")'s firstObject()'s stringValue() as text
       set Stream_size of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Color_primaries")'s firstObject()'s stringValue() as text
       set Color_primaries of aRecord to aValue
   end try
   try
       set aValue to (aNode's elementsForName:"Transfer_characteristics")'s firstObject()'s stringValue() as text
       set Transfer_characteristics of aRecord to aValue
   end try
   
   set end of planDates to aRecord
end repeat
planDates

Both are built upon a sample posted by Shane Stanley in https://macscripter.net/viewtopic.php?pid=188768

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 27 avril 2019  23:05:56

Offline

 

#4 2019-04-28 06:11:33 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3471

Re: Can Applescript read an XML file stored as a variable?

Oops, I forgot several keys.
The script below create the records with the exact content of each one embedded in the xml file.

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

Germaine()

on Germaine()
   
   set theFiles to choose file of type {"public.xml"} with multiple selections allowed
   set planDates to {}
   
   repeat with aFile in theFiles
       set theXMLFile to POSIX path of aFile
       set end of planDates to {"datas for " & theXMLFile & linefeed}
       set theURL to (current application's |NSURL|'s fileURLWithPath:theXMLFile)
       set {theXMLDoc, theError} to (current application's NSXMLDocument's alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference))
       if theXMLDoc = missing value then error (theError's localizedDescription() as text)
       set {planEvents, theError} to (theXMLDoc's nodesForXPath:"//Mediainfo/File/track" |error|:(reference))
       if planEvents = missing value then error (theError's localizedDescription() as text)
       
       set allKeys to {"ID", "Transfer_characteristics", "Complete_name", "Writing_library", "Stream_size", "Format_settings__Matrix", "Bit_rate_mode", "Overall_bit_rate", "Format_version", "Compression_mode", "Format_Settings_Wrapping", "Color_primaries", "Chroma_subsampling", "Standard", "Commercial_name", "Bit_rate", "Format_settings__BVOP", "Color_space", "Channel_s_", "Height", "Duration", "Scan_type", "Delay_SDTI", "Format_settings__Endianness", "Format", "Muxing_mode", "Format_profile", "Width", "Bit_depth", "File_size", "Bits__Pixel_Frame_", "Format_settings__GOP", "Frame_rate", "Encoded_date", "Scan_order", "Format_settings", "Writing_application"}
       
       
       repeat with aNode in planEvents
           # Create an empty dictionary
           set theDict to current application's NSMutableDictionary's alloc()'s init()
           repeat with aKey in allKeys
               try
                   set aValue to (aNode's elementsForName:aKey)'s firstObject()'s stringValue() as text
                   (*
           # Attempt to keep the original spelling but don't give the wanted result
           if aKey is "ID" then
               set aKey to "|ID|" --> will become |id|
           else if aKey is "Standard" then
               set aKey to "|Standard|" --> will become |standard|
           end if
           *)

                   (theDict's setObject:aValue forKey:aKey)
               end try
           end repeat
           set end of planDates to (theDict as record)
       end repeat
       set end of planDates to {linefeed}
   end repeat
   return planDates
end Germaine

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 28 avril 2019  14:10:34

Edited to use Choose file with multiple selections allowed.

Last edited by Yvan Koenig (2019-04-30 11:59:32 am)

Offline

 

#5 2019-04-29 10:34:28 am

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

Thank you for the replies CK & Yvan.

When I try the scripts suggested I get an error "Can't get POSIX path of alias" which highlights the "quoted form" line where I am setting theFile variable:

set theFile to quoted form of POSIX path of (choose file)

The way I'm trying to get it to work is that the file I choose to run through the script is a .MXF file.  My shell script then uses MediaInfo to read the metadata as an XML and then pull specific information from that XML to see if it meets my criteria. 

It looks like I should be able to sort the variables properly with your suggestions, but I'm still having trouble getting it to read the XML information from the MXF/MediaInfo shell script.

I really appreciate the help.  Thank you.

Offline

 

#6 2019-04-29 02:25:45 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3471

Re: Can Applescript read an XML file stored as a variable?

I don't understand. Here the given instruction behaves flawlessly.

Applescript:

set theFile to quoted form of POSIX path of (choose file)

--> "'/Users/**********/Desktop/2019-04-12 16-43.pdf'"

I don't know which system you are using.

You may try:

Applescript:

set theFile to quoted form of (POSIX path of (choose file))

--> "'/Users/**********/Desktop/2019-04-12 16-43.pdf'"

Of course I know a way to get what you describe, but it requires a really odd instruction:

Applescript:

set theFile to quoted form of (POSIX path of (choose file with multiple selections allowed))

It's odd because this kind of choose file command return a list of aliases so POSIX path of is unable to do its job if more than one item are selected.
It's true that it wrongly highlight quoted form although it fails to apply POSIX path.

Applescript:

set theAliases to (choose file with multiple selections allowed)
--> {alias "SSD 500:Users:**********:Desktop:mediainfo copie 2.xml", alias "SSD 500:Users:**********:Desktop:mediainfo copie.xml", alias "SSD 500:Users:**********:Desktop:mediainfo.xml"}
repeat with anAlias in theAliases
   set theXMLFile to POSIX path of anAlias
   log theXMLFile
   -- or
   set theQuotedXMLFile to quoted form of POSIX path of anAlias
   log theQuotedXMLFile
   -- continue the code here
end repeat
(*/Users/**********/Desktop/mediainfo copie 2.xml*)
(*'/Users/**********/Desktop/mediainfo copie 2.xml'*)
(*/Users/**********/Desktop/mediainfo copie.xml*)
(*'/Users/**********/Desktop/mediainfo copie.xml'*)
(*/Users/**********/Desktop/mediainfo.xml*)
(*'/Users/**********/Desktop/mediainfo.xml'*)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 29 avril 2019  22:23:36

Last edited by Yvan Koenig (2019-04-29 02:27:32 pm)

Offline

 

#7 2019-04-30 10:10:34 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 91

Re: Can Applescript read an XML file stored as a variable?

JQ wrote:

Thank you for the replies CK & Yvan.

When I try the scripts suggested I get an error "Can't get POSIX path of alias" which highlights the "quoted form" line where I am setting theFile variable:

set theFile to quoted form of POSIX path of (choose file)

The way I'm trying to get it to work is that the file I choose to run through the script is a .MXF file.  My shell script then uses MediaInfo to read the metadata as an XML and then pull specific information from that XML to see if it meets my criteria. 

It looks like I should be able to sort the variables properly with your suggestions, but I'm still having trouble getting it to read the XML information from the MXF/MediaInfo shell script.

I really appreciate the help.  Thank you.



Hi JQ, I've edited my script in the original post accordingly.  Hopefully, you should no longer get this error.

Offline

 

#8 2019-04-30 12:01:12 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3471

Re: Can Applescript read an XML file stored as a variable?

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 :

Applescript:

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

Offline

 

#9 2019-04-30 12:21:15 pm

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

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:

Applescript:


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.

Offline

 

#10 2019-04-30 12:45:40 pm

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

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.

Offline

 

#11 2019-05-03 12:59:48 pm

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

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:

Applescript:


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

Offline

 

#12 2019-05-03 07:49:06 pm

technomorph
Member
Registered: 2017-12-14
Posts: 114

Re: Can Applescript read an XML file stored as a variable?

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/uploa … mework.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.


Filed under: xml, Record, xpath, XMLtoDictKit

Offline

 

#13 2019-05-06 03:14:48 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 91

Re: Can Applescript read an XML file stored as a variable?

JQ wrote:

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.

In the handler trackInfo, replace this line:

Applescript:

return the value of the XML element named key in the trackdata

with this:

Applescript:

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:

Applescript:

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.

Applescript:

set info to trackInfo given type:"Video", key:"blahblah"
if the info = false then error "Incorrect Codec"

Offline

 

#14 2019-05-08 11:46:36 am

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

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:

Applescript:


set theNameSpaced to do shell script "basename " & quoted form of thisFile

and

Applescript:


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:

Applescript:


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

Offline

 

#15 2019-05-08 02:17:04 pm

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

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?

Offline

 

#16 2019-05-08 02:44:29 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 91

Re: Can Applescript read an XML file stored as a variable?

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:

JQ wrote:

Applescript:

-- THIS HANDLER GENERATES THE XML
on mediainfoFile() -- Only call this once per script run
       set theFile to quoted form of POSIX path of (thisFile)
               .
               .
               .

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:

Applescript:

--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:

Applescript:

on mediainfoFile()

will become:

Applescript:

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:

Applescript:

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.

Offline

 

#17 2019-05-09 09:00:58 am

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

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:

Applescript:


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

Offline

 

#18 2019-05-09 10:20:04 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 91

Re: Can Applescript read an XML file stored as a variable?

JQ wrote:

For some reason in the larger script it still won't define my PromoCodec variable.

I ran your script using the original XML data you provided, and it seemed to work okay.
The result returned was:

Applescript:

{"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) ?

Offline

 

#19 2019-05-09 01:21:24 pm

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

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:

Applescript:


set TEST to (choose file)
set tracks to the mediainfoFile(TEST)'s tracks

The mediainfoFile handler is set to:

Applescript:


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?:

Applescript:


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

Offline

 

#20 2019-05-09 09:16:35 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 91

Re: Can Applescript read an XML file stored as a variable?

JQ wrote:

This works perfectly with one file.  If I wanted to process a batch of files, would this be the way to do it?:

Applescript:


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...

Applescript:

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.

Offline

 

#21 2019-05-10 02:33:43 pm

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

I've re-written the entire script step by step.  But I seem to still have an issue with this line:

Applescript:


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:

Applescript:


--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.

Offline

 

#22 2019-05-11 06:13:09 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 91

Re: Can Applescript read an XML file stored as a variable?

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 ?

Offline

 

#23 2019-05-11 07:54:23 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 222

Re: Can Applescript read an XML file stored as a variable?

JQ wrote:

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:

Applescript:


set TEST to (choose file)
set tracks to the mediainfoFile(TEST)'s tracks

The mediainfoFile handler is set to:

Applescript:


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?:

Applescript:


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



Population one open handler with multiply selection:

Applescript:


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

Last edited by KniazidisR (2019-05-11 08:29:22 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#24 2019-05-13 09:17:40 am

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

This is a stripped down version of the script that I'm working with to try and figure out the problem:

Applescript:


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.

Offline

 

#25 2019-05-13 09:40:48 am

JQ
Member
Registered: 2019-02-04
Posts: 36

Re: Can Applescript read an XML file stored as a variable?

Ok, I've got the repeat step working with this script:

Applescript:


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:

Applescript:


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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)