Monday, August 19, 2019

#1 2019-06-08 07:13:25 pm

learned
Member
Registered: 2008-03-08
Posts: 10

Receiving Error on Write File

I am trying to create a mail rule which appends the body of a message to a file. Ideally this would be a file in iCloud Mobile Documents but for the purpose of debugging, I am trying to do it just do a file in Downloads.

The script is below and the error comes in the write_to_file routine. Hoping for some clues.

Applescript:


tell application "Mail"
   set theSel to selection -- selected emails will be used for test
end tell
using terms from application "Mail"
   perform mail action with messages theSel in mailboxes (missing value) for rule (missing value)
end using terms from

using terms from application "Mail"
   on perform mail action with messages theMessages
       repeat with aMessage in theMessages
           try
               set theSubject to getMessageSubject(aMessage)
               set theBody to getMessageBody(aMessage)
           end try
       end repeat
       -- set thisFile to "Macintosh HD/Users/learned/Library/Mobile Documents/iCloud~co~noteplan~NotePlan/Documents/Calendar/" & theSubject & ".txt"
       set thisFile to "~/Downloads/" & theSubject & ".txt"
       log thisFile
       log theBody
       my write_to_file(theBody, thisFile, true)
   end perform mail action with messages
end using terms from

on getMessageSubject(aMessage)
   tell application "Mail" to return subject of aMessage
end getMessageSubject

on getMessageBody(aMessage)
   tell application "Mail" to return content of aMessage
end getMessageBody

on write_to_file(this_data, target_file, append_data)
   try
       set the target_file to the target_file as string
       log target_file
       set the open_target_file to open for access file target_file with write permission
       if append_data is false then set eof of the open_target_file to 0
       write this_data to the open_target_file starting at eof
       close access the open_target_file
       return true
   on error
       try
           close access file target_file
       end try
       return false
   end try
end write_to_file


Filed under: file, mail

Offline

 

#2 2019-06-08 07:46:58 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 97

Re: Receiving Error on Write File

It's always helpful if you include the error (verbatim) as well, because the error message is what points you/us to the probable cause.

Skimming your code, however, what jumps out at me is that you've prepended the file specifier to your posix path in this line:

Applescript:

open for access file target_file

so try removing that, and just having:

Applescript:

open for access target_file

Secondly, make sure your posix paths are full paths, and don't use a tilde "~" as a shortcut for the home directory because these will not be expanded by the standard additions commands:

Applescript:

set thisFile to "~/Downloads/" & theSubject & ".txt"

should be:

Applescript:

set thisFile to the POSIX path of (path to downloads folder) & theSubject & ".txt"

Offline

 

#3 2019-06-09 09:58:15 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 424

Re: Receiving Error on Write File

Applescript:


tell application "Mail" to set theSel to selection -- selected emails will be used for test

using terms from application "Mail"
   perform mail action with messages theSel in mailboxes (missing value) for rule (missing value)
end using terms from

using terms from application "Mail"
   on perform mail action with messages theMessages
       repeat with aMessage in theMessages
           try
               tell application "Mail"
                   set theSubject to subject of aMessage
                   set theBody to content of aMessage
               end tell
           end try
       end repeat
       set thisFile to the POSIX path of (path to downloads folder) & theSubject & ".txt"
       log (thisFile as string)
       log theBody
       my write_to_file(theBody, thisFile, true)
   end perform mail action with messages
end using terms from

on write_to_file(this_data, target_file, append_data)
   try
       log (target_file as string)
       set the open_target_file to open for access target_file with write permission
       if append_data is false then set eof of the open_target_file to 0
       write this_data to the open_target_file starting at eof
       close access the open_target_file
       return true
   on error
       try
           close access file target_file
       end try
       return false
   end try
end write_to_file

Last edited by KniazidisR (2019-06-09 10:12:34 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)