PDA

View Full Version : Applescript access to attachments ?


RobTrew
2007-10-07, 11:05 AM
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, 09: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):


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



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, 02:38 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.

file name of file attachment 1 as string works but not
file name of file attachment 2 as string

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, 03:04 PM
Is there a way to access the list of attachments?

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:

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

iNik
2007-12-10, 01: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, 09: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 should work is something like:


tell application "OmniFocus"
tell default document
set note of project "b" to note of project "a"
end tell
end tell


or a subrange of the note:


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

RobTrew
2008-02-20, 03: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 ?