Wednesday, August 20, 2014

#1 2012-11-28 10:23:46 pm

kuhn
Member
Registered: 2012-11-27
Posts: 22

OmniFocus add document

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

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:

Applescript:

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

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


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.

Last edited by kuhn (2012-11-28 10:24:23 pm)


Filed under: Hazel, omnifocus

Offline

 

#2 2012-11-29 12:55:54 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: OmniFocus add document

Hello.

It works now, at least for me. 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.

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

Last edited by McUsrII (2012-11-29 12:57:52 am)


Filed under: folder action, omnifocus

Offline

 

#3 2012-11-29 03:28:55 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

McUsrII wrote:

Hello.

It works now, at least for me. 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.

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. 

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


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.


Filed under: omnifocus

Offline

 

#4 2012-11-29 04:01:37 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: 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.

Offline

 

#5 2012-11-29 04:17:31 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

McUsrII wrote:

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.

Offline

 

#6 2012-11-29 04:29:29 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: OmniFocus add document

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.

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

Offline

 

#7 2012-11-29 06:37:39 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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.

Offline

 

#8 2012-12-02 01:52:00 pm

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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

Offline

 

#9 2012-12-02 04:00:52 pm

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: OmniFocus add document

kuhn wrote:

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

Offline

 

#10 2012-12-02 04:15:48 pm

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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.

Offline

 

#11 2012-12-03 10:08:20 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: OmniFocus add document

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.

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

Last edited by McUsrII (2012-12-04 06:32:57 am)


Filed under: folderaction, omnifocus

Offline

 

#12 2012-12-04 05:56:18 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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:

Applescript:

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.

Offline

 

#13 2012-12-04 06:06:31 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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

Offline

 

#14 2012-12-04 06:09:41 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: OmniFocus add document

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

Last edited by McUsrII (2012-12-04 06:21:31 am)


Filed under: folder action, omnifocus

Offline

 

#15 2012-12-04 06:11:52 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

Hmm.  No toOmnifocus.log there.

Offline

 

#16 2012-12-04 06:21:55 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: OmniFocus add document

Then there were no errors.


Filed under: omnifocus

Offline

 

#17 2012-12-04 06:23:28 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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

Offline

 

#18 2012-12-04 06:30:13 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: OmniFocus add document

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.

Last edited by McUsrII (2012-12-04 06:31:17 am)


Filed under: omnifocus

Offline

 

#19 2012-12-04 06:32:41 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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!

Offline

 

#20 2012-12-04 06:34:17 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: OmniFocus add document

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

Last edited by McUsrII (2012-12-04 06:36:53 am)


Filed under: omnifocus

Offline

 

#21 2012-12-04 06:36:15 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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

Offline

 

#22 2012-12-04 06:52:23 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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.

Offline

 

#23 2012-12-11 06:41:13 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

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.

Offline

 

#24 2012-12-11 08:28:12 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2268
Website

Re: OmniFocus add document

I surmise, you'd like to select one file in the frontmost finder window, and then run the script, or would you prefer to choose a file through a dialog box? smile

I am sorry that folder actions doesn't work for you. You can increase the priorites of them, by using the renice command, to make them more responsive, but that is nothing I am doing per post here, so you'd have to read up on both renice an launch agents, to make that hack work.


Filed under: omnifocus

Offline

 

#25 2012-12-11 08:37:24 am

kuhn
Member
Registered: 2012-11-27
Posts: 22

Re: OmniFocus add document

Not sure about the answer to your question. I'm planning on having Hazel take the file from a folder and run the script once Hazel detects a file, or files, added to the folder. I know that Hazel always expects "end" rather than "property" to being the script if that helps at all.

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.063 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)