Attaching a file via script

Hi,

since OmniFocus 2 ist out I struggle with this script. The script takes a scanned file (in this case a unpaid invoice) and creates an OmniFocus task including the file:

 set DAYS_FROM_START to 5

tell application "Finder" to set theFile to (first item of (selection as list)) as alias
tell application "Finder" to set file_path to (POSIX file of theFile)
tell application "Finder" to set file_name to (name of theFile)

set dteBase to current date

set theNote to "Scanned " & (dteBase as string) & " 

"
set projectname to "Verwaltung"

set ContextName to "Bank"

set theTask to "offene Rechnung \"" & file_name & "\""

tell application "OmniFocus"
	set task_title to theTask
	tell default document
		-- GET A REFERENCE TO AN EXISTING PROJECT (OR NEW) PROJECT 
		set lstProj to flattened projects where name = projectname
		if lstProj ≠ {} then
			set oProj to item 1 of lstProj
		else
			set oProj to (make new project with properties {name:projectname})
		end if
		
		-- GET A REFERENCE TO AN EXISTING (OR NEW) CONTEXT
		set lstContexts to flattened contexts where name = ContextName
		if lstContexts ≠ {} then
			set oContext to item 1 of lstContexts
		else
			set oContext to (make new context with properties {name:ContextName})
		end if
		
		-- MAKE A NEW TASK UNDER THE SPECIFIED PROJECT, 
		-- ATTACHING IT TO THE SPECIFIED CONTEXT
		-- AND STAMPING IT WITH A START DATE
		
		set dteStart to dteBase - (time of dteBase) -- zero the time to midnight
		set dteStart to dteStart + (0 * days) -- and add three days
		
		set dteDue to dteStart + (1 * days) -- Due N days after start
		
		tell oProj
			tell (make new task with properties {name:task_title, context:oContext, defer date:dteStart, due date:dteDue, flagged:false})
				set its note to theNote
				tell its note to make new file attachment with properties {file name:file_name, embedded:true}
			end tell
		end tell
	end tell
end tell

Well,… it dowsn’t work with OmniFocus 2. I think there is something wrong with POSIX handling, but I can’t figure it out. I hope you can help me. I’m not really into Apple Script.

Thanks
Regards,
Kai

In the line quoted above, try replacing file_name with file_path. Does that do what you want?

No ist dos not. Then I get:

2014-10-13 09:56:16.103 hazelworker[42549] AppleScript error: {
    NSLocalizedDescription = "The variable file_path is not defined.";
    NSLocalizedFailureReason = "The variable file_path is not defined.";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: null()>";
    OSAScriptErrorBriefMessageKey = "The variable file_path is not defined.";
    OSAScriptErrorMessageKey = "The variable file_path is not defined.";
    OSAScriptErrorNumberKey = "-2753";
    OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'utxt'(\"file_path\")>";
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";

thanks for the help.

Regards

That’s strange! When I read your post, it looked like that should be defined up at the top of the script:

tell application “Finder” to set file_path to (POSIX file of theFile)

But if that’s not defined anymore, I guess we could just use POSIX file of theFile directly, i.e.

tell its note to make new file attachment with properties {file name:POSIX file of theFile, embedded:true}

Hope this helps!

Hi,

I am trying to attach a file to an action which is in the inbox. But it gives the following error. What is this happening?

Also, does it make any difference to code this JS rather than AppleScript?

/* E-waste /
/
text note
/
/
test.wav /
/
file:///Users/cagatay/Desktop/test.wav */
Result:
Error -10014: Handler only handles single objects.

//////////
OF = Application(‘OmniFocus’)
OF.includeStandardAdditions = true

var Finder = Application(‘Finder’)
var selection = Finder.selection()

var theTask = OF.defaultDocument.flattenedTasks.whose({name: ‘E-waste’})
console.log(theTask.name())
var theNote = theTask.note
console.log((theNote.text()))

selection.forEach(function(item) {
var theFileAttachement = OF.FileAttachment({name:item.name(), fileName: item.url(), embedded: false})
console.log(theFileAttachement.name)
console.log(theFileAttachement.fileName)
theNote.fileAttachments.push(theFileAttachement) ///// Error -10014: Handler only handles single objects.
})

//theFileAttachement.save();

thanks nice tip