Creating new Projects using Javascript for Automation


#1

I’ve been trying to write some code to download JSON information from remote server and create a Project out of it. Sadly I seem to be tripping right out of the gate…I can’t figure out how to simply create a new document and make it appear.

Here’s what I have so far:

OmniPlan = Application("OmniPlan");
OmniPlan.includeStandardAdditions = true;

//create project, give it a name
var myProject = new OmniPlan.Project();
myProject.title = "My Random Project";

//add to document
var myDocument = new OmniPlan.Document();
myDocument.name = "Bob";
myDocument.project = myProject;

//set as document in window
//though frankly I'd rather just create a new windows and add this document to it.
var window = OmniPlan.windows[0];  //yes, this is sloopy, I'm just prototyping, there's a window open...
window.document = myDocument;

I know that the last line is making the whole thing crash, so I can assume that’s just totally wrong. But using window.add(); or window.document.add(); doesn’t work. Neither does .push for that matter. Besides which I’m not even sure I’m trying to put it in the right place, or if it even can be put!

If anyone can give me some direction? I’m sorry it’s such a n00bish question but…I really just can’t figure out where to go with this… I wish I could find a “getting started” tutorial for Javascript for Automation with Omniplan…


Edit:

Did some further work and found that this procedure works with TextEdit:

app = Application("TextEdit");
app.activate();

myDoc = app.Document();
myDoc.name = "testing";
app.documents.push(myDoc);

And yet this conceptually similar code will not work in OmniPlan:

OmniPlan = Application("OmniPlan");
OmniPlan.activate();

document = OmniPlan.Document();
document.name = "Testing";
OmniPlan.documents.push(document);

But instead gives me the following error in the console:

app = Application("OmniPlan")
    app.activate()
    Application("OmniPlan").documents.push(app.Document({"name":"Testing"}))
        --> Error -10000: AppleEvent handler failed.
Result:
Error -10000: AppleEvent handler failed.

With this pop-up also appearing:

Error on line 6: Error: Instantiating a document with properties {
    displayName = Testing;
} resulted in no windows.

Starting to wonder if perhaps this is some sort of bug? Or if there’s just some magical property I’m not setting that needs to be set?


#2

More likely to get seen by Omni staff if you email support through the menu system Help option.

I’m not using Omniplan these days, and don’t have a license for a current version, but generally the pattern should be that you make a new object and then push it into the relevant collection (documents, windows, tasks etc.) Until you have placed the new object in a collection, you won’t see anything in the GUI

https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW1

Looking quickly at a trial version, I see that in the object model a project is a property of a document, so you may need to start by creating a new Document object and adding that to the documents collection.

I notice however, that doing this in the usual way is throwing errors in both AppleScript and JavaScript, so a quick word with Support is probably your next step.


#3

Thanks for confirming this! I’ll put a word in with support and see where it gets me :).


#4

I’ve just started looking at Javascript for Omni apps. I’d really like to be able to use Siri to say something like '“Add a task named foo in OmniPlan".

Currently, I say “Remind me to foo” and I have things set up to shunt to OmniFocus (which is GTD heaven, BTW).

Bob