Using JXA to add tasks


#1

I have an external JSON file that I’ve created from Jira that I want to iterate through and create tasks in an existing omniplan document.

I can’t seem to understand the structure enough to add the tasks to the project. Below is the current code.

var docroot = "/Users/me/Documents/";

var doc = "2019.oplx";

var path = "/Users/me/issues_1547512253.json";

var app = Application("OmniPlan");

app.includeStandardAdditions = true;

// Open OmniPlan Document

app.open(docroot + doc);

    s = app.document.project.scenario;

    console.log(s.tasks.length);

    // Read Data 
    ObjC.import('Foundation');
    var fm = $.NSFileManager.defaultManager;
    var contents = fm.contentsAtPath(path.toString()); // NSData
    contents = $.NSString.alloc.initWithDataEncoding(contents, $.NSUTF8StringEncoding);

    // Eval data to objects
    data_struct = eval(ObjC.unwrap(contents));

    for(i = 0; i < data_struct.length; i++){
      console.log(data_struct[i].key);
      console.log(data_struct[i].url);
      console.log(data_struct[i].summary);
      console.log(data_struct[i].assignee);
      console.log(data_struct[i].status);
      task = app.make(app.Task({
            name: data_struct[i].key,
            startingDate: Date.now(),
            duration: 86400,
            completed: 0.0,
            completedEffort: 0,
            remainingEffort: 86400,
            priority: 1,
            taskStatus: "ok",
            effort: 86400,
            taskType: "standard task",
            staticCost: null,
            customData: null,
            style: null,
            startingConstraintDate: Date.now(),
            endingConstraintDate: Date.now(),
            startAfterDate: Date.now(),
            note: data_struct[i].summary + "\n" + data_struct[i].url		
      }));
      app.add(task,s);

    }

Output Below:

    app = Application("OmniPlan")
      app.open("/Users/me/Documents/2019.oplx")
        --> app.documents.byId("jWuB4Zntc7O")
      app.document.project.scenario.tasks.length
        --> 26

    /* 26 */
    /* TASK-124701 */
    /* https://example.com/rest/api/2/issue/417945 */
    /* repo is failed to get replicated */
    /* tomha */
    /* Done */
    app = Application("OmniPlan")
      app.make({style:null, completedEffort:0, note:"repo is failed to get replicated\nhttps://example.com/rest/api/2/issue/417945", startAfterDate:1496284655, endingConstraintDate:1496284655, startingDate:1496284655, taskType:"standard task", remainingEffort:86400, customData:null, staticCost:null, effort:86400, priority:1, completed:0, taskStatus:"ok", duration:86400, startingConstraintDate:1496284655, name:"TASK-124701"})
        --> Error -1701: Parameter is missing.
    Result:
    Error -1701: Parameter is missing.