OmniFocus add document

A very nice poster, McUsrII, put up an attempt to help me with the following AppleScript:

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 : "@Mac"

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"
               set theContext to (first flattened context where its name = pDefaultContext)
               set theProject to (first flattened project where its name = pDefaultProject)
               
               if (pUseQuickEntry) then
                   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
                       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

It is an attempt to combine the following two scripts:

-- 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

And this script:

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 *)

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
                    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
                
            end tell
        end repeat
    end try
end adding folder items to

Unfortunately McUsrII’s script to try to combine them didn’t seem to work as a Folder Action. Perhaps I’m screwing something up, I have very very little experience.

I did see one little thing that I should have added in my original request though, the ability for the script/folder action to add a custom default Spotlight Comment to the file after adding it to my OmniFocus database. I’m doing some, by my standards, sophisticated sorting and filing rules with Hazel and asking Hazel to look for a specific Spotlight Comment applied after adding the file to OmniFocus seems to be the only way to assure the Hazel rule isn’t triggered before the Folder Action has a chance to do its thing.

Many Many thanks in advance and to McUsrII for the help and pointers.

Hello.

It works now, at least for me. :slight_smile: It is nifty! I haven’t added the Spotlight comment, unsure of what you mean, if you mean the kind of comment you can edit or make by the Show Info window in Finder, then I can add that for you.


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
	end try
end adding folder items to

Yes, you’ve got it exactly on the Spotlight comment. It still isn’t working yet for me though. I’m obviously doing something not right I’m sure. When I click on the button to open scriplet in editor it pops open in AppleScript Editor, I save it as a script to my Folder Action Scripts Folder, setup a folder with it as the attached script in Folder Actions Setup.app, and proceed to try to add a file to that folder. Nothing comes up. I get the above results, i.e. nothing, whether I change the default Project and Context from your original Incoming and MacFolderTest defaults or something that is in my OmniFocus database.

When I go about the same procedure with this, the original script, it works fine minus, of course, the added project and context.

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 *)

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
					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
				
			end tell
		end repeat
	end try
end adding folder items to

Any ideas?? I really appreciate your help, this has been great! I’m so close but screwing something up obviously.

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: