How to get data from xml file into clipboard or variables inside Keyboard Maestro?

I have a xml file that my editing software generates. I would like to be able to go in and read data inside XML file and save it to clipboard or variable inside Keyboard Maestro?

I was thinking of just modifying the xml to be a shell script, but I don’t know xml that well and if shells work that way.

My quick answer being the novice that I am is that you could read a file in. And do regex on it to grab the contents that you want. That is providing the tag is specific (e.g.

). Because if there are multiple you will need some mechanism to determine which one you get data from. I am going to give this a try and post what I come up with.

Wow what I suspected worked well. This program is off the hook! Tell me if you need help understanding any part of it. In the zip is a macro to import and a test xml file. The macro pulls in the data from within the label tag and puts it in the clipboard. You will have to point to the path of the XML file. (1.3 KB)

1 Like

Chris’ method will work well if your needs are relatively simple.

But parsing XML, HTML, etcetera gets really hairy as the document complexity increases.

At some point you need to use more specific tools.

OSX’s shell command PlistBuddy works really well for simple queries.

/usr/libexec/PlistBuddy -c Print:BluetoothVersionNumber /Library/Preferences/

AppleScript and System Events can do more sophisticated queries:

set plistFilePath to "/Library/Preferences/"

tell application "System Events"
  tell property list file plistFilePath
    set nameList to name of property list items
    if nameList contains "BRPairedDevices" then
      set BRPairedDevices to value of property list item "BRPairedDevices"
    end if
  end tell
end tell

Satimage’s XMLLib.osax is faster and more sophisticated yet, although most people need some help to get started using it.

I imagine JavaScript has tools for parsing XML, so Rob will probably chime in with some more goodies.


I've put one example at:

and a little more detail in the gist which is linked at the foot of that page