Drafts Action Directory

Eval Draft

Posted by @jtokash, Last update 5 months ago

// Does a JavaScript eval on the current draft and puts the result at the bottom of the draft.
// Some examples:

var output = “”;

output += “Model: ”+device.model+“\n”;
output += “OS: ”+device.systemName+“\n”;
output += “OS version: ”+device.systemVersion+“\n”;
output += “Battery level: ”+device.batteryLevel+“\n”;

function getContentsOfURL(url){
var http = HTTP.create();
var response = http.request({
“url”:url
});

if (response.success) {
    var text = response.responseText;
    var data = response.responseData;
}
return text;

}

var stockData = JSON.parse(getContentsOfURL(“https://api.iextrading.com/1.0/stock/AAPL/quote”));

output += “\n”+stockData.symbol+“: ”+stockData.latestPrice+“\n”;

// End your script with the variable you’d like to see in the result.
output;

//-//

Model: iPad
OS: iOS
OS version: 11.2.5
Battery level: 0.8299999833106995

AAPL: 172.5

Install

Steps

  • script

    // See online documentation for examples
    // http://drafts5-help.agiletortoise.com/
    
    var endOfCodeMarker = "//-//";
    
    var code = draft.content;
    
    var indexOfResultMarker = draft.content.indexOf(endOfCodeMarker);
    if(indexOfResultMarker>-1){
    	code = draft.content.substring(0,indexOfResultMarker);
    }
    
    var output = eval(code);
    
    draft.content = code + endOfCodeMarker+"\n\n"+ output;
    draft.update();
    

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.