Thursday, March 30, 2017

#1 2017-03-11 05:03:57 pm

codingnoob
Member
Registered: 2017-03-11
Posts: 2

Add text without overwriting

I just need a script to copy a email into a text document. so far so easy.
But now I want, that every email who is received next is added to the text document. I got the Mail-rule thing but I just don´t now how to NOT overwrite. I also tried many versions of my little script but I can´t get a solution.


Applescript:


using terms from application "Mail"
   on perform mail action with messages theMessages
       
       tell application "Mail"
           repeat with theMessage in theMessages
               set theText to content of theMessage
               set theFile to (POSIX file "Macintosh HD:Users:*****:Desktop:voting:vote-1.txt")
               set theFile to theFile as string
               open for access theFile
               set fileContents to (read theFile)
               write theText & fileContents to theFile starting at eof
               close access theFile
           end repeat
       end tell
   end perform mail action with messages
end using terms from
tell application "Mail"
   set myMessages to selection
   tell me to perform mail action with messages myMessages
end tell

And sorry for this bad formatting and stuff skills - i´m completly a noob ;D
Hope somebody can help me...

Last edited by codingnoob (2017-03-11 05:10:32 pm)


Filed under: , text, applescript, overwrite

Offline

 

#2 2017-03-12 03:50:29 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2885

Re: Add text without overwriting

You may try:

Applescript:


using terms from application "Mail"
   on perform mail action with messages theMessages
       set theFile to my buildPath() # Build it only once
       tell application "Mail"
           repeat with theMessage in theMessages
               set theText to content of theMessage
               try
                   set fileRef to open for access file theFile with write permission
                   write theText to fileRef starting at eof
                   close access fileRef
               on error
                   close access file theFile
               end try
           end repeat
       end tell
   end perform mail action with messages
end using terms from

on buildPath()
   set p2d to path to desktop
   set votingFolder to "voting"
   set targetFolder to (p2d as text) & votingFolder & ":"
   tell application "System Events"
       if not (exists folder targetFolder) then
           make new folder at end of p2d with properties {name:votingFolder}
       end if
   end tell
   set theFile to (targetFolder & "vote-1.txt") # it's a string
   
   return theFile
end buildPath


tell application "Mail"
   set myMessages to selection
   tell me to perform mail action with messages myMessages
end tell

I moved the instruction defining the path of the file out of block speaking to Mail because when it's in such a block it compiles as :

Applescript:

set theFile to (path to desktop rule type rich text) & "voting:vote-1.txt" # it's a string

which fails if we have to re-compile the code

CAUTION. The script above stores theText at the end of the existing file.


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) dimanche 12 mars 2017 09:49:51

Offline

 

#3 2017-03-12 09:13:59 am

codingnoob
Member
Registered: 2017-03-11
Posts: 2

Re: Add text without overwriting

You are my HERO!!!!

Thanks alot!!!!

Man I´m so Happy you made my Week!

Great people on this world ;D

Offline

 

#4 2017-03-12 09:32:48 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2885

Re: Add text without overwriting

Thanks for the feedback.

Just several years of practice wink


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) dimanche 12 mars 2017 15:32:43

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.041 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)