Created
March 2, 2014 21:09
-
-
Save nealtz/9313957 to your computer and use it in GitHub Desktop.
Apple Mail App: Save and print attachment or email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Apple Mail: Save selected E-Mail attachment and print it | |
-- Created by Boy Nils Schulte am Hülse: nsah.de (02.03.2014) | |
-- Based on "Save Mail as PDF and it's attachments to folder" Created by hubionmac (29.09.2010): http://hubionmac.com/wordpress/archives/2087 | |
global frontmost_message_viewer | |
--this is the posix (unix) path of the folder you would like to store the messages in | |
set mymailboxpath to (path to documents folder) & "Buchfuehrung:2013 Buchfuehrung:2013 Kreditoren NSAH:" as text | |
set loopCounter to 0 | |
tell application "Mail" | |
set myselection to my check_message_viewer_and_return_selection() | |
repeat with currentMail in myselection | |
set loopCounter to loopCounter + 1 -- Workaround for Gmail E-Mails with multible Labels | |
if loopCounter = 1 then | |
open currentMail | |
set currentSender to my (getEmail(sender of currentMail)) | |
set currentDateSent to my getDatestring(date sent of currentMail) | |
set currentSubject to my replace_chars(my replace_chars(my replace_chars(my replace_chars(my replace_chars(my replace_chars(subject of currentMail, ":", "_"), "/", "_"), " ", "_"), "(", ""), ")", ""), ".", "") --Doppelpunkte kommen bei Dateinamen nicht so gut | |
set currentFolder2Store to mymailboxpath | |
-- Check if there are attachments and process them | |
set attachCount to count of (mail attachments of currentMail) | |
if attachCount is not equal to 0 then | |
repeat with a in (every mail attachment of currentMail) | |
set current_a_name to name of a | |
set current_a_name to my checkname_with_pdf_suffix(current_a_name, currentFolder2Store, false) | |
if current_a_name contains "pdf" then -- Only for PDF attachments | |
set current_a_name to currentDateSent & "_" & current_a_name | |
display dialog "Name der Datei:" default answer current_a_name with icon note | |
set current_a_name to text returned of the result | |
save a in (currentFolder2Store & current_a_name as rich text) | |
-- Print: commands must likly be changed for the used computer | |
do shell script "lp -d Brother_MFC_J5910DW -o PageSize=A4 -o fit-to-page -o BRMonoColor=Mono " & quoted form of POSIX path of (currentFolder2Store & current_a_name as rich text) | |
end if | |
end repeat | |
else -- Save email as PDF and print it | |
set desktop_pdf_name to my checkname_with_pdf_suffix("1.pdf", path to desktop, false) | |
set the clipboard to desktop_pdf_name | |
tell application "System Events" | |
tell process "Mail" | |
set wc to count of every window | |
end tell | |
end tell | |
my print_current_mail_as_pdf() | |
tell application "System Events" | |
tell process "Mail" | |
repeat until (count of every window) is wc | |
end repeat | |
end tell | |
end tell | |
-- Abfrage zur Bennenung der PDF-Datei | |
if currentSender contains "partner-abrechnung@amazon.de" then | |
set pdfDateiName to currentDateSent & "_Amazon_Gutschrift_" & (rich text 57 through end of currentSubject) & ".pdf" | |
else | |
set pdfDateiName to currentDateSent & "_" & currentSender & "_" & currentSubject | |
display dialog "Name der Datei:" default answer pdfDateiName with icon note | |
set pdfDateiName to text returned of the result & ".pdf" | |
end if | |
my move_desktop_pdf(desktop_pdf_name, currentFolder2Store, pdfDateiName) | |
end if | |
--close last_message window | |
activate | |
tell application "System Events" | |
tell process "Mail" | |
keystroke "w" using command down | |
end tell | |
end tell | |
--open destination folder in finder ( did it really work? YES!! =)) | |
--do shell script "open " & quoted form of currentFolder2Store | |
end if | |
end repeat | |
end tell | |
to move_desktop_pdf(desktop_pdf_name, currentFolder2Store, pdfDateiName) | |
--used to move the printed pdf to it's final destination | |
set finalname to checkname_with_pdf_suffix("__message.pdf", currentFolder2Store, false) | |
try | |
do shell script "mv " & quoted form of (POSIX path of (path to "desk") & desktop_pdf_name) & " " & quoted form of (POSIX path of currentFolder2Store & pdfDateiName) | |
on error msg | |
error "Fehler beim Bewegen der gedruckten Nachricht: " & msg as text | |
end try | |
end move_desktop_pdf | |
to getEmail(mailstring) | |
-- if an email contains the senders name like "Mr.Bla <bla@bla.com>" then returns just the email not leaves the name | |
if mailstring contains "<" then | |
return (characters ((offset of "<" in mailstring) + 1) through ((offset of ">" in mailstring) - 1) of mailstring) as text | |
else | |
return mailstring | |
end if | |
end getEmail | |
to getDatestring(thedate) | |
--format a date to a string like 2010-03-22 | |
set monthnum to characters -2 through -1 of ("0" & ((month of thedate) as integer)) as text | |
set daynum to characters -2 through -1 of ("0" & ((day of thedate) as integer)) as text | |
set yearnum to year of the thedate | |
return yearnum & "-" & monthnum & "-" & daynum as text | |
end getDatestring | |
to create_messagefolder(thepath) | |
--I love mkdir -p, simple, short, easy to use | |
try | |
do shell script "mkdir -p " & quoted form of POSIX path of thepath | |
on error msg | |
error msg | |
end try | |
end create_messagefolder | |
to replace_chars(this_text, search_string, replacement_string) | |
--this replaces characters | |
--used for folder and filenames, since a : must not be used for that | |
if this_text contains the search_string then | |
set AppleScript's text item delimiters to the search_string | |
set the item_list to every text item of this_text | |
set AppleScript's text item delimiters to the replacement_string | |
set this_text to the item_list as string | |
set AppleScript's text item delimiters to "" | |
end if | |
return this_text | |
end replace_chars | |
to checkname_with_pdf_suffix(n, D, looped) | |
--check if filename exists in D | |
-- so if "A File.pdf" exists it names it "A File 1.pdf","A File 2.pdf",... | |
tell application "Finder" | |
set thefiles to name of every item of (D as alias) | |
end tell | |
if thefiles contains n then | |
if looped = false then | |
set n to ((characters 1 through -5 of n) & " 1" & (characters -4 through -1 of n)) as text | |
my checkname_with_pdf_suffix(n, D, true) | |
else | |
set tmp to (last word of ((characters 1 through -5 of n) as text) as integer) | |
set tmpcount to (count of characters of (tmp as text)) + 5 | |
set tmp to tmp + 1 | |
set n to ((characters 1 through (-1 * tmpcount) of n) & tmp & (characters -4 through -1 of n)) as text | |
my checkname_with_pdf_suffix(n, D, true) | |
end if | |
else | |
return n | |
end if | |
end checkname_with_pdf_suffix | |
to print_current_mail_as_pdf() | |
--hopefully works on every mac in every language =) | |
-- GUI-Scripting is not the best way, but somehow the only way at the moment :-/ | |
tell application "Mail" | |
activate | |
tell application "System Events" | |
tell process "Mail" | |
keystroke "p" using command down | |
set p to "false" | |
repeat until 0 = 1 | |
if (count of every sheet of window 1) > 0 then | |
set p to "ready" | |
exit repeat | |
end if | |
end repeat | |
if p = "ready" then | |
click menu button 1 of sheet 1 of window 1 | |
delay 0.25 | |
key code 125 | |
key code 125 | |
delay 0.25 | |
set cwc to count of every window | |
keystroke return | |
repeat until 1 = 0 | |
if (cwc + 1) = (count of every window) then | |
exit repeat | |
end if | |
end repeat | |
click text field 1 of window 1 | |
keystroke "a" using command down | |
keystroke "v" using command down | |
keystroke "d" using command down | |
keystroke return | |
else | |
error "timeout" | |
end if | |
end tell | |
end tell | |
end tell | |
end print_current_mail_as_pdf | |
to check_message_viewer_and_return_selection() | |
-- check if frontmost window is a message viewer, otherwhise tell the user to RTFM!... wait there is no manual... don't care error change user! | |
tell application "Mail" | |
set frontmost_message_viewer to {} | |
repeat with i from 1 to count of every message viewer | |
if index of window of message viewer i = 1 then | |
set frontmost_message_viewer to message viewer i | |
exit repeat | |
end if | |
end repeat | |
if frontmost_message_viewer = {} then | |
error "Ist ja gar kein Message Viewer im Vordergrund, so kann ich einfach nicht arbeiten!" | |
else | |
return selection | |
end if | |
end tell | |
end check_message_viewer_and_return_selection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Danke