Creating new Projects using Javascript for Automation

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?

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.

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

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

Does anyone have an example of using JavaScript for Automation to create a new OmniPlan project (and then add a task and save the file)?

Here is a complete example (create + customize project, add group / tasks / dependencies, save project).

var app = Application("/Applications/OmniPlan.app")
app.includeStandardAdditions = true

function run() {

	app.activate()
  
	// close existing windows (without saving)
	for (var i in app.windows) {
		app.close(app.windows[i], {saving:"no"})
	}

	// create new document
	doc = app.Document({
		template: app.defaultTemplate
	}).make();

	// give the project a title
	proj = doc.project
	proj.title = "my project"

	// need to get doc again after changing project
	doc = app.documents[0]

	// specify which columns to show
	window = app.windows[0]
	
	window.taskColumns = [
		"Violations","Notes","Title","Effort", // default columns
		"Start","End","Dependencies","Assigned"
	]

	// remove existing tasks (defaultTemplate creates "Task 1")
	for (var i in doc.tasks) {
		doc.tasks[i].delete()
	}

	// create group / tasks
	group = app.Task({
		name: "my group",
		taskType: "group task"
	})

	task = app.Task({
		name: "my task",
		//taskType: "standard task"
	})

	milestone = app.Task({
		name: "my milestone",
		taskType: "milestone task"
	})

	// add the new group / tasks
	doc.tasks.push(group)
	group.tasks.push(task)
	group.tasks.push(milestone)
	
	// create links (after creating the tasks)
	milestone.depend({upon:task})

	// save the document (.oplx extension is added)
	//file = Path("/Users/myname/Downloads/newPlan") // absolute path
	file = [app.pathTo("downloads folder"),"newPlan"].join("/") // special folder
	app.save(doc, {in:file} )
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.