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.
 
Quote:
Originally Posted by RobTrew View Post
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
I have to admit that I'm really stupid. This script works like a charm if you copy it right. I forgot the first line again... Sorry for beeing so dumb!

Just one more question: Can you help me to add a Start Date for that task as well? I'd like something like "today+3".

Cheers,
Ben

PS: Rob you are truly a hero!
 
Quote:
Originally Posted by hubutz View Post
I forgot the first line again...
No problem easily done.

Quote:
a Start Date for that task as well? I'd like something like "today+3"
Perhaps something along these lines:

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

set dteBase to current date
set theNote to "Scanned " & (dteBase 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
		-- AND STAMPING IT WITH A START DATE
		
		set dteStart to dteBase - (time of dteBase) -- zero the time to midnight
		set dteStart to dteStart + (3 * days) -- and add three days
		
		tell oProj
			tell (make new task with properties {name:task_title, context:oContext, start date:dteStart})
				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
 
Quote:
Originally Posted by hubutz View Post
Just one more question: Can you help me to add a Start Date for that task as well? I'd like something like "today+3".
This works for me.

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

set theDate to current date

set theStartDate to current date
set time of theStartDate to 0 + (8 * hours)

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, start date:(theStartDate) + (3 * days)})
				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
To modify the time change the number of hours using 24hour time in the this line of code.

Code:
set time of theStartDate to 0 + (8 * hours)
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -8. The time now is 11:04 PM.


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