The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   Folder action for automatically adding new files to OmniFocus (http://forums.omnigroup.com/showthread.php?t=20161)

dave_m 2011-02-16 09:25 AM

Folder action for automatically adding new files to OmniFocus
 
1 Attachment(s)
A user emailed earlier in the week wondering if there was a way to automatically add bills that he was scanning as PDFs to OmniFocus as actions. He knew that he could have his scanning software place the PDFs in a particular folder, but wasn't sure how to get them into OmniFocus from there without too many clicks.

The answer I came up with is the attached Folder action script. It takes any files that are added to the folder and attaches them to individual actions in the quick entry window.

To use, extract the .scpt file from the .zip and place it in /Library/Scripts/Folder Action Scripts/. You can add the action to any folder by control-clicking that folder in the finder and choosing Services >Folder Actions Setup...

I am by no means an AppleScript expert so there may be some situations where the script fails, but in my testing here it works well. As always, your feedback is welcome

RobTrew 2011-02-16 10:23 AM

[QUOTE=dave_m;93442]To use, extract the .scpt file from the .zip and place it in /Library/Scripts/Folder Action Scripts/. [/QUOTE]

Looks good.

I would probably tend to install it in[INDENT]~/Library/Scripts/Folder Action Scripts
(where ~ is the user's home folder)[/INDENT]rather than[INDENT]/Library/Scripts/Folder Action Scripts
(under the root, affecting all users)[/INDENT]

kingsinger 2011-02-19 01:08 AM

I'm assuming it leaves the attached file in the folder with the folder action attached? Is that right?

whpalmer4 2011-02-19 08:19 AM

[QUOTE=kingsinger;93643]I'm assuming it leaves the attached file in the folder with the folder action attached? Is that right?[/QUOTE]
That's right, it doesn't do anything to the file, just opens the quick entry window and makes an action with the file embedded.

kingsinger 2011-02-19 12:40 PM

[QUOTE=whpalmer4;93680]That's right, it doesn't do anything to the file, just opens the quick entry window and makes an action with the file embedded.[/QUOTE]

What happens if you move the file later? Does OF put a copy of the file in its own directory somewhere that isn't affected by moving the file in the original folder? Or does the link adjust to the new location?

whpalmer4 2011-02-19 03:35 PM

Note that I said "embedded" :-) It puts a copy of the file in the database, and you'll sync it all around, if you are syncing. If it is a file you are going to change, you need to proceed with caution or you will be burned sooner or later by not getting your changes back into the database copy. I don't recommend embedding files that you plan to edit unless you really understand how it all works. Safer to put it somewhere like MobileMe or Dropbox where you can edit it directly, and just have a link to the file (or even just the name). On the other hand, if it is a file you are only going to reference, not change, and the size isn't too large, you shouldn't have any trouble.

If you do embed a large file, note that you won't be truly rid of it until after all of your syncing devices have synced the change where you delete it and had an opportunity to compact the database (normally at least an hour later).

If you've linked a file instead of embedding it, if the file is no longer present at the same location, attempts to open it will get you a file chooser dialog along with a warning that the file cannot be located at the old path. If you navigate to the new location, the link in the db gets updated, or you can cancel and deal with it at some other time. If this behavior makes you think "hmm, maybe they just store the path to the file at the time I attached it" you would be 100% correct. A nice side effect of doing it this way is that if you've stored the file in a place that has the same path on any system you use (MobileMe, for example), you don't actually have to cart the payload around.

KevinCoates 2011-03-03 10:24 AM

Close quick entry?
 
Hi

Thanks for the script. Does anyone have suggestions for an addition to the script that would close the quick entry window rather than leaving it open?

I've taken a look but so far it's beyond me.

Kevin

whpalmer4 2011-03-03 11:36 AM

1 Attachment(s)
Here's an updated version of the script that allows customization of the behavior. The two properties at the top of the file control whether files are embedded or linked, and whether actions are added to the Quick Entry window or filed directly in the Inbox without user intervention.

If you want different behavior in a bunch of different directories, it might be worth making a couple of copies of the script, setting the properties, and saving with descriptive names.

m_kuhn 2012-11-24 03:27 PM

Incredibly helpful
 
Being an absolute AppleScript novice, to say the least. Would there be a way to take this Folder action script and modify it to add a default Project and context to the action as well?

This would simply make my year.

Here on David Sparks site he describes how to make an action via applescript with a default project and context:

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

I am trying to accomplish some rather in depth Hazel, Paperless, automatic OCRing mojo and I thought I could simply look at the applescripts and combine them myself, but... Way beyond me. I know someone with some AppleScript chops can do it, probably in an instant.

Thanks in advance.

Jim Correia 2012-12-20 12:32 PM

1 Attachment(s)
[QUOTE=m_kuhn;117775]Being an absolute AppleScript novice, to say the least. Would there be a way to take this Folder action script and modify it to add a default Project and context to the action as well?

This would simply make my year.[/QUOTE]

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

m_kuhn 2012-12-21 02:51 AM

[QUOTE=Jim Correia;118539]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[/QUOTE]

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.

whpalmer4 2012-12-21 10:55 PM

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

[/code]

m_kuhn 2012-12-22 03:28 AM

[QUOTE=whpalmer4;118594]m_kuhn, it opens fine here, might be worth downloading it again. I'll also include a text copy of the script below.
[/QUOTE]

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
[/CODE]

whpalmer4 2012-12-22 09:33 AM

1 Attachment(s)
Does your icon with a bug on it look like this?

[URL=http://imageshack.us/photo/my-images/28/screenshot20121222at100.png/][IMG]http://img28.imageshack.us/img28/6116/screenshot20121222at100.png[/IMG][/URL]

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.

m_kuhn 2012-12-23 03:07 AM

1 Attachment(s)
[QUOTE=Jim Correia;118539]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[/QUOTE]

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

RobTrew 2012-12-23 06:53 AM

Hazel seems a little picky with the kind of [I]theFile[/I] 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 [I]Application[/I] 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
[/CODE]

[COLOR="White"]--[/COLOR]

RobTrew 2012-12-23 08:06 AM

1 Attachment(s)
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[/CODE]

[IMG]http://forums.omnigroup.com/attachment.php?attachmentid=2671&stc=1&d=1356298369[/IMG]

m_kuhn 2012-12-26 02:03 AM

Need a little more direction
 
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?

RobTrew 2012-12-26 06:04 AM

1 Attachment(s)
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

[COLOR="White"]--[/COLOR]

m_kuhn 2012-12-28 03:57 AM

Success!
 
Bingo! Thanks so much for all your efforts. I'm pretty much tickled pink. (Not sure if people still use that aphorism.)

Thanks again.


All times are GMT -8. The time now is 09:41 PM.

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