The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > OmniFocus > OmniFocus Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Adjusting of AppleScript to add project, context and start date Thread Tools Search this Thread Display Modes
Hi there,

I fould a really helpful script on asianefficiency.com, which can be used with a hazel rule (http://www.asianefficiency.com/task-...focus-hotspot/).

The script automatically looks at a folder and creates a new task for new files in the folder (and moving the file away). This is quite nice, but it would be a lot more helpful for me if I could assign a project, a context and a start time (today+x).

Anyone able to help me? Didn't found a documentation of all the OmniFocus Parameters (and still if i'd found it i think i wouldn't have managed to get this done; kinda new to apple and not the biggest nerd).

Here is the script:
Code:
tell application "Finder" to set file_name to (name of theFile)
set theDate to current date
set theNote to "Scanned " & (theDate as string) & " 

"
set projectname to "Proceed Files"

set theTask to "Proceed: \"" & file_name & "\""

tell application "OmniFocus"
	set task_title to theTask
	tell default document
		set newTask to make new inbox task with properties {name:task_title}
		set theProject to projectname
		set note of newTask to theNote
		tell the note of newTask
			make new file attachment with properties {file name:theFile, embedded:false}
		end tell
	end tell
end tell
the variable "projectname" seems not to be working, I've set that to a project i have "Proceed Files" but... this is not added to the task.

Would be cool if you could help me :-)

Cheers,
Ben
 
You'll need to get references to Project and Context objects.

Something roughly like this:

Code:
tell application "Finder" to set file_name to (name of theFile)

set theDate to current date
set theNote to "Scanned " & (theDate as string) & " 

"
set projectname to "Proceed Files"

set ContextName to "Some Context"

set theTask to "Proceed: \"" & file_name & "\""

tell application "OmniFocus"
	set task_title to theTask
	tell default document
		set oProj to my GetProject(it, projectname)
		set oContext to my GetContext(it, ContextName)
		
		tell oProj
			tell (make new task with properties {name:task_title, context:oContext})
				set its note to theNote
				tell its note to make new file attachment with properties {file name:file_name, embedded:false}
			end tell
		end tell
	end tell
end tell


on GetProject(oDoc, strName)
	using terms from application "OmniFocus"
		tell oDoc
			set lstProj to flattened projects where name = strName
			if lstProj ≠ {} then
				item 1 of lstProj
			else
				make new project with properties {name:strName}
			end if
		end tell
	end using terms from
end GetProject

on GetContext(oDoc, strName)
	using terms from application "OmniFocus"
		tell oDoc
			set lstContexts to flattened contexts where name = strName
			if lstContexts ≠ {} then
				item 1 of lstContexts
			else
				make new context with properties {name:strName}
			end if
		end tell
	end using terms from
end GetContext
 
Thanks a lot, I knew i'd not understand any of this.

Hazel says that it's expecting an "end" instead of the "on GetProject(oDoc, strName)", so it's actually not working :-(

Gonna try a bit myself, will see if that works (i doubt it :-d)
 
It seems that Applescript code embedded in Hazel can't use function definitions (Hazel probably wraps the code in a function handler).

You should be able to unpackage the functions along these lines:

Code:
tell application "Finder" to set file_name to (name of theFile)

set theDate to current date
set theNote to "Scanned " & (theDate as string) & " 

"
set projectname to "Proceed Files"

set ContextName to "Some Context"

set theTask to "Proceed: \"" & file_name & "\""

tell application "OmniFocus"
	set task_title to theTask
	tell default document
		-- GET A REFERENCE TO AN EXISTING PROJECT (OR NEW) PROJECT 
		set lstProj to flattened projects where name = projectname
		if lstProj ≠ {} then
			set oProj to item 1 of lstProj
		else
			set oProj to (make new project with properties {name:projectname})
		end if
		
		-- GET A REFERENCE TO AN EXISTING (OR NEW) CONTEXT
		set lstContexts to flattened contexts where name = ContextName
		if lstContexts ≠ {} then
			set oContext to item 1 of lstContexts
		else
			set oContext to (make new context with properties {name:ContextName})
		end if
		
		-- MAKE A NEW TASK UNDER THE SPECIFIED PROJECT, ATTACHING IT TO THE SPECIFIED CONTEXT
		tell oProj
			tell (make new task with properties {name:task_title, context:oContext})
				set its note to theNote
				tell its note to make new file attachment with properties {file name:file_name, embedded:false}
			end tell
		end tell
	end tell
end tell

Last edited by RobTrew; 2013-05-29 at 02:51 AM.. Reason: edited code
 
Hmm, thanks for the fast answer - this isn't producing an error in Hazel, but it doesn't create a task at all. Don't know if there is a way (for me) to see where it stucks or what it's actually doing.
 
Quote:
Originally Posted by hubutz View Post
Hmm, thanks for the fast answer - this isn't producing an error in Hazel, but it doesn't create a task at all. Don't know if there is a way (for me) to see where it stucks or what it's actually doing.
Note that the code doesn't aim to create a task in the inbox – it should be placing it directly under the project.
 
yeah, i got that, but actually it isn't adding a task at all, not in the inbox, not in the project... :(
 
I know this is not a full solution, but I thought you might be interested in two associated articles and scripts for creating tasks in OmniFocus via Hazel.

The first is the original David Sparks script. From this blog post http://macsparky.com/blog/2012/8/app...mnifocus-tasks

Code:
-- Lovingly crafted by David Sparks, The Omni Group, and Ben Waldie -- macsparky.com

set theDate to current date
set theTask to "Pay Life Insurance"
set theNote to "Lovingly Scanned by your Mac on " & (theDate as string)

tell application "OmniFocus"
    tell front document
        set theContext to first flattened context where its name = "Tech"
        set theProject to first flattened project where its name = "Finance"
        tell theProject to make new task with properties {name:theTask, note:theNote, context:theContext}
    end tell
end tell
The second is from Patrick Lenz who modified David Sparks script. From this blog post http://patricklenz.com/posts/create-...a-hazel-redux/

Code:
tell application "Finder" to set file_name to (name of theFile)

tell application "OmniFocus"
    set task_title to "Pay " & file_name
    tell default document
        set newTask to make new inbox task with properties {name:task_title}
        tell the note of newTask
            make new file attachment with properties {file name:theFile, embedded:false}
        end tell
    end tell
end tell
I tried combining these two scripts, but could at most only get a task with an attachment and a context into the inbox. This was what I have so far.

Code:
tell application "Finder" to set file_name to (name of theFile)

tell application "OmniFocus"
	set task_title to "Pay " & file_name
	tell default document
		set theContext to first flattened context where its name = "iMac"
		set newTask to make new inbox task with properties {name:task_title, context:theContext}
		tell the note of newTask
			make new file attachment with properties {file name:theFile, embedded:false}
		end tell
	end tell
end tell
I can get a task with attachment and context into the inbox. The addition of sending it to a project per the method from the David Sparks script does not work.

To get the obvious out of the way — yes, I did replace the "make new inbox task" with "make new task" when testing this with a line similar to

Code:
set theProject to first flattened project where its name = "Finance"
All comment and suggestions welcome.

Thank you in advance.
 
Quote:
Originally Posted by Dale View Post
I know this is not a full solution, but I thought you might be interested in two associated articles and scripts for creating tasks in OmniFocus via Hazel.

This was what I have so far.

Code:
tell application "Finder" to set file_name to (name of theFile)

tell application "OmniFocus"
	set task_title to "Pay " & file_name
	tell default document
		set theContext to first flattened context where its name = "iMac"
		set newTask to make new inbox task with properties {name:task_title, context:theContext}
		tell the note of newTask
			make new file attachment with properties {file name:theFile, embedded:false}
		end tell
	end tell
end tell
I can get a task with attachment and context into the inbox. The addition of sending it to a project per the method from the David Sparks script does not work.

To get the obvious out of the way — yes, I did replace the "make new inbox task" with "make new task" when testing this with a line similar to

Code:
set theProject to first flattened project where its name = "Finance"
All comment and suggestions welcome.

Thank you in advance.
Hi Dale,

thanks for sharing this. I've tried this one but it's not doing anything to my OmniFocus. I'm wondering if this could be because I have a german OSX?
 
I am not sure if your language setting would be affecting these scripts. I had many versions of the script silently fail within Hazel because of simple errors in the script itself. Check first that you modified "iMac" in my script from above to a context you currently have in your OmniFocus. It is not clear if you used the following line in your script or not.

Code:
set theProject to first flattened project where its name = "Finance"
If you did; please remove the line.

Does the script work?

If the script is not working, could you test the David Sparks script and the Patrick Lens script?

1. Create a folder on you desktop named "Test" and add it to Hazel.
2. Create a rule for the folder "Test" to run if a file named "sparks.txt" is added to the folder — have this rule run the David Sparks script.
3. Create another rule for the folder "Test" to run if a file named "lens.txt" is added to the folder — have this rule run the Patrick Lens script.

Please post your results. Did my modified script run? Did the David Sparks script run? Did the Patrick Lens script run?

Thank you.
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -8. The time now is 07:58 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.