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 ? |
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). |
[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. |
[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] |
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.
|
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] |
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 ? |
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. :( |
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. |
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 :) |
There is a table listing attached files in the cache.
You should be able to read it like this: [CODE]on run tell application "OmniFocus" tell front document window of default document set lstID to id of selected trees of content where class of value = task or class of value = inbox task if length of lstID < 1 then return set strTaskID to first item of lstID end tell end tell AttachmentList(strTaskID) end run on AttachmentList(strTaskID) set strCMD to "sqlite3 ~/Library/Caches/com.omnigroup.OmniFocus/OmniFocusDatabase2 'select name from Attachment where task=\"" & strTaskID & "\"'" paragraphs of (do shell script strCMD) end AttachmentList [/CODE] |
Wow, you drill into the SQLite database directly! That’s awesome, thanks!
Is this documented anywhere, perchance? |
OK, now I’ve got a note containing File Attachments that I can save, and an sqlite table containing the filenames of the attachments, but I can’t figure out how to pair them up. Which filename goes with which File Attachment?
Any tips? It would be easy if the File Attachment object had a persistentIdentifier property or something that showed up in the sqlite table. |
[I]name[/I] is only one of the fields in the table.
[INDENT]persistentIdentifier text, containingTransactionHint text, context text, creationOrdinal integer, dateAdded timestamp NOT NULL, dateModified timestamp NOT NULL, folder text, name text, perspective text, previewPNGData blob, task text[/INDENT] The OmniFocus.ofocus package contains various zip files. The name of the zip file containing the attachment is given by the field [I]containingTransactionHint[/I]. If the relevant file is unzipped, you will find that it contains a folder named [I]data[/I], containing subfolders with unique names. The [I]persistentIdentifier[/I] fields gives the name of the subfolder containing the file specified in the record. [COLOR="White"]--[/COLOR] |
[QUOTE=RobTrew;95104][I]name[/I] is only one of the fields in the table…[/QUOTE]
Thanks for the added detail! I’m still stumped on how to match up the File Attachments visible to my AppleScript to the file names present in the Attachments table, however. The Support Ninjas tell me that[code]get file name of aFileAttachment[/code] should work. I’ve reported the fact that it doesn’t work with embedded attachments as a bug. |
[QUOTE=Kevin Yank;95107]I’m still stumped on how to match up the File Attachments visible to my AppleScript to the file names present in the Attachments table[/QUOTE]
Do you need to ? I would have thought that all you need from the applescript is the id of the task/project. From there the information in the cache will take you straight to the files ... |
Ah, I see what you mean! Ignore the AppleScript File Attachment objects entirely and rely on the sqlite database to enumerate and locate the file attachments.
I could see that working, but I’m trying to write an AppleScript that I can invoke to send the selected task(s) (and their attachments) to another application. Unzipping the transaction file for each task to access the attachments seems like it would be a slow and inefficient process for what I’m trying to achieve. I’ll wait and see what the Ninjas say about the apparent bug in their AppleScript support for attachments. Failing that, I’ll take another look at drilling into that database. Thanks again! |
All times are GMT -8. The time now is 09:42 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.