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

 
Folder action for automatically adding new files to OmniFocus Thread Tools Search this Thread Display Modes
Quote:
Originally Posted by Jim Correia View Post
Attached is a modification of Bill Palmer's script which allows you to specify the project and context when adding the task. (You can't specify a project when using Quick Entry due to a limitation in OmniFocus 1, and the items are always left in the Inbox since I minimally modified the existing script.)

Hopefully you can adapt this to your Hazel workflow without too much difficulty.

—Jim
Thanks. Unfortunately the script won't open in AppleScript Editor nor will Hazel recognize it...

Error:

The document “Add files as OmniFocus actions.scpt” could not be opened.
 
m_kuhn, it opens fine here, might be worth downloading it again. I'll also include a text copy of the script below.

Jim, thanks for improving on my improvement! I don't think I've seen that approach to setting the project. Should the script perhaps also include a "compact" command to force the new action to be swept out into the assigned project? Tradeoff seems to be a possibly unexpected clean up when a file hits that folder vs. the new task lingering in the Inbox even though both project and context are assigned.

Code:
property pEmbedFile : true (* if true, file will be embedded
                               if false, file will be linked *)
property pUseQuickEntry : true (* if true, Quick Entry window used and left open
                              if false, actions added directly to Inbox *)
property pProjectName : missing value (* set this to a project name, "Clean the Garage" for example, to set a project when creating tasks *)
property pContextName : missing value (* set this to a context name, "Home" for example, to set a context when creating tasks *)

on adding folder items to this_folder after receiving added_items
	local __asDB
	try
		repeat with i from 1 to number of items in added_items
			set this_item to item i of added_items
			tell application "Finder" to set file_name to (name of this_item)
			tell application "OmniFocus"
				if (pUseQuickEntry) then
					tell quick entry
						open
						set NewTask to make new inbox task with properties {name:file_name}
						tell the note of NewTask
							make new file attachment with properties {file name:this_item, embedded:pEmbedFile}
						end tell
						activate
					end tell
				else
					tell front document
						set NewTask to make new inbox task with properties {name:file_name}
						tell the note of NewTask
							make new file attachment with properties {file name:this_item, embedded:pEmbedFile}
						end tell
					end tell
				end if
				if pProjectName is not missing value and not pUseQuickEntry then
					-- We can only set the project if we aren't opening the quick entry window due to a limitation in OmniFocus 1.x
					try
						set task_project to item 1 of (get flattened projects of front document whose name is pProjectName)
						set assigned container of NewTask to task_project
					on error msg
						set msg to "Couldn't set project: " & msg
						display alert msg buttons {"OK"}
					end try
				end if
				if pContextName is not missing value then
					try
						set task_context to item 1 of (get flattened contexts of front document whose name is pContextName)
						set context of NewTask to task_context
					on error msg
						set msg to "Couldn't set context: " & msg
						display alert msg buttons {"OK"}
					end try
				end if
			end tell
		end repeat
	end try
end adding folder items to
 
Quote:
Originally Posted by whpalmer4 View Post
m_kuhn, it opens fine here, might be worth downloading it again. I'll also include a text copy of the script below.
Bizarre. I get the same error when I download the script and the icon for the script has a bug on it.

When I copy and paste the script into AppleScript Editor I'm ok. Though it still throws an error of some kind that won't let the script run.

However, I still can't use this in a Hazel workflow because it is a Folder Action script. Is there anyway to change it to a stand alone script that can be triggered on a file from a Hazel workflow?

When I try to compile the script in Hazel I get the following error: Expected “end” but found “property”.

The below script works flawlessly as a Folder Action but I'd love to get it as a stand alone script!:

Code:
property pEmbedFile : true (* if true, file will be embedded
if false, file will be linked *)
property pUseQuickEntry : false (* if true, Quick Entry window used and left open
if false, actions added directly to Inbox *)
property pDefaultProject : "missing value"
property pDefaultContext : "missing value"

on adding folder items to this_folder after receiving added_items
	try
		repeat with i from 1 to number of items in added_items
			set this_item to item i of added_items
			tell application "Finder" to set file_name to (name of this_item)
			tell application "OmniFocus"
				if (pUseQuickEntry) then
					set theContext to (first flattened context where its name = pDefaultContext as rich text)
					set theProject to (first flattened project where its name = pDefaultProject)
					tell quick entry
						open
						tell theProject
							set NewTask to make new task with properties {name:file_name, context:theContext}
							tell the note of NewTask to make new file attachment with properties {file name:this_item, embedded:pEmbedFile}
						end tell
						activate
					end tell
				else
					tell front document
						set theContext to (first flattened context where its name = pDefaultContext as rich text)
						set theProject to (first flattened project where its name = pDefaultProject)
						tell theProject
							set NewTask to make new task with properties {name:file_name, context:theContext}
							tell the note of NewTask to make new file attachment with properties {file name:this_item, embedded:pEmbedFile}
						end tell
					end tell
				end if
				
			end tell
		end repeat
	end try
end adding folder items to

Last edited by m_kuhn; 2012-12-22 at 03:31 AM.. Reason: Update
 
Does your icon with a bug on it look like this?



That's the icon for Script Debugger. That may also explain why it won't open for you, even in the Applescript Editor. I'll attach a version compiled by AE to this post and you can see if that downloads any better for you, just to clear up the mystery. Jim probably uses Script Debugger, and so do I, so that might explain why we don't get the error you are seeing.

Yes, the script is a Folder Action script, not expected to work directly in Hazel without modification. I don't have time right now to modify it for you, but the modification is simple enough. I'll try to get to it in the next day or two if someone else doesn't beat me to it.
Attached Files
File Type: zip Add files as OmniFocus actions.scpt.zip (5.3 KB, 689 views)
 
Quote:
Originally Posted by Jim Correia View Post
Attached is a modification of Bill Palmer's script which allows you to specify the project and context when adding the task. (You can't specify a project when using Quick Entry due to a limitation in OmniFocus 1, and the items are always left in the Inbox since I minimally modified the existing script.)

Hopefully you can adapt this to your Hazel workflow without too much difficulty.

—Jim
Quite right you were, this one opened in AppleScript Editor just fine. However, the script doesn't seem to work even as a Folder Action. I've fiddled and played with it, but the best I was ever able to get was a new quick entry action but without a file attached, OmniFocus then promptly crashed. I'm not sure how I was able to even get that now as I'm not able to recreate it.

This Folder Action however, works flawlessly, could this one be modified as a stand alone AppleScript? :
Attached Files
File Type: zip Add to OmniFocus Action.scpt.zip (4.4 KB, 702 views)
 
Hazel seems a little picky with the kind of theFile code that it is happy to process (at least as embedded Applescript) – it appears to balk, for example, at properties and subroutines.

I'm sure there's a way around it, but in the meanwhile you can make a simple droplet script by saving the following from Applescript Editor in Application format, and then dragging and dropping files onto it.

Code:
property pEmbedFile : true (* if true, file will be embedded
if false, file will be linked *)

property pDefaultProject : "Example Project"
property pDefaultContext : "Example Context"

on open argv
	tell application id "OFOC"
		set oProject to my GetObject(pDefaultProject, "project")
		set oContext to my GetObject(pDefaultContext, "context")
		set oDoc to front document
		
		repeat with theFile in argv
			tell application "Finder" to set {strPath, strName} to {POSIX path, name} of theFile
			tell oDoc
				tell (make new task at end of tasks of oProject with properties {name:strName, context:oContext})
					tell its note to make new file attachment with properties {file name:strPath, embedded:pEmbedFile}
				end tell
			end tell
		end repeat
	end tell
end open

on GetObject(strName, strType)
	run script "tell application id \"OFOC\"
		tell front document
			set lst to " & strType & "s where its name = \"" & strName & "\"
			if lst ≠ {} then
				return item 1 of lst
			else
				return (make new " & strType & " with properties {name:\"" & strName & "\"})
			end if
		end tell
	end tell"
end GetObject
--

Last edited by RobTrew; 2012-12-23 at 07:04 AM..
 
A Hazel version (external rather than embedded Applescript file)
(can still be used as a droplet)

Code:
property pTitle : "Add document to OF through Hazel"
property pVer : "0.03"

property pstrProject : "Example Project"
property pstrContext : "Example Context"

property poProject : missing value
property poContext : missing value

property pEmbed : true

-- USE AS A DROPLET (save as .app)
on open argv
	repeat with oArg in argv
		PlaceFile(oArg)
	end repeat
end open

-- OR SPECIFY IN A HAZEL RULE (save as .scpt)
on hazelProcessFile(theFile)
	PlaceFile(theFile)
end hazelProcessFile

-- ( GENERIC ) 
on PlaceFile(theFile)
	tell application "Finder" to set {strPath, strName} to {POSIX path, name} of theFile
	
	tell application id "OFOC"
		tell front document
			-- If the script is saved as a compiled .scpt, using persistent properties avoids retrieving the project and context each time
			if poProject is missing value then set poProject to my GetObject(pstrProject, "project")
			if poContext is missing value then set poContext to my GetObject(pstrContext, "context")
			
			tell (make new task at end of tasks of poProject ¬
				with properties {name:strName, context:poContext}) ¬
				to tell its note to make new file attachment with properties {file name:strPath, embedded:pEmbed}
		end tell
	end tell
end PlaceFile

-- Makes an object of this name if it can't find one
on GetObject(strName, strType)
	run script "tell application id \"OFOC\"
		tell front document
			set lst to flattened " & strType & "s where name = \"" & strName & "\"
			if lst ≠ {} then
				return item 1 of lst
			else
				return (make new " & strType & " with properties {name:\"" & strName & "\"})
			end if
		end tell
	end tell"
end GetObject
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2012-12-23 at 21.31.41.png
Views:	1424
Size:	57.7 KB
ID:	2671  

Last edited by RobTrew; 2012-12-23 at 12:33 PM.. Reason: Ver 0.03: Added 'flattened' keyword to GetObject()
 
Thanks Rob, looks great. I have to say, though, I need a little more direction on what to delete, etc... I've not been able to make either the last one or the previous one work in any of the formats including simply saving it to an app and using as a droplet.

Any ideas?
 
No need to delete anything.

If you save the attached text script as a compiled script (.scpt) you should be able to specify it in a Hazel rule, as above.

Working here with OS X 10.7 and Hazel build 886 of Version 3.0.16

--
Attached Files
File Type: applescript HazelPlaceFile.applescript (1.6 KB, 733 views)
 
Bingo! Thanks so much for all your efforts. I'm pretty much tickled pink. (Not sure if people still use that aphorism.)

Thanks again.
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export each object automatically as separate PNG files PipSqueak OmniGraffle General 1 2013-02-12 02:38 PM
Default to next action when adding a new action? ethant OmniFocus 1 for Mac 0 2010-10-06 01:55 PM
Where is my database saved? [A: the OmniFocus folder inside App Support folder] Michael Blake OmniFocus 1 for Mac 2 2009-08-10 02:08 PM
Action Groups Don't Get Marked as Done Automatically minato2 OmniFocus for iPhone 15 2008-11-25 12:39 PM
5.0 b Why are layers locked down automatically when editing 4.x files? dude OmniGraffle General 0 2008-01-23 06:29 PM


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


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