The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > OmniFocus > OmniFocus Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Applescript access to attachments ? Thread Tools Search this Thread Display Modes
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

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:
Originally Posted by Tim Wood View Post
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.
Code:
file name of file attachment 1 as string
works but not
Code:
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.
 
Quote:
Originally Posted by Lutin View Post
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:

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
 
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 should 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
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
__________________
CTO, The Omni Group
 
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
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
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

You should contact the Ninjas :)
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access "Wrap To Shape" property from Applescript? Sophie OmniGraffle General 12 2013-01-19 09:50 PM
AppleScript Access to Change Tracking bjmclaughlin OmniPlan Extras 0 2012-03-07 04:55 AM
Applescript access to tasks in inbox dmcg OmniFocus Extras 2 2009-01-09 09:44 AM
Applescript access to natural language date parsing? RobTrew OmniFocus Extras 3 2007-12-27 01:22 PM
Web Access chrjohns OmniFocus 1 for Mac 0 2007-02-19 10:41 AM


All times are GMT -8. The time now is 05:41 AM.


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