Problems Assigning a Project to a Task with Hazel

I’m trying to set my tasks containing a scanned receipt to a specific project after Hazel runs. I can get everything working except for theProject. If I get rid of theProject in both lines of code shown below, the file will get added to the Miscellaneous project since it has a context. Do you know why the code below will not send the new OF task to the project I’ve listed? Here is a quick view of my project hierarchy incase it matters. Ignore the parenthesis. They are just descriptions for this post.

Is the “/” the problem since it’s in the name of my project?

Miscellaneous (Single Action List)
Personal (Folder)
Singles Actions (Folder)
Finances / Bills (Single Action List)
Work (Folder)

set theTask to "Add to expenses"

tell application "Finder" to set file_name to (name of theFile)
tell application "OmniFocus"
	tell front document
		set theProject to first flattened project where its name = "Finances / Bills"
		set theContext to first flattened context where its name = "Devices"
		set NewTask to make new inbox task with properties {name:theTask, project:theProject, context:theContext}
		tell the note of NewTask
			make new file attachment with properties {file name:theFile, embedded:true}
		end tell
	end tell
end tell

I asked Omni engineer Tim Wood about this problem; here’s what he says:

‘project’ isn’t a defined property on inbox task (or ‘task’ for that matter – it has ‘containing project’ and ‘container’, which might be a task or project). In this case, you’d want to use the ‘assigned container’ on ‘inbox task’ to specify the project (or an existing task in the inbox, to make it a task group). Depending on your setting for “Clean Up inbox items” preference in the Organization pane, the new task may get immediately processed into the specified project, or it may remain in the inbox with the given assigned container.
So, for example, this works for me:

tell application "OmniFocus"
        tell front document
                set MyProject to project "Today" of folder "Omni"
                set MyContext to context "Home"
                set MyInbox to make new inbox task with properties {name:"Test", assigned container:MyProject, context:MyContext}
        end tell
end tell

I hope this helps!

3 Likes

Thank you for looking into this. I’m still learning AppleScript, so I may be confused on basic things. I wasn’t sure if I should replace my code with your code, so I was modifying mine with some of the ideas you’ve listed. Since I’m getting mixed results, these are the questions I have.

  1. Does the project Today already exist in your database?
  2. Does the folder Omni already exist in your database?
  3. Does the context Home already exist in your database?
  4. Will this code create these if they don’t exist?

Will your code work if your project, context, and folder are towards the bottom of a hierarchy? I wasn’t sure if I should stick with using the terms first flattened.

Examples

  • Project
    • Project
    • Project
      • Today
  • Context
    • Context
    • Context
    • Context
      • Home
  • Folder
    • Folder
      • Omni