{"id":2500,"date":"2014-11-07T13:34:34","date_gmt":"2014-11-07T13:34:34","guid":{"rendered":"https:\/\/www.mjtnet.com\/blog\/?p=2500"},"modified":"2018-03-25T09:10:05","modified_gmt":"2018-03-25T09:10:05","slug":"macro-scheduler-14-2-python-json-xml-auto-documenting-macro-recorder","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2014\/11\/07\/macro-scheduler-14-2-python-json-xml-auto-documenting-macro-recorder\/","title":{"rendered":"Macro Scheduler 14.2 &#8211; Python, JSON, XML and Auto Documenting Macro Recorder"},"content":{"rendered":"<p>I am pleased to announce that we have today released <a href=\"https:\/\/www.mjtnet.com\/macro-scheduler.htm\">Macro Scheduler<\/a> 14.2<\/p>\n<p>Amongst other things this new version includes the following great new features:<\/p>\n<ul>\n<li>Self Documenting Macro Recorder with snapshots of Windows\/Objects being Activated\/Clicked on<\/li>\n<li>The ability to run Python code within your macros!<\/li>\n<li>A native JSON Parser<\/li>\n<li>A native XML Parser using XPath<\/li>\n<\/ul>\n<p>Here&#8217;s an overview of these main new features, but for a more complete list of improvements <a href=\"https:\/\/www.mjtnet.com\/mswhatsnew.htm\">view the history list here<\/a>. <\/p>\n<p><strong>Self Documenting Macro Recorder<\/strong><\/p>\n<p>Ever recorded a macro and then tried to edit it but couldn&#8217;t figure out which bit did what?  Well, now when you record a macro the macro recorder will take snapshots of windows that are activated and objects that you click on.  These will be inserted into the script as image comments, right above the line of code they refer to.  So, now, it&#8217;s easier to see exactly what your macro is doing and which bits you want to edit\/copy.<\/p>\n<p><strong>Run Python Code Inside your Macros<\/strong><\/p>\n<p>In 14.2 there&#8217;s a new function called <a href=\"https:\/\/www.mjtnet.com\/manual\/pyexec.htm\">PyExec<\/a>.  It allows you to run real <a href=\"http:\/\/en.wikipedia.org\/wiki\/Python_(programming_language)\">Python<\/a> code and get back the values of any Python variables you specify.  You&#8217;ll need to install the Python 2.7 DLL to your Macro Scheduler folder (or compiled .EXE folder) &#8211; there&#8217;s a link in the help file to a zip file with all the files you need.<\/p>\n<p>You can run any Python code and even use third party Python imports.  This is pretty wild IMO &#8211; the possibilities are endless.  Here&#8217;s a simple example:<\/p>\n<pre class=\"brush:macroscript\">\/*\r\nFirst ensure Python27.dll and imports are in your Macro Scheduler program folder.\r\nDownload and unzip this file:\r\nhttps:\/\/www.mjtnet.com\/software\/python27.zip\r\n*\/\r\n\r\nLet>url=http:\/\/ip.jsontest.com\/\r\n\r\n\/*\r\npython_code:\r\n\r\nimport urllib2\r\nimport json\r\n\r\n# grab data from http:\/\/ip.jsontest.com\/ - see www.jsontest.com\r\nresponse = urllib2.urlopen('%url%')\r\n\r\n# load the json\r\ndict = json.loads(response.read())\r\n\r\n# get the ip member\r\nmyip = dict[\"ip\"]\r\n\r\n# make a nice string representation of the dict\r\nsdict = json.dumps(dict)\r\n\r\n# Anything we print to IO is returned in the PYExec output var\r\nprint \"All Done\"\r\n*\/\r\n\r\n\/\/Load the Python code to a variable\r\nLabelToVar>python_code,pcode\r\n\r\n\/\/Run the code and request the values of the sdict and myip variables ...\r\nPYExec>pcode,output,sdict,myip\r\n\r\n\/\/Display the IP address\r\nMessageModal>Your pubic IP is: %myip%<\/pre>\n<p><strong>Parsing JSON<\/strong><\/p>\n<p>In the last few years <a href=\"http:\/\/en.wikipedia.org\/wiki\/JSON\">JSON<\/a> has become a very popular way to transmit data objects.  Most web services now use it so it cannot be ignored.  While you can parse JSON using string handling\/regex having an easy to use native parser is essential.  Enter: <a href=\"https:\/\/www.mjtnet.com\/manual\/jsonparse.htm\">JSONParse<\/a><\/p>\n<p>In the above Python example we used Python&#8217;s own JSON handling to extract an IP address from some JSON retrieved from a web service.  Here&#8217;s how we can do the exact same thing using native MacroScript code:<\/p>\n<pre class=\"brush:macroscript\">HTTPRequest>http:\/\/ip.jsontest.com\/,,GET,,JSON\r\nJSONParse>JSON,ip,myIP\r\nMessageModal>Your pubic IP is: %myIP%<\/pre>\n<p>And here&#8217;s an example which gets data out of a more complicated structure:<\/p>\n<pre class=\"brush:macroscript\">\/\/Requires Macro Scheduler 14.2\r\n\r\n\/*\r\nMyJSON:\r\n{ \"uid\" : \"1234\", \r\n  \"clients\" : [\"client1\",\"client2\",\"client3\"],\r\n  \"people\" : [{\"Name\":\"Marcus\",\"Age\":\"21\"},{\"Name\":\"Dorian\",\"Age\":\"18\"}],\r\n  \"color\" : \"red\",\r\n  \"size\" : 14 }\r\n*\/\r\n\r\nLabelToVar>MyJSON,sJSON\r\n\r\nJSONParse>sJSON,uid,result\r\nJSONParse>sJSON,clients,result\r\nJSONParse>sJSON,clients[1],result\r\nJSONParse>sJSON,people[1].Name,result<\/pre>\n<p><strong>Parsing XML<\/strong><\/p>\n<p>Of course <a href=\"http:\/\/en.wikipedia.org\/wiki\/XML\">XML<\/a> is also still very popular.  Up til now we&#8217;ve had to parse XML using substring handling\/regex or use Microsoft&#8217;s XML object via VBScript &#8211; which is a little over-complicated IMO.  We now have a simple to use native XML parser function &#8211; <a href=\"https:\/\/www.mjtnet.com\/manual\/xmlparse.htm\">XMLParse<\/a>.  Here&#8217;s an example:<\/p>\n<pre class=\"brush:macroscript\">\/\/Requires Macro Scheduler 14.2\r\n\r\nLabelToVar>XML,sXML\r\nXMLParse>sXML,\/bookstore\/book,val,numBooks\r\nLet>k=0\r\nRepeat>k\r\n  Let>k=k+1\r\n  XMLParse>sXML,\/bookstore\/book[%k%]\/title\/text(),val,len\r\n  MessageModal>val\r\nUntil>k=numBooks\r\n\r\n\/*\r\nXML:\r\n<&#63;xml version=\"1.0\" encoding=\"UTF-8\"&#63;>\r\n\r\n<bookstore>\r\n\r\n<book category=\"COOKING\">\r\n  <title lang=\"en\">Everyday Italian<\/title>\r\n  <author>Giada De Laurentiis<\/author>\r\n  <year>2005<\/year>\r\n  <price>30.00<\/price>\r\n<\/book>\r\n\r\n<book category=\"CHILDREN\">\r\n  <title lang=\"en\">Harry Potter<\/title>\r\n  <author>J K. Rowling<\/author>\r\n  <year>2005<\/year>\r\n  <price>29.99<\/price>\r\n<\/book>\r\n\r\n<book category=\"WEB\">\r\n  <title lang=\"en\">XQuery Kick Start<\/title>\r\n  <author>James McGovern<\/author>\r\n  <author>Per Bothner<\/author>\r\n  <author>Kurt Cagle<\/author>\r\n  <author>James Linn<\/author>\r\n  <author>Vaidyanathan Nagarajan<\/author>\r\n  <year>2003<\/year>\r\n  <price>49.99<\/price>\r\n<\/book>\r\n\r\n<book category=\"WEB\">\r\n  <title lang=\"en\">Learning XML<\/title>\r\n  <author>Erik T. Ray<\/author>\r\n  <year>2003<\/year>\r\n  <price>39.95<\/price>\r\n<\/book>\r\n\r\n<\/bookstore>\r\n*\/<\/pre>\n<p><strong>How to Download\/Upgrade<\/strong><\/p>\n<p>If you have a valid maintenance plan you can download the update from <a href=\"https:\/\/www.mjtnet.com\/dldregd.htm\">your account here<\/a>.  If your maintenance has lapsed you can also purchase upgrades and renew maintenance from within your account.<\/p>\n<p>Trial versions can be <a href=\"http:\/\/www.mjtnet.com\/downloads.htm\">downloaded here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am pleased to announce that we have today released Macro Scheduler 14.2 Amongst other things this new version includes the following great new features: Self Documenting Macro Recorder with snapshots of Windows\/Objects being Activated\/Clicked on The ability to run Python code within your macros! A native JSON Parser A native XML Parser using XPath [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,37],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2500"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=2500"}],"version-history":[{"count":14,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2500\/revisions"}],"predecessor-version":[{"id":2870,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2500\/revisions\/2870"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=2500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=2500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=2500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}