The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   Applescript access to attachments ? (http://forums.omnigroup.com/showthread.php?t=5100)

RobTrew 2007-10-07 10:05 AM

Applescript access to attachments ?
 
At the time of writing, it seems that one can manually place PDF files as attachments to tasks, but that there is not yet an Applescript route to placing or referencing such attachments.

Is that correct, or am I missing something ?

Tim Wood 2007-10-09 08:09 PM

There are undoubtedly holes in the scripting support for attachments, so if you have a specific thing you are trying that isn't working, let us know.

Some snippets of commands that our automated tests do (not intended as a runnable script, just as example commands):

[code]
tell MyProject
tell note
-- Create an linked file attachment
make new file attachment with properties {file name:MyFile}

-- Get the path of the attachment
file name of first file attachment as string

-- Check whether it is embedded or linked
embedded of first file attachment

-- Save the data in the attachment to a file
tell first file attachment
save in "hfs:path:to:output.ext"
end

-- Create an embedded attachment (same as above; embedded just defaults to 'false')
make new file attachment with properties {file name:MyFile, embedded:true}

-- Duplicate an attachment
duplicate file attachment 1 to end of characters

-- Delete an attachment
delete file attachment 1

end
end
[/code]


If you can send in bug reports with scripts showing what you'd expect to work, it'll help (not sure if it'll make it into 1.0, but let us know if it is a show-stopper for a script you are trying to write).

Lutin 2007-12-08 01:38 PM

[QUOTE=Tim Wood;22519]There are undoubtedly holes in the scripting support for attachments, so if you have a specific thing you are trying that isn't working, let us know.[/QUOTE]

[CODE]file name of file attachment 1 as string[/CODE] works but not
[CODE]file name of file attachment 2 as string[/CODE]

Is there a way to access the list of attachments?

Thanks for being so reactive. I appreciate it. It's what allows us to customize OF very greatly, and make its strength.

Lutin 2007-12-08 02:04 PM

[QUOTE=Lutin;28016]
Is there a way to access the list of attachments?[/QUOTE]

Hmmm... note to myself: always check for an update to OmniFocus before a feature request: it might already be there.

This function will do what I asked for:

[CODE]-- Return a list of path of the files linked or enclosed in the note of the task
on getPathOfFilesAttached(aTask)
tell front document of application "OmniFocus"
tell note of aTask
set NumberOfFileAttached to number of file attachment
set i to 1
set ListOfPaths to {}
repeat while i ≤ NumberOfFileAttached
set end of ListOfPaths to file name of file attachment i as string
set i to i + 1
end repeat

end tell
end tell
return ListOfPaths as list
end getPathOfFilesAttached[/CODE]

iNik 2007-12-10 12:38 PM

The one place where attachments/links really falls down is in "get note". If I'm setting a variable to a note (e.g. to copy a task's info and note to a new task/project), I'd like that variable to not be plain text unless coerced, so that the whole note, files inclusive, can be copied and otherwise managed.

Tim Wood 2007-12-12 08:38 PM

Hrm; this was working but something in the attachment code seems to have broken. Definitely log a bug.

One issue that you need to be aware of is that since there is no AppleScript native type for rich text, if you do:

set MyNote to note of MyProject

it will be coerced to plain text. This isn't likely to change, but what [b]should[/b] work is something like:

[code]
tell application "OmniFocus"
tell default document
set note of project "b" to note of project "a"
end tell
end tell
[/code]

or a subrange of the note:

[code]
tell application "OmniFocus"
tell default document
set note of project "b" to paragraph 1 of project "a"
end tell
end tell
[/code]

RobTrew 2008-02-20 02:48 PM

Here is a challenge:

Can you see a syntactic route (through the inky orchards of Applescript) to copying attachments from an Omnifocus task to an Omnioutliner row ?

Kevin Yank 2011-03-23 04:08 AM

The code provided by Lutin above is not working for me. When I attempt to access one of the file attachments of the note of a task, I get a scripting error -10000.
[code] count every file attachment of note of inbox task id "n9bV1yd8CdN" of document id "m1VId-wcJcH"
--> 1
get file attachment 1 of note of inbox task id "n9bV1yd8CdN" of document id "m1VId-wcJcH"
--> error number -10000[/code]

I’ve reported this to the OmniFocus support ninjas. Hoping I’ve missed something obvious, because I’m not holding my breath for a speedy fix if it is indeed a bug. :(

Kevin Yank 2011-03-23 04:34 AM

OK, the problem seems to have to do with “embedded” file attachments. I’ve been able to access and save out file attachments using this code:[code]tell note of theItem
set NumberOfFileAttached to number of file attachment
set i to 1
set ListOfPaths to {}
repeat while i ≤ NumberOfFileAttached
if embedded of file attachment i then
set tempFilePath to my pathForTemporaryFile("OmniFocus")
tell file attachment i
save in tempFilePath
end tell
set end of ListOfPaths to tempFilePath
else
set end of ListOfPaths to file name of file attachment i as string
end if
set i to i + 1
end repeat
end tell[/code]

This raises a new problem: embedded file attachments are saved as files with no type. I’ll start a new thread for this.

Lutin 2011-03-23 04:55 AM

It works with normal attachment, but not with embedded attachement.

You can test this with this adapted code:

[CODE]-- Return a list of path of the files linked or enclosed in the note of the task
on getPathOfFilesAttached(aTask)
tell front document of application "OmniFocus"
tell note of aTask
set NumberOfFileAttached to number of file attachment
set i to 1
set ListOfPaths to {}
repeat while i ≤ NumberOfFileAttached
if file attachment i is embedded then
set end of ListOfPaths to "embedded file: " & file name of file attachment i
else
set end of ListOfPaths to file name of file attachment i as string
end if

set i to i + 1
end repeat

end tell
end tell
return ListOfPaths as list
end getPathOfFilesAttached


tell application "OmniFocus"
tell default document
set myTask to task id (id of item 1 of selected trees of content of front document window)
set result to my getPathOfFilesAttached(myTask)
result
end tell
end tell
[/CODE]


You should contact the Ninjas :)


All times are GMT -8. The time now is 04:17 AM.

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