Hello. There should be a lot to remove here, but I am not sure how Hazel runs these things, I know for sure that you won’t have any other option than to embed the file into Omnifocus, so I have removed that, I am unsure about the quick entry window however.
I hope you can try it from here, and we’ll modify some as we go along. But I am not the right man for Hazel, as I have never used it.
The last part of the script:
tell AddToOmniFocus to run {aFileList}
Shows how it is to be invoked, if it is embedded in a Hazel script.
If it is to be loaded into a Hazel-AppleScript, then you’d better have a run script (“hfs path” as alias ) with parameters {theList}
The list. Right now, the script fires for a list of aliases, this can be changed so that it fires for individual files, I am not sure what you need.
script AddToOmniFocus
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 run {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)
my logit("item file_name" & file_name, "toOmnifocus")
set file_cmt to (comment of this_item)
set comment of this_item to "whatever you like"
set file_cmt to (comment of this_item)
my logit("item file_cmt" & file_cmt, "toOmnifocus")
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:true}
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:true}
end tell
end tell
end if
end tell
# tell application "Finder" to set comment of this_item to file_cmt
tell application "Finder"
set file_cmt to (comment of this_item)
my logit("item file_cmt" & file_cmt, "toOmnifocus")
end tell
end repeat
on error e number n
my logit("Error: " & e & " " & n, "toOmnifocus")
end try
end run
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
end script
tell AddToOmniFocus to run {aFileList}
Edit
I think you will find what you need here: Create OmniFocus Tasks with AppleScript ” MacSparky