rccharles

downloading files in apple mail.

Jun 10th, 2019
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2.  
  3. Requirements:
  4.    https://discussions.apple.com/thread/8524215
  5.    
  6. Quick hints
  7. 1) Apple hids the User's library folder.  You will want to make it visible.
  8.   http://osxdaily.com/2013/10/28/show-user-library-folder-os-x-mavericks/
  9. 2) Place this Applescript in this folder with name ShortMailDownload.scpt.  You may not use the App extension.
  10.    "/Users/mac/Library/Application Scripts/com.apple.mail"    
  11. 2) The first "on" statement needs to be
  12.    on perform mail action with messages theMessages
  13. 3) Mail > Preferences... > Rules > Add Rule
  14.     "Perform the following actions"   [ buttons should be ]
  15.     "Run AppleScript" "ShortMailDownload"
  16. 4) When saving you may get a warning message that some other app, Mail, has made changes to the script. When ever the script is run, the script changes a global variable which modifies the script.  Ignore and save any way.
  17.  
  18. Limitation: Yosemite, 10.10.5, Mail Version 8.2 (2104) only supports extracting one attachment. No problem in High Sierra, 10.13.6.
  19.  
  20. off topic.  Clicking on menu item:
  21. tell application "System Events" to click menu item "Date" of menu "Sort By" of menu item "Sort By" of menu "View" of menu bar item "View" of menu bar 1 of process "Mail"
  22.  
  23. Based on the example " Sample Rule Action Script.scpt" script in macOS 10.6.8 found in folder
  24. "/Library/Scripts /Library/Scripts/Mail Scripts/Rule Actions"
  25.   Sample Rule Action Script.scpt
  26.  
  27. I think "on perform mail action" needs to be the first "on" routine.
  28.  
  29. -- If run as an ordinary script, instead of directly from the Scripts
  30.     -- menu, it will call the default handler instead.
  31.     on r u n
  32.         tell application "Mail" to set selectedMessages to selection
  33.         tell me to perform mail action with messages (selectedMessages)
  34.     end r u n
  35.     http://hints.macworld.com/article.php?story=20070215145127300
  36.  
  37. There is lots of outdated advice on download attachments found by Google.
  38. https://stackoverflow.com/questions/39882312/how-to-download-an-attachment-from-mail-via-applescript-and-mail-rules
  39.  
  40.  
  41.        Copyright 2018 rccharles  
  42.      
  43.        Permission is hereby granted, free of charge, to any person obtaining a copy  
  44.        of this software and associated documentation files (the "Software"), to deal  
  45.        in the Software without restriction, including without limitation the rights  
  46.        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  47.        copies of the Software, and to permit persons to whom the Software is  
  48.        furnished to do so, subject to the following conditions:  
  49.        
  50.        The above copyright notice and this permission notice shall be included in all  
  51.        copies or substantial portions of the Software.  
  52.        
  53.        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
  54.        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
  55.        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
  56.        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
  57.        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
  58.        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  
  59.        SOFTWARE.  
  60.  
  61. *)
  62. (*  Uncomment when you want to debug this script in the Script Editor. Go into Mail and select a message. Run this script. *)
  63. on run
  64.     tell application "Mail"
  65.         try
  66.             set theSelectedMessages to selection
  67.             log "class of theSelectedMessages is " & class of theSelectedMessages
  68.             log "count of theSelectedMessages is " & (count of theSelectedMessages)
  69.         on error errMsg number n
  70.             display dialog "Silly, you need to select a message in Mail." & return & "on error the errMsg is " & errMsg & " number is " & n giving up after 8
  71.             return
  72.         end try
  73.  
  74.         my processMessages(theSelectedMessages, "buster")
  75.        
  76.     end tell -- app "Mail"
  77. end run
  78.  
  79. (**)
  80. using terms from application "Mail"
  81.     on processMessages(theMessages, theAccount)
  82.         -- Required by debug routine.  
  83.         global debugRunning
  84.         set debugRunning to ""
  85.        
  86.         -- debug status for displaying messages in this routine.
  87.         set debugSwitch to true
  88.         log "debugSwitch is " & debugSwitch
  89.         -- initialize
  90.         set longWait to 20
  91.        
  92.        
  93.         set startMsg to "perform mail with " & getMyName() & " ====== " & ((current date) as string) & " ====== perform mail "
  94.         log startMsg)
  95.         --set inboxes to mailbox "INBOX" of account theAccount
  96.         --set inbox to mailbox "Inbox" of account theAccount
  97.         set outbox to mailbox "Junk" of account theAccount
  98.        
  99.         if (count of theMessages) = 0 then
  100.             log "odd no messages to process."
  101.             display dialog "Odd, no messages to process." giving up after longWait
  102.             return 1
  103.         end if
  104.        
  105.        
  106.        
  107.        
  108.         tell application "Mail"
  109.             --activate
  110.             log "was activated"
  111.            
  112.             -- look throught the messages we selected.
  113.             repeat with aMessage in theMessages -- loop through each message
  114.                 log "class of aMessage is " & class of aMessage
  115.                
  116.                 -- get the senders email address
  117.                 set fromMail to aMessage's sender
  118.                 log "fromMail is " & fromMail
  119.                
  120.                 -- Create folder for the individual student.
  121.                 -- ---- Set final target folder ---->
  122.                 set pathToStudents to pathToHome & finalDestination & ":"
  123.                 log "pathToStudents is " & pathToStudents
  124.                 repeat 1 times -- sinulate continue
  125.                    
  126.                     (* move v : Move object(s) to a new location.
  127.                                      move specifier : the object for the command
  128.                                      to location specifier : The new location for the object(s). *)
  129.                    
  130.                     try
  131.                        
  132.                         move aMessage to outbox
  133.                     on error errMsg number n
  134.                         -- It's ok if the folder already exits [-48 ]. Put out warning for all other errors.
  135.                         set commonError to "attempting to move " & " on error the errMsg is " & errMsg & " number is " & n
  136.                         display dialog commonError giving up after longWait
  137.                         log commonError
  138.                         exit repeat -- look at next message. simulate iterate here.
  139.                        
  140.                     end try
  141.                    
  142.                    
  143.                 end repeat -- continue
  144.             end repeat -- next message
  145.         end tell --tell app "Mail"
  146.         return 0
  147.     end processMessages
  148. end using terms from
  149.  
  150.  
  151. (* ======================== Subroutines ======================= *)
  152. -- ------------------------------------------------------  
  153. (*
  154. *)
  155. on appendToFile(fileId, theData)
  156.    
  157.     local theSize, writeWhere
  158.    
  159.     set theSize to (get eof fileId)
  160.     set writeWhere to theSize + 1 as integer
  161.     write theData to fileId starting at writeWhere
  162.    
  163. end appendToFile
  164.  
  165. -- ------------------------------------------------------  
  166. (*
  167.  debug(<string>)
  168.  Write messages to a log file.
  169.  
  170.   -- Need to place these two lines in the calling routine.
  171.     global debugRunning
  172.     set debugRunning to ""
  173.  -- references appendToFile()  
  174.   -- example:
  175.     debug("start program. Reading from " & listOfFiles)
  176.    
  177.    found here: /Users/mac/Documents/BJ\ Prior\ Years/BJ2004/sendmailapp2\ copy
  178.  
  179. *)
  180. on debug(theMessage)
  181.     -- return
  182.     global debugRunning
  183.     local theSize, startupDiskName, pathToLog, fileReference
  184.    
  185.     set pathToLog to (path to home folder as text) & "tryAttachmentsLog.txt"
  186.     -- log "pathToLog is " & pathToLog
  187.     -- display dialog "pathToLog is " & pathToLog giving up after 4
  188.    
  189.     try
  190.         -- Complete the path.
  191.         set pathToLog to pathToLog as text
  192.         set fileReference to (open for access file pathToLog ¬
  193.             with write permission)
  194.        
  195.         if debugRunning = "" then
  196.             set theSize to (get eof fileReference)
  197.             if theSize > 0 then
  198.                 appendToFile(fileReference, " " & return)
  199.             end if
  200.             appendToFile(fileReference, "   --- debug on " & ((current date) as string) & "   --- " & return)
  201.             set debugRunning to "running"
  202.         end if
  203.         -- log "theMessage " & theMessage
  204.         -- display dialog "in debug..." & return & "theMessage " & theMessage giving up after 3
  205.         appendToFile(fileReference, theMessage & return)
  206.        
  207.         close access fileReference
  208.         tell application "Finder"
  209.             set the creator type of the file pathToLog ¬
  210.                 to "R*ch"
  211.         end tell
  212.     on error mes number n
  213.         try
  214.             set commonErr to "error ... " & mes & " error number is " & n
  215.             log commonErr
  216.             close access fileReference
  217.             display dialog commonErr giving up after 4
  218.         end try
  219.        
  220.     end try
  221.     -- log "end of debug"
  222. end debug
  223.  
  224. (*
  225. write log message to script editor log and to our file log
  226. *)
  227. on debugLog(theMessage)
  228.     log "debugLog: " & theMessage
  229.     return debug(theMessage)
  230. end debugLog
  231.  
  232. -- ------------------------------------------------------  
  233. (*
  234.   ideas from:
  235.   https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
  236.  
  237.  use the alias way.
  238. *)
  239. on fileExists(theFile) -- (String) as Boolean
  240.     (* "System Events" and "Finder" checking for file existance revealed problems. l*)
  241.     set debugging to false
  242.     if debugging then log "  fileExists: theFile is " & theFile
  243.     try
  244.         set theAlias to theFile as alias
  245.         set theExistance to true
  246.     on error errMsg number n
  247.         if debugging then log "  fileExists: n is " & n
  248.         -- File or folder doesn't exist.
  249.         if n is not -43 then
  250.             set commonError to "on error the errMsg is " & errMsg & " number is " & n
  251.             if debugging then log "  fileExists: " & commonError
  252.             display dialog commonError giving up after 10
  253.             -- cause grief above.
  254.             error "Failure of alias." number -1
  255.         else
  256.             set theExistance to false
  257.         end if
  258.     end try
  259.     if debugging then log "  fileExists: theExistance is " & theExistance
  260.     return theExistance
  261. end fileExists
  262.  
  263. (*
  264.   Philip Regan
  265.   https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
  266. *)
  267. (*on fileExists(theFile) -- (String) as Boolean
  268.     tell application "System Events"
  269.         if exists file theFile then
  270.             return true
  271.         else
  272.             return false
  273.         end if
  274.     end tell
  275. end fileExists*)
  276.  
  277. -- ------------------------------------------------------
  278. (*
  279.    Yvan Koenig
  280.    https://macscripter.net/viewtopic.php?id=43133
  281.    with mods for no extension present
  282.    
  283. *)
  284. on getExt(theName)
  285.     if (offset of "." in theName) is greater than 0 then
  286.         set saveTID to AppleScript's text item delimiters
  287.         set AppleScript's text item delimiters to {"."}
  288.         set theExt to last text item of theName
  289.         set AppleScript's text item delimiters to saveTID
  290.         if theExt ends with ":" then set theExt to text 1 thru -2 of theExt
  291.     else
  292.         set theExt to ""
  293.     end if
  294.     return theExt
  295. end getExt
  296.  
  297. -- ------------------------------------------------------
  298. (*
  299.     Input: a file with or without an extension.
  300.     hhas
  301.     https://forums.macrumors.com/threads/applescript-to-get-file-name.927338/
  302.    
  303.     may not work with folders with extensions like apps.
  304.    
  305.     Test cases:
  306.                 set fileName to " "                  
  307.                 set fileExt to my getExt(attachmentName)
  308.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  309.                 set fileExt to my getExt("testfileNo")
  310.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  311.                 set fileExt to my getExt("path:to:testfileNo:")
  312.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  313.                 log ">>>>>>>>>>>>>>>>>>>>>>"
  314.                 set {fileName, fileExt} to my getNameExt(attachmentName)
  315.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  316.                 set {fileName, fileExt} to my getNameExt("testfileNo")
  317.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  318.                 set {fileName, fileExt} to my getNameExt("path:to:testfileNo:")
  319.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  320. *)
  321. on getNameExt(fileName)
  322.     set saveTID to AppleScript's text item delimiters
  323.     set AppleScript's text item delimiters to "."
  324.     if fileName contains "." then
  325.         set {displayName, nameExt} to {text 1 thru text item -2, text item -1} of fileName
  326.     else
  327.         set {displayName, nameExt} to {fileName, ""}
  328.     end if
  329.     set AppleScript's text item delimiters to saveTID
  330.    
  331.     return {displayName, nameExt}
  332. end getNameExt
  333. -- ------------------------------------------------------
  334. (*
  335.     modified to let the extension be.
  336.  
  337.     by mklement0
  338.     https://stackoverflow.com/questions/5770384/how-find-the-file-name-of-an-executing-applescript
  339. *)
  340. on getMyName()
  341.     local myAlias, myName
  342.     tell application "System Events"
  343.         set myAlias to path to me -- alias to the file/bundle of the running script
  344.         set myName to name of myAlias -- filename with extension, if any.
  345.         -- leave extension alone.
  346.     end tell
  347.     return myName
  348. end getMyName
  349.  
  350. -- ------------------------------------------------------
  351. (*
  352.    ls_l is list file with options  
  353.   the format is best for debuging.
  354.  
  355.   example usage:
  356.   set {fileExists, fromUnix} to ls_l(attachmentNamePath, "-l")
  357.   log "attachmentNamePath fileExists is " & fileExists & return & " fromUnix is " & fromUnix
  358.  
  359.   *)
  360. on ls_l(attachmentNamePath, options)
  361.     --log "ls_l"
  362.     --log options
  363.    
  364.     set unixAttachmentNamePath to POSIX path of attachmentNamePath
  365.     --log "unixDesktopPath = " & unixAttachmentNamePath
  366.    
  367.     set quotedUnixAttachmentNamePath to quoted form of unixAttachmentNamePath
  368.     --log "quoted form is " & quotedUnixAttachmentNamePath
  369.     try
  370.         set fromUnix to do shell script "ls " & options & " " & quotedUnixAttachmentNamePath
  371.         set fromUnix to "ls " & options & return & fromUnix
  372.         set fileExists to true
  373.     on error errMsg number n
  374.         set fromUnix to "ls " & options & "  error..." & errMsg & " with number " & n
  375.         set fileExists to false
  376.        
  377.     end try
  378.     return {fileExists, fromUnix}
  379. end ls_l
  380.  
  381. -- ------------------------------------------------------
  382. (*
  383. textToList seems to be what you are trying to do
  384.   thisText is the input string
  385.   delim is what to split on
  386.  
  387.   returns a list of strings.  
  388.  
  389. - textToList was found here:
  390. - http://macscripter.net/viewtopic.php?id=15423
  391.  
  392. *)
  393.  
  394. on textToList(thisText, delim)
  395.     set resultList to {}
  396.     set {tid, my text item delimiters} to {my text item delimiters, delim}
  397.    
  398.     try
  399.         set resultList to every text item of thisText
  400.         set my text item delimiters to tid
  401.     on error
  402.         set my text item delimiters to tid
  403.     end try
  404.     return resultList
  405. end textToList
Add Comment
Please, Sign In to add comment