OmniFocus File Attachment Permissions

I am attempting to get a file added to an OmniFocus task and am receiving the following error when attempting to embed the file. If I do not embed the file there is no issue.

“The file “August Survey.pdf” couldn’t be opened because you don’t have permission to view it. (257)”

Any help would be greatly appreciated.

Thank you,
Mike

tell application "OmniFocus"
   tell front document
       set NewTask to make new inbox task with properties {name:"Test"}
       tell the note of NewTask
           make new file attachment with properties {file name:"Users:Mike:Library:Mobile Documents:com~apple~CloudDocs:OmniFocus:August Survey.pdf", embedded:false} ¬
               -- Works
           --make new file attachment with properties {file name:"Users:Mike:Library:Mobile Documents:com~apple~CloudDocs:OmniFocus:August Survey.pdf", embedded:true} ¬
           -- Fails with this error... ¬
           -- The file “August Survey.pdf” couldn’t be opened because you don’t have permission to view it. (257)
       end tell
   end tell
end tell

Assuming you can actually view that file directly and the error message is a little off the mark, does it work if…

  1. You use an alternative file in that folder?
  2. You use the same file in an alternative folder (e.g. Desktop)?
  3. You use an alternative file in an alternative folder (e.g. Desktop)?

Thank you sylumer.

Yes, I am able to view / open / modify the file.

  1. No, it does not work attempting a different file, I have the same issue with all / any files.
  2. No, it does not work attempting a different folder, I tried running the same with the file residing on the desktop as you suggested.
  3. No, it does not work attempting a different file and/or folder.

So strange, the script runs just fine not embedding the file but I would like to have a copy of the file within the task for when I am traveling and do not have internet access.

Could you put it between triple back ticks to format it? As it stands, it’s quite long and poorly formatted.

E.g.

``` 
tell application “foo”
    do bar
end tell
``` 

gives

tell application “foo”
    do bar
end tell
1 Like

Thank you!!!

--	Created by: Mike Weir
--	Created on: 2/18/19
--	Copyright © 2019 NWTDOT, All Rights Reserved

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--using terms from application "Mail"
--on perform mail action with messages messageList for rule aRule
--https://macscripter.net/viewtopic.php?id=43790%2019/01/27
my Mail_to_OmniFocus()
--end perform mail action with messages
--end using terms from
on Mail_to_OmniFocus()
	
	--set attachmentsFolder to (path to desktop folder) as text
	--	set attachmentsFolder to "/Users/Mike/Library/Mobile Documents/com~apple~CloudDocs/OmniFocus/" as text
	
	set attachmentsFolder to "/Users/Mike/OmniFocus/" as text
	
	
	tell application "Mail"
		activate
		set theMessages to (messages of first message viewer whose flagged status is true)
	end tell
	
	---------------------------------------
	repeat with eachMessage in theMessages
		# Open a mail and treat it
		tell application "Mail"
			open eachMessage
			delay 2
			set wName to name of window 1
			
			tell eachMessage # extract datas from the selected message
				set mail_Subject to subject
				set mail_Subject to my replace(mail_Subject, {"Re: ", "Fwd: "}, "") # I know, I'm a bit paranoid
				set mail_body to content
				
			end tell # the message
		end tell # application Mail
		
		set thePDFname to my replace(mail_Subject, {":", "/"}, "-") & ".pdf" # I know, I'm a bit paranoid
		set posixFolderPath to POSIX path of attachmentsFolder
		set pdfPath to POSIX path of (posixFolderPath & thePDFname)
		-- set pdfPath to (a reference to file pdfPath)
		
		
		-- Print the opened email as PDF
		tell application "System Events"
			tell process "Mail"
				set frontmost to true
				set windowName to name of window 1
				keystroke "p" using {command down} # Issue the Print shortcut
				tell window wName
					repeat
						try
							if (exists pop up button 1 of sheet 1) then exit repeat
						end try
						delay 2
					end repeat
					
					tell sheet 1
						set PDFButton to first menu button
						click PDFButton
						tell menu 1 of PDFButton
							name of menu items
							click menu item 2
						end tell
						repeat
							if exists sheet 1 then exit repeat # the Print sheet exists, exit the loop!
							delay 2
						end repeat
					end tell # sheet 1
					
					tell sheet 1 of sheet 1
						set wichElements to class of UI elements
						if wichElements contains combo box then # maybe Mojave
							get position of combo boxes --> {{910, 118}, {910, 148}}
							set value of combo box 1 to thePDFname
						else -- Yosemite, El Capitan, Sierra, High Sierra
							get position of text fields --> {{1262, 194}, {1262, 224}, {1458, 263}, {1086, 646}, {1086, 616}, {1086, 552}, {1086, 584}}
							set value of text field 1 to thePDFname
						end if
					end tell # sheet 1 of sheet 1 
					
					keystroke "g" using {command down, shift down}
					repeat until exists sheet 1 of sheet 1 of sheet 1
						delay 2
					end repeat
					tell sheet 1 of sheet 1 of sheet 1
						--name of UI elements
						set wichElements to class of UI elements # According to system in use, may be
						if wichElements contains combo box then
							set SaveIndex to -1
							set value of combo box 1 to posixFolderPath
						else
							set SaveIndex to 1
							set value of text item 1 to posixFolderPath
						end if
						name of buttons --> {"Aller", "Annuler"}
						click button 1
					end tell # sheet 1 of sheet 1 of sheet 1 
					
					tell sheet 1 of sheet 1
						click button SaveIndex
					end tell # sheet 1 of sheet 1 
				end tell -- window wName
			end tell # process Mail
		end tell # "System Events"
		
		# Now speak again to application Mail
		tell application "Mail"
			try
				# Do that only if the message has attacments
				set num to 1
				repeat with theAttachment in eachMessage's mail attachments
					set thePDFname to rich text 1 thru ((offset of "." in thePDFname) - 1) of thePDFname
					set fileName to thePDFname & " - " & theAttachment's name
					set startPosition to offset of "." in fileName
					set endingPosition to length of fileName
					set theExt to rich text startPosition thru endingPosition of fileName
					set fileName to thePDFname & " (Att. 0" & num & ")" & theExt
					save theAttachment in file (attachmentsFolder & my replace(fileName, {":", "/"}, "-")) # I know, I'm a bit paranoid
					set num to num + 1
				end repeat
			end try
			try
				repeat while exists window wName
					close window wName
					delay 2
				end repeat
			end try
			--Get the Message ID for use in OmniFocus
			set Message_ID to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & (message id of eachMessage)
			set Message_ID to "message://%3C" & (Message_ID) & "%3E"
			log Message_ID
			--Remove the flag
			set flagged status of eachMessage to false
			--Move the Message to the "OmniFocus" Email Folder
			set mailbox of eachMessage to mailbox "OmniFocus" of account "Smith Group"
			delay 2
		end tell # application Mail
		
		log pdfPath
		
		tell application "OmniFocus"
			tell front document
				set NewTask to make new inbox task with properties {name:mail_Subject}
				tell the note of NewTask
					make new file attachment with properties {file name:pdfPath, embedded:true} -- works
					--make new file attachment with properties {file name:pdfPath, embedded:true} -- fails with this error... "Error: The file “August Survey.pdf” couldn’t be opened because you don’t have permission to view it. (257)"
					make new paragraph at beginning of paragraph 1 with data "Original Email" & return & return --& mail_body & return & return & return & return & return
					set value of attribute "link" of style of paragraph 1 of note of NewTask to Message_ID
				end tell
			end tell
		end tell
	end repeat # with eachMessage…
end Mail_to_OmniFocus

(*
replace every occurences of d1 by d2 in text t
*)
on replace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end replace

I’ve had a scan through and don’t see any obvious, but I don’t use Omnifocus or Mail, so it’s not something I can personally try running.

You’ve obviously got an extensive script there, so I think as my first step I’d be trying to isolate a basic example baseline, probably with a hard coded path in the script, and something about the same size as the excerpt below perhaps :wink:.

I’d take creating a task and adding an attachment both as embedded and non-embedded (which I believe is a link). I’d try this using a simple file (perhaps a small image file), from your home/user folder (as you have to have full permissions there), and do it for both embed and not-embed options, via both Applescript and manually (option key and drag in for embedding I believe).

See if you get consistent results of automation vs. manual. Often when carrying things out manually, you get a more descriptive error message.

If manual vs. automated is consistent hopefully it’ll yield some extra info. If not, I’d be raising this as a support query with the OmniGroup.


FWIW, in the excerpt below from your script above, I think that you’ve got an inconsistency between line 5 and line 6 (commented) as they appear identical in terms of code; therefore not matching your initial statement in your original post … and the comments on those lines are mutually exclusive.

		tell application "OmniFocus"
			tell front document
				set NewTask to make new inbox task with properties {name:mail_Subject}
				tell the note of NewTask
					make new file attachment with properties {file name:pdfPath, embedded:true} -- works
					--make new file attachment with properties {file name:pdfPath, embedded:true} -- fails with this error... "Error: The file “August Survey.pdf” couldn’t be opened because you don’t have permission to view it. (257)"
					make new paragraph at beginning of paragraph 1 with data "Original Email" & return & return --& mail_body & return & return & return & return & return
					set value of attribute "link" of style of paragraph 1 of note of NewTask to Message_ID
				end tell
			end tell
		end tell
	end repeat # with eachMessage…
end Mail_to_OmniFocus

Hope that helps at least a little bit.