Use AppleScript to check if a project already exists before creating it with applescirpt

I’m looking to use Applescript (one or more scripts) to create projects and tasks associated with those projects. I want to connect Tinderbox with OmniFocus to do this. I’ve found some sample code that shows me how to create projects and tasks: https://github.com/jsit/omnifocus-3-applescript-guide, and Apple Script Pulled from https://forum.eastgate.com/t/an-applescript-that-creates-an-omnifocus-project-from-a-tinderbox-note-and-reciprocally-links-them/2914. What is not clear to me is how I can create use Applescript to create a task in a project that already exists in OmniFocus.

What I don’t know how to do is create projects in a specific folder or check to see if a project or task already exists before it is created. I’ve read something about the use of a “flaten” qualifier but it is not clear to me how to use it. Most of the examples I’ve found are in OmniFocus 2 and not 3. I’d love to get some guidance on how to make this work.

I’ve tried this:

tell application "Tinderbox 9"
	tell front document's selection
		set theTaskName to name
		set theTaskText to value of (attribute named "Text")
		set theProjectName to value of (attribute named "Project")
		set noteURL to value of (attribute named "NoteURL")
		set startDate to value of (attribute named "StartDate")
		set endDate to value of (attribute named "EndDate")
	end tell
end tell
tell front document of application "OmniFocus"
	set theProject to first flattened project where its name is theProjectName
	
	tell theProject
		set theTask to make new inbox task with properties {name:theTaskName, note:theTaskText}
	end tell
end tell

I get this error ** **OmniFocus got an error: Can’t make or move that element into that container.****". The is a project with this name so I’m not sure why the index is invalid.

Update, I got this to work:

tell application "Tinderbox 9"
	tell front document's selection
		set theTaskName to name
		set theTaskText to value of (attribute named "Text")
		set theProjectID to value of (attribute named "OmniFocusProdID")
		set noteURL to value of (attribute named "NoteURL")
	end tell
end tell
tell front document of application "OmniFocus"
	set theProject to first flattened project where its id is theProjectID
	
	tell theProject
		set theTask to make new task with properties {name:theTaskName, note:theTaskText}
		set omniFocusTaskID to id of theTask
	end tell
end tell
tell application "Tinderbox 9"
	tell front document's selection
		set value of (attribute named "Badge") to "link"
		set value of (attribute named "Color") to "poppy"
		set value of (attribute named "URL") to ("omnifocus:///task/" & omniFocusTaskID)
		set value of (attribute named "OmniFocusTaskID") to (omniFocusTaskID)
		
	end tell
end tell

Not I need to figure out how to pass dates. Tinderbox sends a UTC date that Omnifocus does not seem to recognize. Any idea?

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