I have been working on a script to save email and their attachments as PDF files. Overcomplicated scrips have me floundering so have started from the beginning with what I thought was a simple approach. See the scrip below. But I cannot even get past the delay routine it keeps telling me the result is false and I have to stop the scrip. If I comment out the delay routine I am then told it cannot get sheet 1 of process mail.
tell application "Finder"
set destinationFolder to (choose folder)
tell application "Mail"
set theMsg to selection
set the Subject_ to item 1 of theMsg
tell application "System Events"
tell process "mail"
keystroke "p" using command down
repeat until exists sheet 1 of window 1
delay 0.02
end repeat
tell sheet 1 of window 1
click menu button "PDF"
click menu button "save as PDF" of menu 1 of menu button "PDF"
set value of text field 1 to PosixPath of destinationFolder & "/" & Subject_
end tell
end tell
end tell
end tell
end tell
Can anyone help please.
Peter
Hi MitchBVI,
You have a bunch of problems in your “simple” script.
First, you have everything in a tell Finder block of code. That makes no sense. Why are you telling the Finder to tell Mail and System Events to do anything? You don’t need the Finder for that. It’s not efficient and can cause errors. You actually don’t need the Finder for any of the code. You also have the system events code inside the tell Mail code. As explained, you don’t need Mail to tell System Events. Next, spelling is important. You have to tell process Mail… using a capital M. That’s the source of the error you mentioned. Next, “save as PDF” is also spelled wrong (capital S), plus it’s a menu item not a menu button, and finally it has 3 dots in its name (typed by pressing option-semi colon).
There’s a few more errors in your code but I’ll stop now. I’m not meaning to be critical. I’m just pointing out the errors so you can learn. Here’s working code. I hope it helps.
set destinationFolder to choose folder
set posixDestFolder to POSIX path of destinationFolder
tell application "Mail"
activate
set theMessages to selection
set theMsg to item 1 of theMessages
set theSubject to subject of theMsg
end tell
tell application "System Events"
tell process "Mail"
keystroke "p" using command down
repeat until exists sheet 1 of window 1
delay 0.02
end repeat
tell sheet 1 of window 1
click menu button "PDF"
repeat until exists menu 1 of menu button "PDF"
delay 0.02
end repeat
click menu item "Save as PDF." of menu 1 of menu button "PDF"
end tell
repeat until exists window "Save"
delay 0.02
end repeat
tell window "Save"
keystroke "g" using {command down, shift down}
repeat until exists sheet 1
delay 0.02
end repeat
end tell
keystroke posixDestFolder
keystroke return
tell window "Save"
set value of text field 1 to theSubject
end tell
end tell
end tell
Wow and thank you. The script I posted was part of a longer one that also saved attachments. Checked for bad characters etc. That did not work or at least in total so I wrote the script I posted to check it and as you correctly pointed out full of spelling mistakes.
Anyway thanks again I am hopeful I can adapt what you provided to do all I need and that is greatly appreciated.:)
I am relatively new to Applescript, I started a tell block in finder as I thought I need that to choose the folder to save the PDF in
Take care
Peter