Mich hat heute eine Email erreicht, in der mir folgende Aufgabe beschrieben wurde (Ich hoffe mal Udo ist mit der Veröffentlichung einverstanden, hätte ich selber nicht besser formulieren können.):
Ich möchte eine Mail in Mail.app anklicken, danach sollte die Mail als pdf (wie unter Ablage Drucken PDF Als PDF sichern) in einem speziellen Ordner auf dem Schreibtisch mit folgendem Namen (Sendedatum, E-Mail-Adresse und Betreff aus Mail) gesichert werden. Der eventuelle Anhang sollte ebenfalls in diesem Ordner gesichert werden. Die Mail kann danach gelöscht werden.
zusammen mit dieser Aufgabenstellung und den ersten Code-Zeilen von Udo ist dann das hier entstanden (ich mag zwar GUI-Scripting nicht, aber das hier ROCKT ;-) ):
-- Save Mail as PDF and it's attachments to folder
-- Created by hubionmac (22.03.2010) requested by Udo
global frontmost_message_viewer
--this is the posix (unix) path of the folder you would like to store the messages in
tell application "Finder" to set mymailboxpath to POSIX path of ((desktop) as alias) & "mail_box/"
tell application "Mail"
set myselection to my check_message_viewer_and_return_selection()
--works only with one selected message for many reasons...
if (count of myselection) = 1 then
repeat with currentMail in myselection
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(subject of currentMail, ":", "-"), "/", ":") --Doppelpunkte kommen bei Dateinamen nicht so gut
set currentFolder2Store to mymailboxpath & currentDateSent & " " & currentSender & " " & currentSubject & "/" as rich text
my create_messagefolder(currentFolder2Store)
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, (POSIX file currentFolder2Store) as alias, false)
save a in (((POSIX file currentFolder2Store) as rich text) & current_a_name) as rich text
end repeat
set desktop_pdf_name to my checkname_with_pdf_suffix("1.pdf", path to desktop, false)
set the clipboard to desktop_pdf_name
my print_current_mail_as_pdf()
repeat until (index of window of frontmost_message_viewer) is 1
delay 1
end repeat
my move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--open destination folder in finder ( did it really work? YES!! =))
do shell script "open " & quoted form of currentFolder2Store
end repeat
else
error "Sorry, ich kann zur Zeit nur mit einer ausgewählten Email hantieren"
end if
end tell
to move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--used to move the printed pdf to it's final destination
set finalname to checkname_with_pdf_suffix("__message.pdf", (POSIX file currentFolder2Store) as alias, false)
try
tell application "Finder" to do shell script "mv " & POSIX path of ((desktop) as alias) & quoted form of desktop_pdf_name & " " & quoted form of currentFolder2Store & quoted form of finalname
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_posix)
--I love mkdir -p, simple, short, easy to use
try
do shell script "mkdir -p " & quoted form of thepath_posix
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 with i from 1 to 10
if (count of every sheet of window 1) > 0 then
set p to "ready"
exit repeat
end if
delay 1
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
keystroke return
delay 1
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
In habe mal auf das Löschen der Email verzichtet (das soll mal jeder lieber alleine rein schreiben), aber es scheint wunderbar zu funktionieren. Bin mal gespannt ob es auch bei anderen läuft…
Updates vom 29.09.2010 und 27.09.2011:
- Auswahl von mehren Emails wird nun auch möglich
- Abgesendete Emails werden als solche erkannt und in einem anderen Verzeichnis mit Empfänger-Adresse im Ordnernamen gespeichert (req. by Andreas)
- Lokale Email-Ordner funktionieren nun auch mit dem Skript
-- Save Mail as PDF and it's attachments to folder
-- Created by hubionmac (29.09.2010)
-- 29.09.2010
-- -Auswahl von mehren Emails wird nun auch möglich
-- -Abgesendete Emails werden als solche erkannt und in einem anderen Verzeichnis mit Empfänger-Adresse im Ordnernamen gespeichert (req. by Andreas)
-- 27.09.2011 made it work with local mailboxes, too
global frontmost_message_viewer
--this is the posix (unix) path of the folder you would like to store the messages in
tell application "Finder" to set mymailboxpath to POSIX path of ((desktop) as alias) & "received_mail_box/"
tell application "Finder" to set mysentmailboxpath to POSIX path of ((desktop) as alias) & "sent_mail_box/"
tell application "Mail"
set myselection to my check_message_viewer_and_return_selection()
repeat with currentMail in myselection
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(subject of currentMail, ":", "-"), "/", "--") --Doppelpunkte kommen bei Dateinamen nicht so gut
if my is_in_sent_mail(currentMail) is true then
--if the file was sent FROM this email account
set sentToEmail to address of item 1 of to recipient of currentMail
set currentFolder2Store to mysentmailboxpath & sentToEmail & " " & currentDateSent & " " & currentSubject & "/" as rich text
else
--if the email was sent TO this email account
try
set myemail to item 1 of (get email addresses of account of mailbox of currentMail)
on error
--27.09.2011 added to make it work with local mailboxes, too
set myemail to item 1 of (get address of to recipient of currentMail)
end try
set currentFolder2Store to mymailboxpath & currentDateSent & " " & currentSender & " " & currentSubject & " send to" & myemail & "/" as rich text
end if
my create_messagefolder(currentFolder2Store)
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, (POSIX file currentFolder2Store) as alias, false)
save a in (((POSIX file currentFolder2Store) as rich text) & current_a_name) as rich text
end repeat
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
my move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--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 repeat
end tell
to move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--used to move the printed pdf to it's final destination
set finalname to checkname_with_pdf_suffix("__message.pdf", (POSIX file currentFolder2Store) as alias, false)
try
tell application "Finder" to do shell script "mv " & POSIX path of ((desktop) as alias) & quoted form of desktop_pdf_name & " " & quoted form of currentFolder2Store & quoted form of finalname
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_posix)
--I love mkdir -p, simple, short, easy to use
try
do shell script "mkdir -p " & quoted form of thepath_posix
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
on is_in_sent_mail(amessage)
tell application "Mail"
set sentmailboxes to every mailbox of sent mailbox
set currentmailbox to mailbox of amessage
repeat with i from 1 to (count of sentmailboxes)
if item i of sentmailboxes = currentmailbox then
return true
end if
end repeat
return false
end tell
end is_in_sent_mail
Update vom 17.04.2012:
Das Skript funktionierte leider nicht mehr unter 10.7… tja jetzt schon wieder… dazu mussten die Pfade etwas angepasst werden
-- Save Mail as PDF and it's attachments to folder
-- Created by hubionmac (29.09.2010)
-- 29.09.2010
-- -Auswahl von mehren Emails wird nun auch möglich
-- -Abgesendete Emails werden als solche erkannt und in einem anderen Verzeichnis mit Empfänger-Adresse im Ordnernamen gespeichert (req. by Andreas)
-- 27.09.2011 made it work with local mailboxes, too
##2012-04-17 changed the way paths are formated (posix/mac) and now the script runs on 10.7 again =)
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 "desk") & "received_mail_box:" as text
set mysentmailboxpath to (path to "desk") & "sent_mail_box:" as text
tell application "Mail"
set myselection to my check_message_viewer_and_return_selection()
repeat with currentMail in myselection
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(subject of currentMail, ":", "-"), "/", "--") --Doppelpunkte kommen bei Dateinamen nicht so gut
if my is_in_sent_mail(currentMail) is true then
--if the file was sent FROM this email account
set sentToEmail to address of item 1 of to recipient of currentMail
set currentFolder2Store to mysentmailboxpath & sentToEmail & " " & currentDateSent & " " & currentSubject & ":" as rich text
else
--if the email was sent TO this email account
try
set myemail to item 1 of (get email addresses of account of mailbox of currentMail)
on error
--27.09.2011 added to make it work with local mailboxes, too
set myemail to item 1 of (get address of to recipient of currentMail)
end try
set currentFolder2Store to mymailboxpath & currentDateSent & " " & currentSender & " " & currentSubject & " send to " & myemail & ":" as rich text
end if
my create_messagefolder(currentFolder2Store)
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)
display dialog (currentFolder2Store & current_a_name) as rich text
save a in (currentFolder2Store & current_a_name as rich text)
end repeat
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
my move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--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 repeat
end tell
to move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--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 " & POSIX path of (path to "desk") & quoted form of desktop_pdf_name & " " & quoted form of POSIX path of currentFolder2Store & quoted form of finalname
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
on is_in_sent_mail(amessage)
tell application "Mail"
set sentmailboxes to every mailbox of sent mailbox
set currentmailbox to mailbox of amessage
repeat with i from 1 to (count of sentmailboxes)
if item i of sentmailboxes = currentmailbox then
return true
end if
end repeat
return false
end tell
end is_in_sent_mail
Update vom 18.02.2013:
Das Skript funktionierte leider nicht mehr unter 10.8… tja jetzt schon wieder… habe ich schon mal gesagt, dass GUI-Scripting doof ist? Ok, zudem spielte da auch noch Gatekeeper mit rein. Man darf Attachments nur noch im dafür ausgewiesenen Download-Order per Skript speichern. Anschließend darf man diese aber auch wieder verschieben, nervig, aber nicht sicher ;-)
-- Save Mail as PDF and it's attachments to folder
-- Created by hubionmac (29.09.2010)
-- 29.09.2010
-- -Auswahl von mehren Emails wird nun auch möglich
-- -Abgesendete Emails werden als solche erkannt und in einem anderen Verzeichnis mit Empfänger-Adresse im Ordnernamen gespeichert (req. by Andreas)
-- 27.09.2011 made it work with local mailboxes, too
##2012-04-17 changed the way paths are formated (posix/mac) and now the script runs on 10.7 again =)
##2013-02-18 Updated code so that it works using 10.8.3 (new dialogs and new gatekeeper made this necessary)
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 "desk") & "received_mail_box:" as text
set mysentmailboxpath to (path to "desk") & "sent_mail_box:" as text
my checkIfUIElementsAreEnabled()
tell application "Finder"
set os_version to version
end tell
if os_version does not start with "10.8" then
error "This Script was only tested using 10.8… I quit here…"
end if
tell application "Mail"
set downloadFolder to ((path to home folder) as rich text) & "Downloads:"
set myselection to my check_message_viewer_and_return_selection()
repeat with currentMail in myselection
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(subject of currentMail, ":", "-"), "/", "--") --Doppelpunkte kommen bei Dateinamen nicht so gut
if my is_in_sent_mail(currentMail) is true then
--if the file was sent FROM this email account
set sentToEmail to address of item 1 of to recipient of currentMail
set currentFolder2Store to mysentmailboxpath & sentToEmail & " " & currentDateSent & " " & currentSubject & ":" as rich text
else
--if the email was sent TO this email account
try
set myemail to item 1 of (get email addresses of account of mailbox of currentMail)
on error
--27.09.2011 added to make it work with local mailboxes, too
set myemail to item 1 of (get address of to recipient of currentMail)
end try
set currentFolder2Store to mymailboxpath & currentDateSent & " " & currentSender & " " & currentSubject & " send to " & myemail & ":" as rich text
end if
my create_messagefolder(currentFolder2Store)
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)
##10.8 fix… you may only save attachments in the download folder… this does not mean you cannot move it afterwards ;-)
save a in downloadFolder & current_a_name
set thefile to quoted form of POSIX path of (downloadFolder & current_a_name as alias)
set mypath to quoted form of POSIX path of currentFolder2Store
do shell script "mv -f " & thefile & " " & mypath
end repeat
set desktop_pdf_name to my checkname_with_pdf_suffix("1.pdf", path to desktop, false)
tell application "System Events"
tell process "Mail"
set wc to count of every window
end tell
end tell
my print_current_mail_as_pdf(desktop_pdf_name)
tell application "System Events"
tell process "Mail"
repeat until (count of every window) is wc
end repeat
end tell
end tell
my move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--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 repeat
end tell
to move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--used to move the printed pdf to it's final destination
set finalname to checkname_with_pdf_suffix("__message.pdf", currentFolder2Store, false)
set movedIt to false
repeat with i from 1 to 10
try
do shell script "mv " & POSIX path of (path to "desk") & quoted form of desktop_pdf_name & " " & quoted form of POSIX path of currentFolder2Store & quoted form of finalname
set movedIt to true
exit repeat
end try
delay 1
end repeat
if movedIt is false then error "Fehler beim Bewegen der gedruckten Nachricht: " & msg as text
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(pdfname)
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
keystroke return
##wait until second sheet opened
delay 1
set value of text field 1 of sheet 1 of sheet 1 of window 1 to pdfname
keystroke "d" using command down
delay 0.25
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
on is_in_sent_mail(amessage)
##Just Try
try
tell application "Mail"
set sentmailboxes to every mailbox of sent mailbox
set currentmailbox to mailbox of amessage
repeat with i from 1 to (count of sentmailboxes)
if item i of sentmailboxes = currentmailbox then
return true
end if
end repeat
return false
end tell
end try
return false
end is_in_sent_mail
on checkIfUIElementsAreEnabled()
tell application "System Events"
get system attribute "sysv"
if result is greater than or equal to 4144 then -- Mac OS X 10.3.0
if UI elements enabled then
return true
else
beep
aktivate
display dialog "GUI Scripting is not enabled" & return & return & "Open System Preferences and check Enable Access for Assistive Devices in the Accessibility (or Universal Access) preference pane, then run this script again." with icon stop
if button returned of result is "OK" then
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
end tell
end if
end if
else
beep
display dialog "This computer cannot run this script" & return & return & "The script uses GUI Scripting technology, which requires an upgrade to Mac OS X 10.3 Panther or newer." with icon caution buttons {"Quit"} default button "Quit"
end if
end tell
end checkIfUIElementsAreEnabled
Update vom 23.03.2014:
Das Skript funktioniert nun auch unter 10.9.2 (habe nun gewagt selber zu aktualisieren). Dabei hat sich in Mail selbst nichts geändert, aber nun muss jede Anwendung einzeln die Erlaubnis erteilt bekommen GUI-Scriping durchzuführen. Das Script also ab besten über ein richtiges Programm wie FastScripts ausführen, den Dialog abwarten und dann bei FastScripts das Häckchen setzen…
-- Save Mail as PDF and it's attachments to folder
-- Created by hubionmac (29.09.2010)
-- 29.09.2010
-- -Auswahl von mehren Emails wird nun auch möglich
-- -Abgesendete Emails werden als solche erkannt und in einem anderen Verzeichnis mit Empfänger-Adresse im Ordnernamen gespeichert (req. by Andreas)
-- 27.09.2011 made it work with local mailboxes, too
##2012-04-17 changed the way paths are formated (posix/mac) and now the script runs on 10.7 again =)
##2013-02-18 Updated code so that it works using 10.8.3 (new dialogs and new gatekeeper made this necessary)
## 2014-03-23 Updated code so that it works using 10.9.2 The Dialogs have not changed, but handling GUI Scripting made this necessary. Now you will have to enable GUI Access for each single app that whats access... so this script app or the app running this script (e.g. FastScripts)
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 "desk") & "received_mail_box:" as text
set mysentmailboxpath to (path to "desk") & "sent_mail_box:" as text
my enabledGUIScripting(true)
tell application "Finder"
set os_version to version
end tell
if os_version does not start with "10.9" then
error "This Script was only tested using 10.9… I quit here…"
end if
tell application "Mail"
set downloadFolder to ((path to home folder) as rich text) & "Downloads:"
set myselection to my check_message_viewer_and_return_selection()
repeat with currentMail in myselection
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(subject of currentMail, ":", "-"), "/", "--") --Doppelpunkte kommen bei Dateinamen nicht so gut
if my is_in_sent_mail(currentMail) is true then
--if the file was sent FROM this email account
set sentToEmail to address of item 1 of to recipient of currentMail
set currentFolder2Store to mysentmailboxpath & sentToEmail & " " & currentDateSent & " " & currentSubject & ":" as rich text
else
--if the email was sent TO this email account
try
set myemail to item 1 of (get email addresses of account of mailbox of currentMail)
on error
--27.09.2011 added to make it work with local mailboxes, too
set myemail to item 1 of (get address of to recipient of currentMail)
end try
set currentFolder2Store to mymailboxpath & currentDateSent & " " & currentSender & " " & currentSubject & " send to " & myemail & ":" as rich text
end if
my create_messagefolder(currentFolder2Store)
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)
##10.8 fix… you may only save attachments in the download folder… this does not mean you cannot move it afterwards ;-)
save a in downloadFolder & current_a_name
set thefile to quoted form of POSIX path of (downloadFolder & current_a_name as alias)
set mypath to quoted form of POSIX path of currentFolder2Store
do shell script "mv -f " & thefile & " " & mypath
end repeat
set desktop_pdf_name to my checkname_with_pdf_suffix("1.pdf", path to desktop, false)
tell application "System Events"
tell process "Mail"
set wc to count of every window
end tell
end tell
my print_current_mail_as_pdf(desktop_pdf_name)
tell application "System Events"
tell process "Mail"
repeat until (count of every window) is wc
end repeat
end tell
end tell
my move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--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 repeat
end tell
to move_desktop_pdf(desktop_pdf_name, currentFolder2Store)
--used to move the printed pdf to it's final destination
set finalname to checkname_with_pdf_suffix("__message.pdf", currentFolder2Store, false)
set movedIt to false
repeat with i from 1 to 10
try
do shell script "mv " & POSIX path of (path to "desk") & quoted form of desktop_pdf_name & " " & quoted form of POSIX path of currentFolder2Store & quoted form of finalname
set movedIt to true
exit repeat
end try
delay 1
end repeat
if movedIt is false then error "Fehler beim Bewegen der gedruckten Nachricht: " & msg as text
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(pdfname)
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
keystroke return
##wait until second sheet opened
delay 1
set value of text field 1 of sheet 1 of sheet 1 of window 1 to pdfname
keystroke "d" using command down
delay 0.25
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
on is_in_sent_mail(amessage)
##Just Try
try
tell application "Mail"
set sentmailboxes to every mailbox of sent mailbox
set currentmailbox to mailbox of amessage
repeat with i from 1 to (count of sentmailboxes)
if item i of sentmailboxes = currentmailbox then
return true
end if
end repeat
return false
end tell
end try
return false
end is_in_sent_mail
on enabledGUIScripting(flag)
get system attribute "sysv"
if result is less than 4240 then -- 4240 is OS X 10.9.0 Mavericks
-- In OS X 10.8 Mountain Lion and older, enable GUI Scripting globally by calling this handler and passing 'true' in the flag parameter before your script executes any GUI Scripting commands, or pass 'false' to disable GUI Scripting. Authentication is required only if the value of the 'UI elements enabled' property will be changed. Returns the final setting of 'UI elements enabled' even if unchanged.
tell application "System Events"
activate -- brings System Events authentication dialog to front
set UI elements enabled to flag
return UI elements enabled
end tell
else
-- In OS X 10.9 Mavericks, global access is no longer available and GUI Scripting can only be enabled manually on a per-application basis. Pass true to present an alert with a button to open System Preferences and telling the user to select the current application (the application running the script) in the Accessibility list in the Security & Privacy preference's Privacy pane. Authentication is required to unlock the preference. Returns the current setting of 'UI elements enabled' asynchronously, without waiting for System Preferences to open, and tells the user to run the script again.
tell application "System Events" to set GUIScriptingEnabled to UI elements enabled -- read-only in OS X 10.9 Mavericks
if flag is true then
if not GUIScriptingEnabled then
activate
set scriptRunner to name of current application
display alert "GUI Scripting is not enabled for " & scriptRunner & "." message "Open System Preferences, unlock the Security & Privacy preference, select " & scriptRunner & " in the Privacy Pane's Accessibility list, and then run this script again." buttons {"Open System Preferences", "Cancel"} default button "Cancel"
if button returned of result is "Open System Preferences" then
tell application "System Preferences"
tell pane id "com.apple.preference.security" to reveal anchor "Privacy_Accessibility"
activate
end tell
end if
end if
end if
return GUIScriptingEnabled
end if
end enabledGUIScripting
Hallo, ich wollte das Skript ausprobieren aber bekomme folgenden FEhler:
„System Events“ hat einen Fehler erhalten: NSReceiverEvaluationScriptError: 4
Hast Du GUI-Scripting bei Dir an? (Systemeinstellungen/Eingabehilfe den Hacken bei Zugriff für Hilfsgeräte setzen)
Es war nicht an, aber auch nach der Aktivierung kommt der Fehler.
Dann würde ich sagen, Du setzt vielleicht nicht 10.6 ein :?) Na?
Ist 10.6 Vorraussetzung? Das habe ich nirgends gelesen. Ich hab immer noch 10.4.
Nun… ich denke das liegt am GUI-Skripting… ich habe leider kein 10.4 mehr um das noch zu prüfen… sorry dann ist 10.6 wohl leider Mindestvorraussetzung :-/
Mm, schade. Dann muss ich mich noch gedulden, bis ich umsteige. Danke für die Hilfe!
Hallo hubionmac,
ich habe keine Ahnung von applescript und bin deshalb auf der Suche nach einem fertigen Skript mit dem ich, Abhängig vom Inhalt der Betrf.-Zeile die Anlagen der Mail auf einem Netzlaufwerk ablegen kann.
Das Skript soll über eine Regel, die den Betreff filtert, aufgerufen werden.
Dein Skipt macht ja was ähnliches aber bezieht sich auf die aktuelle Selektion in Mail und nicht auf neu ankommende.
Kann man das so ändern, dass die Neuen-Mails / also aus der Mailregel heraus / verarbeitet werden?
VG
Peter
Servus Peter,
da wollte ich gerade loslegen und etwas schreiben… da fällt mich auf, das ich so etwas schon längt geschrieben habe…
http://hubionmac.com/wordpress/2010/01/mail-regel-skript-attachments-sichern/ :-P
Hallo hubi,
hätte ich ja auch selber finden können – ich probier’s gleich morgen aus.
Vielen lieben Dank für die Hilfe
Peter
das script ist ja fantastisch :)
1 fragen habe ich allerdings:
ich möchte gerne mehrere mails > 300 gleichzeitig so abarbeiten. was müsste ich im script ändern?
grüsse aus der schweiz,
abi
ps: sorry doppelpost. aber ich hab den anderen beitrag (beim falschen script) geschrieben. die frage bezog sich auf dieses script hier! :)
ah, noch eine frage hab ich: bei mir unter 10.6.4 wird der ordner wie die mail benannt und die mail heisst _message.pdf, kann man das noch irgendwo ändern? :)
[…] This post was mentioned on Twitter by Rüdiger, Rüdiger Nitzsche. Rüdiger Nitzsche said: @mr_abi http://bit.ly/av8eSY als Basis sollte doch brauchbar sein […]
Finde den Script super! Ich bin allerdings ein Apple Scripts Neuling und krieg es nicht gebacken den Code vernünftig in Apple Scripts zu kopieren (alles erscheint lila, ZEilenumbrüche sind teilweise seltsam und Apple scripts meldet ständig Syntaxfehler und lässt mich nicht speichern) Könntest Du mir veilleicht den Script direkt mailen.
Wäre super!
Danke!
Ben
War ein Fehler im WordPress-Blog… der Code wird nun wieder korrekt dargestellt und solle im Skript-Editor auch wieder übersetzt werden können =)
Hallo Hubi!
Danke für das super Skript! Würde trotzdem gerne nochmal auf die Frage von “abi” zurückkommen: Ist es nicht möglich das Skript so abzuändern, daß doch die ganze Auswahl nacheinander abgelegt wird? Bin total neu in Apple-Skript und frage mich außerdem, ob ich jedes Mal auf das Skript (ich habs mal auf den Desktop gelegt) klicken muss, anschließend nochmal “Play” im Skripteditor, oder ob das auch einfach geht.
Vielen Dank für Deine Hilfe,
Alex
Nun, das Ding ist, dass das PDF quasi auf dem Fußweg über einen normalen Druckdialog erstellt wird. GUI-Skripting ist das… und das ist weniger dafür geeignet > 300 Mal problemlos zu funktionieren..
Aber wenn Dein Rechner flink genug ist, dürfte es ohne Anpassungen auch mit mehreren Dateien funktionieren 8-)
Du kannst ja mal
if (count of myselection)>1
und weiter untenelse error "…"end if
rausnehmen. und was den Aufruf angeht, such’ mal nach einem Programm mit dem Namen FastScripts bei Google…
Ich hasse es, wenn einem eine Idee nicht mehr los lässt… die neue Version kann nun auch zuverlässig mit mehreren Emails klar kommen =-)
Hallo Hubi,
leider kann ich auch kein applescript, aber dieses finde ich spitzenklasse. Könntest du mir noch einen hinweis geben, wie ich die “Empfänger email-Adresse in den Ordnernamen bekomme. Wäre echt toll. Danke schonmal.
Du müsstest die Variable currentFolder2Store etwas anpassen….
so in der Art:
set myemail to (email addresses of account of mailbox of currentMail)
set myemail to item 1 of myemail
set currentFolder2Store to mymailboxpath & currentDateSent & " " & currentSender & " " & currentSubject & " send to" & myemail & "/" as text
Hallo Hubi,
danke für die schnelle Antwort. Ganz klappt es noch nicht, da bei gesendeten mail von mir, der Empfänger immer als meine eigene Adresse schreibt. Gibt es da eine Möglichkeit, die “Empfänger-email-Adresse” zu bekommen? Dank und Gruß
Leider scheint es unter OS X Lion nicht mehr zu funktionieren. Script bricht mit folgender Fehlermeldung ab:
Error Number:System Events got an error: Der Zugriff für Hilfsgeräte ist deaktiviert.
-25211
Falls Mail im Vollbild ausgeführt wird (also “Vollbild” unter Lion) gibt es vorher noch folgende Fehlermeldung:
Error Number:Ist ja gar kein Message Viewer im Vordergrund, so kann ich einfach nicht arbeiten!
-2700
Sieht wohl so aus, als müßte man das unter Lion nochmal komplett überarbeiten…
OK. Die Hilfsgeräte sind jetzt aktiviert…
Trotzdem scheitert es unter Lion sobald eine Mail ein Attachment enthält in der Zeile:
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, (POSIX file currentFolder2Store) as alias, false)
save a in (((POSIX file currentFolder2Store) as rich text) & current_a_name) as rich text
mit dem Kommentar:
error “„Mail“ hat einen Fehler erhalten: rich text kann nicht in Typ constant umgewandelt werden.” number -1700 from rich text to constant
Leider verstehe ich das nicht..
nun Lars, das scheint ein Bug zu sein. Im OS, es scheinen zumindest auch andere Nutzer das Problem zu haben.. 10.7.1 (vermutlich ehr 10.7.2-3) wird es dann wohl richten. Ein Workaround könnte GUI-Scripting darstellen.. aber wer möchte das schon ;-P
Hallo Hubi,
sehr gutes Script um meine alten E-Mails übersichtlich zu archivieren.
Habe nur folgendes Problem:
Ich habe meine alten E-Mails nach Kunden/Personen in AppleMail in lokale Postfächer eingeordnet. Bei denen bekomme ich dann folgende Fehlermeldung:
– „Mail“ hat einen Fehler erhalten: „account of mailbox of message id 6868 of mailbox “xyz”“ kann nicht gelesen werden. –
Kann ich das Script entsprechend umschreiben, damit er auch die lokalen Postfächer mit einbezieht? (Kenn mich da leider nicht so aus).
Vielen Dank.
Hi Ole-Gunnar,
jetzt endlich komme ich mal dazu das zu beantworten…
Das liegt daran, dass Du lokale Postfächer nutzt, die schlichtweg keine Email-Adresse als Attribut besitzen. Hm… warum habe ich die Email-Adresse denn davon ausgelesen… macht vermutlich Sinn… Ich fange den Fehler mal ab und fragen in dem speziellen Fall dann die Empfänger-Adresse der Email selbst ab… der Code oben ist entsprechend aktualisiert…. Gruß Hubi
hi hubi!
ich suche nach einem script’, welches den text einer/mehrerer e-mail(s) als pdf inkl. aller anhänge (pdf, doc, xls, jpeg etc.) in einem ordner exportiert (name des ordners: “empfänger e-mail-datum-absender e-mail” ideal wäre, wenn ich den namen des ordners beim ausführen des scripts individuell bestimmen könnte). das o. a. script sollte diese anforderung erfüllen, wenn ich es richtig verstanden habe. nachdem ich mich absolut nicht mit scripts auskenne (bin pc-versiert und ein begeisterter mac-newcomer…), möchte ich dich fragen ob du mir eine ausführbare installationsprozedur zur verfügung stellen kannst, so dass ich das skript bloß installieren brauche. ich würde mich natürlich auch mit einer entsprechenden spende bedanken wollen. vielen dank schon mal im voraus, jhkar
Hallo Hubi,
danke für das tolle Script! Ich teste grade Deine Erweiterung, auch mehrere Mails auf einmal abzuarbeiten. Leider wird jedoch immer wieder das selbe pdf (der ersten Mail) unter unterschiedlichem Namen gespeichert. Wenn das richtig klappen würde, wäre es mir eine große Hilfe.
Beste Grüße
Andreas
Ich habe das mal mit der neusten Version vom 27.9 ausprobiert… damit funktioniert das einwandfrei unter 10.6.2
Nachtrag… dort halt ohne Anhänge aber das wird hoffentlich unter 10.6.3 wieder funktionieren
Danke Dir für die schnelle Antwort! Ich habe hier 10.7.2, dann könnte es natürlich daran liegen. Mal schauen, ob ich mir testweise einen Snow Leopard aufsetzen kann. Ich werde berichten!
Ich wünsch Dir schonnmal einen guten Rutsch ins neue Jahr
Gruß
Andreas
Bin noch nicht wach… 10.7.2 meinte ich… damit läuft es hier einwandfrei.
Dann bleibt mir der Sbow Leopard schonmal erspart, das ist doch was :-D
Also ich wähle mehrere Mails aus, sage dann Regeln anwenden und er erstellt auch sauber alle pdf Dateien. Aber alle haben den selben Inhalt. Mal schauen, was da noch bei mir falsch läuft. Da es bei Dir funktioniert, muss es ja irgendwas bei mir sein. Danke nochmal!
Beste Grüße
Andreas
Klingt super das Script. Leider bekomm ich immer folgende Fehlermeldung:
error “„Mail“ hat einen Fehler erhalten: rich text kann nicht in Typ constant umgewandelt werden.” number -1700 from rich text to constant
10.7.3
An welcher Stelle muss ich den Zielordner eingeben?
Grüße
Niklas
Servus Niklas,
ich habe mir den Quellcode noch einmal angesehen und habe nun unter 10.7.3 einen Weg gefunden die Anhänge nun auch wieder zu speichern (das Format der Speicherpfade war schlichtweg ein anderes als bei den älteren Mail-Versionen… )
Die neue Version ist am Ende des Blog-Eintrages angefügt worden.
Die Zielordner werden ganz zu Anfang definiert… diese liegen als Standard auf dem Desktop, da zudem das PDF auch dort standardmäßig landet.
[…] Aber zahlreiche Gigabyte an Daten sollen nicht umsonst gelöscht worden sein, das Skript zum Speichern von Emails als PDF nebst deren Anhängen funktioniert nun wieder unter 10.7, und das ohne den Schreibtisch zu löschen =)This entry was […]
Vielen Vielen Dank! So klappt es!
Hi Hubi,
Danke für das tolle Script. Es macht genau das was ich lange gesucht habe und erleichtert mir die tägliche Arbeit ungemein.
Ich habe aber einen Feature Request. Kann man das Script so umschreiben, dass sowohl die Email und die möglichen Anlagen in einer PDF Datei quasi aneinander gehängt werden!? Möglichst das Ganze so ohne das bei der Konvertierung keine Fenster aufgehen und um das Script als Mail Regel zu nutzen.
Danke im Voraus.
Thomas
Danke für das Lob… aber wie sollen denn die Attachments an das PDF gehängt werden und welches Programm soll die dann wieder vom PDF trennen? Wüsste leider nicht wie. Und den part mit GUI-Scripting musste ich leider wählen, da ich sonst nicht mit Bord-Mitteln die .emlx Datei in ein PDF umwurschteln konnte.
Hm, Ruby könnte da eine Lösung liefern, nur dürftest Du dann nicht vor der Nachinstallation einiger Ruby-Gems zurückschrecken :-/
Hallo nochmal.
Verstehe – ohne den Umweg über das GUI oder Ruby geht es nicht. Mist! Es wäre nur schön gewesen seine Mails und Attachements in einer PDF Datei zu bündeln, da ich diese aktuell über eine Ordnerstruktur archiviere. Der Vorteil von einer Datei für alles liegt dabei auf der Hand – ich muss mir nicht mehrere Dateien zusammensuchen und öffnen, wenn ich Sie z. B. drucken müsste.
Nun denn … Danke trotzdem das Du so schnell geantwortet hast.
Gruß
Thomas
Hallo Hubi,
kann es sein, dass es in 10.8.2 wieder mal nicht mehr geht? Kann man das einfach anpassen?
Danke und schönen Gruß, Andreas
Kann sein… ich habe gerade kein 10.8 zur Hand (bin noch unter 10.7 unterwegs…) ich habe wohl erst in 2 Wochen dafür Zeit :-/
Hallo Hubi, gibt es schon ein update?
Servus Hubi!
Danke für das tolle Script. Mit der Version 10.8.2 schleicht sich immer der gleiche Hinweis ein: error “„Mail“ hat einen Fehler erhalten: Fehler in der AppleEvent-Routine.” number -10000 (im Bereich: save a in (currentFolder2Store & current_a_name as rich text)).
Gibts dafür eine Abhilfe?
Update ist raus.
So wie ich das lese,ist das Script genau das, was ich brauche: Mehrere Emails auswählen (egal ob aus Boxen oder im Eingang), Script aktivieren und alle landen als einzelne PDFs auf dem Schreibtisch. Blöde Frage: Wie aktiviere ich das in Mail? Muss ich Spplescript irgendwo noch aktivieren, dass Mail drauf zu greift?
Montain Lion 10.8.2
Update ist raus.
Wow, das war aber eine schnelle Reaktion. Ich habe es unter library/ApplicationScripts/com.apple.mail gespeichert, nur wie rufe ich das in Mail auf, wenn ich die Emails markiert habe? Sorry, Applescript ist für mich ein Buch mit 7 Siegeln. Was muss ich machen?
[…] hubi | Published: February 18, 2013 Ich habe dem Skript ein kleines Update verpasst, so verstummt ihr Update-Anfragen und lasset es Spenden regnen. […]
Darf ich noch mal? Den richtigen Ordner habe ich gefunden, das klappt. Was ist wenn ich mehrere E-Mails markiere? Da wird nur die oberste gespeichert. Und hattest Du das Script nicht so angelegt, dass als Name das Datum vorne steht und dann der Betreff? Dann wäre ich wirklich glücklich!
Beides sollte wie von Dir beschrieben so funktionieren… sofern Du den neusten Quellcode verwendest…
Sorry, geht bei mir nicht. Als Dateiname wird immer nur der Betreff gesetzt und es funktioniert nur, wenn ich eine Mail ausgewählt habe. Sind mehrere Einzelmails ausgewählt oder ein Gesprächsfaden aus mehreren Mails (so gestapelt), passiert gar nichts.
Hallo,
ich finde das die letzte Version des Scripts “very hot”.
Mein Wunsch wäre die Backups im Ordner “Documents” zu haben.
Daher habe ich probiert den Pfad von Deskop auf Documents umzubiegen.
Leider ohne Erfolg….Komme halt nicht von der Programmierer Seite.
Es wäre super, wenn mir jemand dabe helfen könnte! :-)
Danke und Gruss
Due
….habs nun selber herausgefunden
Hi hubi, this is just to let you know, that I used your code, changed it a little bit and used the Evernote API to transfer mails directly to Evernote. You will find the announcement in the Evernote forum: http://discussion.evernote.com/topic/38831-archive-apple-mail-into-evernote-with-the-evernote-api/ or go to codesnippets.fraenklenet.de.
Cool, freut mich…. bin gespannt es zu sehen, wenn Deine Website fertig umgezogen ist. =!)
Danke, dass Du mich daran erinnert hast, dass ich im August 2012 fertig sein will :-)
Hallo,
wäre ein Super Script könnte ich sehr gut gebrauchen nur bringt das Script beim Ausführen die Fehlermeldung “AppleScript-Fehler Die Variable Aktivate ist nicht definiert”.
Habe die MacOsX Version Mavericks.
Wäre nett wenn du das beheben könntest oder mir sagst was ich machen kann.
Viele Grüße
Thomas
Hallo Thomas,
ich habe eine neue Version hochgeladen. In der Tat musste ich für Mavericks eine Kleinigkeit umstellen. Zudem gab es in der Zwischenzeit auch mal Fehler mit einer neuen Evernote API.
Probier die neue Version bitte mal aus; Du solltest auch darauf achten, dass “Darstellung – Nach Konversation ordnen” nicht aktiviert ist. Da gibt es manchmal auch Probleme. Ich bin dem Fehler nicht nachgegangen, da ich ohne “Nach Konversation ordnen” auskommen kann.
Gruß Volker
Geht wieder und 10.9.2.
Servus Hubi! Mit der Mac OS 10.9.1 funktioniert das Script bei mir nicht – keinerlei Reaktion. Gibt es wieder ein Update, wie im Februar 2013 ;-))
Servus Dieter,
das ist der Fluch den GUI-Skriptings… das funktioniert nur so lange das GUI gleich bleibt. Ich schaue mal was sich unter 10.9 geändert hat und poste dann ein Update
Gruß Hubi
Servus Hubi!
Ich hoffe Du hast mich(uns) nicht vergessen! Das Leben kann ohne solche Features ziemlich anstrengend sein…
Grüße Dieter
Geht wieder und 10.9.2.
Servus Hubi! Herzlichen Dank, es funktioniert wieder super – einzig die Versionsabfrage muss man auf 10.9 ändern, dann geht es prächtig – Danke!
Ah stimmt… ich hatte die Codebox vom 10.8er Update aus Versehen eingefügt. Jetzt ist die aktuelle Version für 10.9 ganz unten zu sehen.
[…] vielversprechendste AppleScript kam von Hubertus Schulze Rückamp: Email als PDF speichern, mit Attachments. An dieser Stelle nochmal herzlichen Dank für die Vorlage! Ich benötigte einige Anpassungen, da […]
Servus Hubi! Zur Info: mit der neuen Mail Version 7.3 (1878.2) in OS X 10.9.3 gibt es KEINE Probleme, funktioniert tadellos.
Das Script hat auf jeden Fall einen Fehler:
if movedIt is false then error “Fehler beim Bewegen der gedruckten Nachricht: ” & msg as text
Hat msg noch aus alten Versionen mit on error msg
Außerdem landet bei mir das 1.pdf im Standard Folder des Save dialogues (der Speicherplatz wird also nicht geändert) Dies kann aber auch an DefaultFolder X liegen, da werde ich noch mal schauen.
Auch mit der Version 10.9.4, Mail Version 7.3 (1878.6), gibt es bei mir keinerlei Fehleranzeigen.
Merci für das Feedback =)