2

I try to create Applescript to extract value from .xfdf

XFDF

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
  <annots>
    <square color="#FF0000" creationdate="D:20130828114843+05'30'" date="D:20130828114901+05'30'" flags="print" name="Xi6cOkAWgWHcAhpfBkR5A7" page="0" rect="347.7599999991828,1041.8400000004283,453.5999999989341,1056.9600000003927" subject="Rectangle" title="1 im, awltest7 (AWLTEST7.IM)">
      <contents-richtext>
        <body>
          <p>Text Not Clear</p>
        </body>
      </contents-richtext>
      <popup open="yes" page="0" rect="453.5999999989341,944.4600000003926,573.5999999989341,1056.9600000003927" />
    </square>
    <square color="#FF0000" creationdate="D:20130828114910+05'30'" date="D:20130828114919+05'30'" flags="print" name="ptmmBKtfoDEbVzirMgZLnY" page="0" rect="511.1999999987987,1092.960000000308,550.7999999987057,1123.9200000002352" subject="Rectangle" title="2 im, awltest7 (AWLTEST7.IM)">
      <contents-richtext>
        <body>
          <p>Incorrect dimension</p>
        </body>
      </contents-richtext>
      <popup open="yes" page="0" rect="550.7999999987057,1011.4200000002352,670.7999999987056,1123.9200000002352" />
    </square>
    <square color="#FF0000" creationdate="D:20130828114956+05'30'" date="D:20130828115004+05'30'" flags="print" name="8LaAl2Upx4LEaQptQKXoZx" page="0" rect="355.67999999916424,731.5200000011573,431.99999999898483,750.2400000011135" subject="Rectangle" title="3 im, awltest7 (AWLTEST7.IM)">
      <contents-richtext>
        <body>
          <p>Incorrect Text</p>
        </body>
      </contents-richtext>
      <popup open="yes" page="0" rect="431.99999999898483,637.7400000011133,551.9999999989849,750.2400000011135" />
    </square>
  </annots>
</xfdf>

Applescript

set theXMLFile to ((choose file) as string)

tell application "System Events"
    tell XML element "xfdf" of contents of XML file theXMLFile
        set typeText to (value of XML element "p")
        set nameText to (value of XML element "p")
    end tell
end tell

When I run the script I get this error: System Events got an error: Can’t get XML element "p" of XML element "xfdf" of contents of XML file. Is there anywhere I can extract these value from the XML. Value to extract out "popup", "p".
Edit

set theXMLFile to ((choose file) as string)
tell application "System Events"
    set theXMLFile to XML file theXMLFile
    set loops to XML elements of XML element "annots" of XML element "xfdf" of theXMLFile whose name is "square"
    set coor to {}
    repeat with i from 1 to (count loops)
        --set end of p to value of XML element "square" of XML element "body" of XML element "contents-richtext" of item i of squares
        set end of coor to value of XML attributes of XML element "square" of item i of loops
    end repeat

end tell
squares
chinna_82
  • 6,353
  • 17
  • 79
  • 134

2 Answers2

5

You are directly asking the xfdf element for p, but xfdf is not its parent. You need to dig down into the hierarchy to reach it.

set theXMLFile to ((choose file) as string)
tell application "System Events"
    set theXMLFile to XML file theXMLFile
    set squares to XML elements of XML element "annots" of XML element "xfdf" of theXMLFile whose name is "square"
    set p to {}
    set attrs to {}
    repeat with i from 1 to (count squares)
        set end of p to value of XML element "p" of XML element "body" of XML element "contents-richtext" of item i of squares
        set end of attrs to value of XML attributes of XML element "popup" of item i of squares
    end repeat
end tell
p --> {"Text Not Clear", "Incorrect dimension", "Incorrect Text"}
attrs --> {{"yes", "0", "453.5999999989341,944.4600000003926,573.5999999989341,1056.9600000003927"}, {"yes", "0", "550.7999999987057,1011.4200000002352,670.7999999987056,1123.9200000002352"}, {"yes", "0", "431.99999999898483,637.7400000011133,551.9999999989849,750.2400000011135"}}

Another option is to use the XML Tools Scripting Addition from Late Night Software.

Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30
  • im getting **error "Can’t make \"xfdf\" into type integer." number -1700 from "xfdf" to integer** – chinna_82 Jan 23 '14 at 02:08
  • Make sure `theXMLFile` is an XML file element. I've updated the code in my answer to that effect. – Darrick Herwehe Jan 23 '14 at 04:36
  • its only extract **

    **. How i get from ****values like ``

    – chinna_82 Jan 23 '14 at 10:20
  • [Using the System Events dictionary](https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptx/concepts/work_with_as.html#//apple_ref/doc/uid/TP40001568-1153006) shows that you need to access the `XML attributes` of the `popup` element. Code has been edited to reflect this. – Darrick Herwehe Jan 23 '14 at 13:30
  • One last question. I've edited my question to get **square** value but it throws, `Can’t get XML element \"square\" of XML element 1 of XML element 1 of XML element 1 of contents of XML file`.Please help. – chinna_82 Jan 28 '14 at 03:43
3

I have written a recursive helper function that can solve this.

It searches the xml looking for a particular name and returns a list of XML Elements you can then iterate over to get your values.

The benefit of this is you don't need to know the absolute paths to an Element up front.

Example:

set theXMLFile to ((choose file) as string)

tell application "System Events"
    set xmlData to XML file theXMLFile

    set found_elements to my getXMLElementsByName("p", contents of xmlData)

    log ("Found " & (count of found_elements) & " <p> nodes")

    repeat with i from 1 to count of found_elements
        log (tab & quote & (the value of (item i of found_elements) as text) & quote)
    end repeat

    set found_elements to my getXMLElementsByName("popup", contents of xmlData)
    log ("Found " & (count of found_elements) & " <popup> nodes")       
end tell

on getXMLElementsByName(search_name, search_xml_element)

    set found to {}

    using terms from application "System Events"            
        tell search_xml_element
            set c to the count of XML elements
            repeat with i from 1 to c
                if (the name of XML element i is search_name) then
                    set found to found & {XML element i}
                end if

                if (the (count of XML elements of XML element i) > 0) then
                    set children_found to my getXMLElementsByName(search_name, XML element i)
                    if (the (count of children_found) > 0) then
                        set found to found & children_found
                    end if
                end if

            end repeat
        end tell
    end using terms from

    return found
end getXMLElementsByName
adamh
  • 3,222
  • 1
  • 20
  • 16
  • im not getting any output – chinna_82 Jan 23 '14 at 10:18
  • @chinna_82 Make sure you've got the event log open in AppleScript Editor (with the Result toggle un-checked), or alternatively change the 'log' calls to 'display dialog' – adamh Jan 23 '14 at 10:48