Hazel create OF task with file

Hi,
I’m trying to create a workflow with Hazel and OF. Hazel read file tags and then creates a task on OF existing projects (all based on files tags) and also attach the file to task.

I’ve search for applescripts to run on Hazel but I always get the same error “Error on running Applescript on file…”

I’ve Hazel 5, Omnifocus 3 and MacOS Ventura 13.2.

Does anyone get this error or could help?

Thanks upfront.

Bruno

Could you post your attempt ?

The script I’m using was found in this forum. I’ve analysed Hazel logs and It seems the problem maybe related to definition of the variables “Context” and “Project”. It seems don’t know how to get them…

tell application “Finder”
set fileName to (name of theFile)
set {name:fileName, name extension:nameExtension} to theFile
set fileName to text 1 thru ((get offset of “.” & nameExtension in fileName) - 1) of fileName
set theDate to current date
set theTask to “Read «” & fileName & “»”
set theNote to return & return & "Imported by Hazel on " & (theDate as string) & "

"
end tell

tell application “OmniFocus”
tell front document
set theContext to first flattened context where its name = “Any Device”
set theProject to first flattened project where its name = “Read/Review”
tell theProject
set theAction to make task with properties {name:theTask, context:theContext}
set note of theAction to theNote
tell the note of theAction
make new file attachment with properties {file name:theFile, embedded:false}
end tell
end tell
end tell
end tell

I changed Context to tag and I obtained the error in Hazel:

If I use “create new task” I get this error:
NSLocalizedDescription = “OmniFocus got an error: AppleEvent handler failed.”;

If I use create new inbox task I get the error:
“OmniFocus got an error: Can\U2019t make or move that element into that container.”

The new script is:
tell application “Finder”

set fileName to (name of theFile)

set {name:fileName, name extension:nameExtension} to theFile

set fileName to text 1 thru ((get offset of “.” & nameExtension in fileName) - 1) of fileName

set theDate to current date

set theTask to “Review «” & fileName & “»”

set theNote to return & return & "Imported by Hazel on " & (theDate as string) & "

"

end tell

tell application “OmniFocus”

tell front document

set theTag to first flattened tag where its name = “Review”

set theProject to first flattened project where its name = “ToDo”

tell theProject

set theTask to make task with properties {name:theTask, tag:theTag}

set note of theTask to theNote

tell the note of theTask

make new file attachment with properties {file name:theFile, embedded:false}

end tell

end tell

end tell

end tell

I don’t have OF 3 (version 4, instead) but I’ll take a look.

Thanks for the help.
The script returns the same error outside Hazel. but I’ve isolated the error It’s on tag…
<set theTask to make task with properties {name:theTask, tag :theTag}>

full code (run outside Hazel):

tell application “Finder”

set filePath to (POSIX path of (target of front window as alias))

set theFile to the selection as alias

set fileName to (name of theFile)

set {name:fileName, name extension:nameExtension} to theFile

set fileName to text 1 thru ((get offset of “.” & nameExtension in fileName) - 1) of fileName

set theDate to current date

set theTask to “Review «” & fileName & “»”

set theNote to return & return & "Imported by Hazel on " & (theDate as string) & "

"

end tell

tell application “OmniFocus”

tell front document

set theTag to first flattened tag where its name = “Review”

set theProject to first flattened project where its name = “ToDo”

tell theProject

set theTask to make task with properties {name:theTask, tag :theTag}

set note of theTask to theNote

tell the note of theTask

make new file attachment with properties {file name:theFile, embedded:false}

end tell

end tell

end tell

end tell

Could you enclose the code in triple backticks ?

It’s easier to read as formatted code

yes off course. I just don’t know that:

tell application “Finder”

set filePath to (POSIX path of (target of front window as alias))

set theFile to the selection as alias

set fileName to (name of theFile)

set {name:fileName, name extension:nameExtension} to theFile

set fileName to text 1 thru ((get offset of “.” & nameExtension in fileName) - 1) of fileName

set theDate to current date

set theTask to “Review «” & fileName & “»”

set theNote to return & return & "Imported by Hazel on " & (theDate as string) & "

"

end tell

tell application “OmniFocus”

tell front document

set theTag to first flattened tag where its name = “Review”

set theProject to first flattened project where its name = “ToDo”

tell theProject

set theTask to make task with properties {name:theTask, tag :theTag}

set note of theTask to theNote

tell the note of theTask

make new file attachment with properties {file name:theFile, embedded:false}

end tell

end tell

end tell

end tell

Here you have. Try this:

tell application "Finder"
    set filePath to (POSIX path of (target of front window as alias))
    set theFile to the selection as alias
    set fileName to (name of theFile)
    set {name:fileName, name extension:nameExtension} to theFile
    set fileName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
    set theDate to current date
    set theTask to "Review «" & fileName & "»"
    set theNote to return & return & "Imported by Hazel on " & (theDate as string) & "
    "
end tell

tell application "OmniFocus"
    tell front document
        set theTag to first flattened tag where its name = "Review"
        set theProject to first flattened project where its name = "ToDo"
        tell theProject
            set theTask to make task with properties {name:theTask}
            set note of theTask to theNote
            tell the note of theTask
                make new file attachment with properties {file name:theFile, embedded:false}
            end tell
            add theTag to end of tags of theTask
            return theTask
        end tell
    end tell
end tell
1 Like

It works perfectly. Awesome!! Thanks

Great. I’m glad about that.