OmniFocus add document

Hello.

Did you create the project, and context up front? I did, as both a top level project, residing right below the library, and the same with the context.

Yup, no dice. Don’t know what it is I could possibly be doing wrong. If you can think of the simplest explanation as to what I could possibly be doing wrong, something that wouldn’t occur to you because it is too obvious, I’ve probably done that.

I also had, (have) the Project window open in OmniFocus, I run Omnifocus 1.10.4 and Mac Os X Snow Leopard 10.6.8
Try the handler below, I have added some error handling code in it. When you have tested the folder action again, go to /Users/you/Library/logs and double click on toOmnifocus.log and see if something interesting turns up.


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 : "Incoming"
property pDefaultContext : "MacFolderTest"

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
	on error e number n
		my logit("Error: " & e & " " & n, "toOmnifocus")
	end try
end adding folder items to

to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

I am away for a few days and going iOS only while away so it’ll be a little bit before I can try this. If it makes any difference, by the way, I’m running the latest version of a mountain lion and the latest version of OmniFocus.

Again, thanks so much for your help, when we figure out what it is that I’m doing wrong this script will be amazingly helpful! I hope you get some good use out of it as well.

Success!!! I have no idea what was different, but this script worked beautifully. I even was able to refile my Project and my Context into their deeply buried sub-folders and and Sub-contexts and everything still worked perfectly.

I almost feel guilty asking if you could add the Spotlight Comment ability to the file after it has done the importing into OmniFocus… almost

I am not sure if I can do that, I know I can add to or change the comment temporarily, add the file to OmniFocus, and then restore the previous Spotlight Comment.

What are you supposed to have written in your Spotlight comment? :slight_smile:

I’m wanting Hazel to do its magic on the file after the Folder Action has been triggered. The only way I can think of doing that without Hazel acting on the file before the Folder Script has a chance to is for the folder action to add a Spotlight comment to the file that Hazel then looks for.

I’ve toyed with having the AppleScript embedded into the Hazel rule, but it gets messy it seems…

To answer your question, though, it wouldn’t really matter as long as it was a word or two word phrase that wouldn’t exist in the file until after The Folder action has had a chance to do its magic.

Thanks again.

Hello.

I hope this works, you’ll see where you can add your words. Please do check before and after, that you had a comment, that you changed, (that the item in OmniFocus has your words. And that the original item, has the original Spotlight comment.



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 : "Incoming"
property pDefaultContext : "MacFolderTest"

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"
				set file_name to (name of this_item)
				set file_cmt to (comment of this_item)
				set comment of this_item to "whatever you like"
			end tell
			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
			if pEmbedFile then tell application "Finder" to set comment of this_item to file_cmt
			
		end repeat
	on error e number n
		my logit("Error: " & e & " " & n, "toOmnifocus")
	end try
end adding folder items to

to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit


Well, good news and bad news. Good news:

I think I know why originally the script wouldn’t work for me.

Bad news:

I can’t get any scripts to work for me. Either through Folder actions or through Hazel. This Folder action won’t even run:

tell application "Finder" to display dialog "Folder action triggered"

I think I was having this same difficulty before when you were able to get the script working on your machine but I couldn’t.

I think my computer is in full revolt.

Safari takes a minute and a half to open, the Spotlight index is constantly being rebuilt (often without the blinking dot inside the magnifying glass for some reason). Hazel won’t even recognize a PDF in a folder.

Long story short, I’m going to try the script as soon as I can with my new computer that is on order. Good news: I should be one of the first ones to get a 27" iMac.

Bad news: 27" aren’t shipping for another few weeks and, because I’m a softie, I ordered it through my local Apple Reseller who has been very good to me over the years and since they’re not a Apple Store or a top tier reseller will probably have their order pushed back.

I really appreciate all of your help with this and greatly look forward to trying it out and implementing it when I get a new machine in, likely, 4 weeks.

Ok. Well, Folder actions seem to be working for the moment. But no go on the Spotlight Comment added to the file. It will still add the attached document to my OmniFocus with the preset Project and context but no spotlight comment, I’m afraid.

After waiting for 2 minutes for Console to open, I couldn’t immediately see where the log for that was… ?

Hello.

Go to .Users:You:Library:Logs and doubleclick toOmnifocus.log then it will open in the Console.app.

Strange that you had to wait for such a long time.

I’ll test it in the mean time, here.

Edit:

I have instrumented it, and tested it here, the problem is that OmniFocus doesn’t keep its metadata.

So, if the script apparently works for you, you are out of luck when it comes to having the Spotlight comment in OmniFocus. :frowning:

Hmm. No toOmnifocus.log there.

Then there were no errors.

Bummer. Oh well. It still works great as it is. Thanks so much for all your help. You’ve been immensely patient and helpful.

Well, I’m thinking of letting the dropped file, stay where it is, (before it was dragged), but add comments to it, and then make an alias to it, which I attach to the Omnifocus action, at my leisure.

What do you think about that solution?

Another solution, would maybe just be to add a link as an attachment, by giving another value to the property in the script, but then you’ll have to comment out the part that restores the comment of course.

Not sure how that might work in my workflow as the files usually get saved directly to the Folder Actioned folder…

I suppose I could incorporate another waypoint folder where the file sits before being automatically dropped into the OmniFocus Folder actioned folder.

Worth a try!

I changed the script above, to consider the pEmbedFile. As it is, now the toOmnifocus folder you use, work as your waypoint folder, if you don’t embed by setting pEmbed to false.

As it is, the file is in that folder, and also in an OmniFocus folder when you embed it, so for my purpose, I think it is enough with having it in that drop-folder.

But I think I will look at just embedding an alias later, as it leads to less redundancy, and I can delete it from the folder after it has been embedded, that way, a file, can be part of several tasks and projects, and stay where it is supposed to be. :slight_smile:

Lost me, I’m afraid. I do embed the file in the OmniFocus action.

I see, I think.

For my purposes though I’m archiving a copy of the file elsewhere via a Hazel rule and am having OmniFocus embed the file so that I can have it on my various satellite devices also running OmniFocus. I agree that if all I was doing was working locally with the projects then not embedding the file would be by far the better choice.

As it stands now, before the file ever gets to the to OmniFocus folder it is filed away permanently. I then have a perspective setup which is linked to a recurring routine whereby I actually delete the completed actions that have the embedded files in them so that I don’t bog down my OF database.

Quick question, I hate to bother you again after all the effort you’ve put in, but is there any way that this could be modified to run as a stand alone script? I’m having a lot of trouble again with Folder Actions just not working and am hoping to incorporate the script into a Hazel rule.

Many many thanks.