/** * @@@BUILDINFO@@@ Humanizer.jsx 0.3 Wed Dec 27 2017 20:23:52 GMT+0100 */ /* Humanizer Copyright 2017 Jaroslav Bereza - j@bereza.cz - www.bereza.cz/ps The MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Changelog: ========== 0.3 - ads AM code to humanizer code conversion - ads option for code formating 0.2 - ads getObject and playObject - new example how to use this tool 0.1 - initial release */ /* JSON Public Domain. NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. See http://www.JSON.org/js.html */ "object"!=typeof JSON&&(JSON={}),function(){"use strict";function f(t){return t<10?"0"+t:t}function this_value(){return this.valueOf()}function quote(t){return rx_escapable.lastIndex=0,rx_escapable.test(t)?'"'+t.replace(rx_escapable,function(t){var e=meta[t];return"string"==typeof e?e:"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+t+'"'}function str(t,e){var r,n,o,u,f,a=gap,i=e[t];switch(i&&"object"==typeof i&&"function"==typeof i.toJSON&&(i=i.toJSON(t)),"function"==typeof rep&&(i=rep.call(e,t,i)),typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i)return"null";if(gap+=indent,f=[],"[object Array]"===Object.prototype.toString.apply(i)){for(u=i.length,r=0;r 0){ if(id.length === 4){ try { var typeID = charIDToTypeID(id); if(typeIDToStringID(typeID) === ""){ return stringIDToTypeID(id); } return typeID } catch (e) { return stringIDToTypeID(id); } } return stringIDToTypeID(id); } } Error.runtimeError(19, id); // Bad Argument return undefined; } //////////////////////// // Elementar functions //////////////////////// Humanizer.descriptorToJsonString = function (descriptor){ if(!descriptor) { return new ActionDescriptor(); } var result; var convertDesc = new ActionDescriptor(); convertDesc.putObject( ID.object, ID.object, descriptor ); var jsonDesc = executeAction( ID.convertJSONdescriptor, convertDesc, DialogModes.NO ); result = jsonDesc.getString(ID.json); return result; } Humanizer.jsonStringToObject = function (jsonString){ var result = JSON.parse(jsonString); return result; } Humanizer.objectToJsonString = function (object, spacesIndentation){ var result = JSON.stringify(object, null, spacesIndentation); return result; } Humanizer.jsonStringToDescriptor = function (jsonString){ var jsonDesc = new ActionDescriptor(); var key, convertedDesc; jsonDesc.putString( ID.json , jsonString); convertedDesc = executeAction( ID.convertJSONdescriptor, jsonDesc, DialogModes.NO ); key = convertedDesc.getKey(0); convertedDesc = convertedDesc.getObjectValue(key); return [key,convertedDesc]; } Humanizer.playObject = function(actionID, object, playDialogModes){ playDialogModes = playDialogModes || DialogModes.NO; object = Humanizer.objectToDescriptor(object)[1]; var returnedDesc = app.executeAction(Humanizer._resolveID(actionID), object, playDialogModes); var result = Humanizer.descriptorToObject(returnedDesc); return result; } Humanizer.playObjectReturnDesc = function(actionID, object, dialogMode){ dialogMode = dialogMode || DialogModes.NO; object = Humanizer.objectToDescriptor(object)[1]; return app.executeAction(Humanizer._resolveID(actionID), object, dialogMode); } Humanizer.amCodeToHumanizedCode = function(code, spacesIndentation){ code = code.replace(/executeAction/g,'customHumanizerExecuteAction'); code = code.replace(/app.executeAction/g,'customHumanizerExecuteAction'); return eval(code); function customHumanizerExecuteAction(actionID, amDescriptor, playDialogModes){ actionID = Humanizer._getBestNameFromID(actionID); var descObj = Humanizer.descriptorToJsonString(amDescriptor); if(spacesIndentation){ descObj = Humanizer.formatJSONString(descObj, spacesIndentation); } if(!playDialogModes){ playDialogModes = DialogModes.NO; } var humanizerCode = """var descriptor = """+descObj+""";\n"""+ "Humanizer.playObject('"+actionID+"',descriptor,"+playDialogModes.toString()+" );"; return humanizerCode; } } ////////////////////// // Shortcut functions ////////////////////// Humanizer.descriptorToObject = function (descriptor){ return Humanizer.jsonStringToObject(Humanizer.descriptorToJsonString(descriptor)); } Humanizer.objectToDescriptor = function (object, spacesIndentation){ return Humanizer.jsonStringToDescriptor(Humanizer.objectToJsonString(object, spacesIndentation)); } Humanizer.getObject = function(referenceObject){ if(!referenceObject){Error.runtimeError(19, id); /* Bad Argument*/} return Humanizer.playObject("get", referenceObject, DialogModes.NO) } Humanizer.getDescriptor = function(referenceObject){ if(!referenceObject){Error.runtimeError(19, id); /* Bad Argument*/} return Humanizer.playObjectReturnDesc("get", referenceObject, DialogModes.NO) } }());