Hi Layne
Thanks for all your help.
I think the problem is to do with Lion as from 7/13/2011 it worked just fine until I upgraded to Lion and presto no attachment save. I had not specified the file type but as you can see I had stripped the extension and added that to a new attachment name. So if was a word document it would have doc extension a PDF file then PDF and so on.
Peter
Peter,
I looked on another thread and StefanK was having the same problem. There has tobe a bug with the “save” command.
Guess we have to wait for Apple to fix that.
do you think opening the attachment then using the save as command might help?
Peter
Using GUI scripting like the first part of your script would probably work.
Do you have an application such as UIElementsInspector?
Not sure I understand when you say GUI like the beginning of the script, as I said at the oust I have had a lot of help putting this together and was very happy when it was working. guess I should have known better than to upgrade.
Anyway did not have the UIElementsInspecotor so downloaded it. When I hover over a mail it gives me details of that message but cannot see anything about the attachment , how did you think I could use this please?
GUI scripting, as in
tell application "System Events"
tell process "Mail"
click menu item 1 of menu 4 of menu bar 1
end tell
end tell
Use UIElementsInspector to find the hierarchy of UI items, such as a button or a menu item. What i mention was find a way to save an attachment manually and then lay down the code.Here is a GUI tutorial.
I must be getting more stupid by the minute I think I figured out what you meant and adapted your scrip to do something simple like open the message as follows.
tell application "Mail"
set themsg to selection
if (count of themsg) is less than 1 then --Makes sure a message has been selected in mail box
display dialog "Please select a message in Mail before running this script."
else
tell application "System Events"
tell process "Mail"
click menu item 4 of menu 3 of menu bar 1
end tell
end tell
activate "Mail"
end if
end tell
It runs OK but does not open the message. The event panel shows the following.
tell application “Mail”
get selection
→ {message id 259 of mailbox “OIL/Peake” of account “MobileMe”}
end tell
tell application “System Events”
click menu item 4 of menu 3 of menu bar 1 of process “Mail”
→ menu item “Open Message” of menu “File” of menu bar item “File” of menu bar 1 of application process “Mail”
end tell
tell application “Mail”
activate
end tell
Thank you again for your help. If opening would work I see what you mean as menu item 11 is save attachment.
Peter
Got his from the GUI tutorial you provided and adapted it open messages. Which works just fine.
-this from http://www.macosxautomation.com/applescript/uiscripting/index.html with slight adaptation opens mail message ok next step to save attachments
my do_menu("Mail", "File", "Open Message")
on do_menu(app_name, menu_name, menu_item)
try
-- bring the target application to the front
tell application app_name
activate
end tell
tell application "System Events"
tell process app_name
tell menu bar 1
tell menu bar item menu_name
tell menu menu_name
click menu item menu_item
end tell
end tell
end tell
end tell
end tell
return true
on error error_message
return false
end try
end do_menu
When I change “Open message” to “Save Attachments” this is what I get
tell application “Mail”
activate
end tell
tell application “System Events”
click menu item “Save Attachments” of menu “File” of menu bar item “File” of menu bar 1 of process “Mail”
→ error number -1728 from «class menI» “Save Attachments” of «class menE» “File” of «class mbri» “File” of «class mbar» 1 of «class prcs» “Mail”
end tell
Result:
false
Guess it is going to be more difficult than I thought. thanks
Peter
I once had a similar error. Try:
tell application "Mail"
activate
end tell
tell application "System Events"
tell process "Mail"
click menu item "Save Attachments" of menu "File" of menu bar 1
end tell
end tell
No good with that I am afraid. This is the result.
tell application “Mail”
activate
end tell
tell application “System Events”
click menu item “Save Attachments” of menu “File” of menu bar 1 of process “Mail”
→ error number -1728 from «class menI» “Save Attachments” of «class menE» “File” of «class mbar» 1 of «class prcs» “Mail”
Result:
error “System Events got an error: Can’t get menu item "Save Attachments" of menu "File" of menu bar 1 of process "Mail".” number -1728 from menu item “Save Attachments” of menu “File” of menu bar 1 of process “Mail”
I got the script to work. Our mistake was forgetting that the three periods at the end of “Save Attachments…” are actually part of the title…
Add this script to yours:
set theFolder to (choose folder) --choose folder to save attachments (i did this just so my script would work)
tell application "Mail" to activate
tell application "System Events"
tell process "Mail"
--here's where your script went slightly wrong. The three periods after "Save Attachments" are part of the name
click menu item "Save Attachments." of menu "File" of menu bar 1
delay 0.2
keystroke "g" using {command down, shift down} --open go to folder sheet
delay 0.2
tell sheet 1 of sheet 1 of window 1
set value of text field 1 to POSIX path of theFolder --the go to folder text field
end tell
tell sheet 1 of window 1 --this automatically switches to the first sheet again
click button "Save" -- the button on the first sheet
end tell
end tell
end tell
(* You can then replace the shaky part of your original script with this ^^^^^ *)
Hi Layne
sorry I did not reply yesterday but I still cannot get this to work I get an error message. that Mail cannot save the message. When I increase the delay after keystroke “g” using etc I see a go to folder box and the text is ~/Library/Preferences/ByHost/ which I do not understand. when I have a look in that folder there is nothing in it created today.
The log looks like this.
tell application “AppleScript Editor”
choose folder
→ alias “Macintosh HD:Users:mitch:Desktop:MailTest:”
end tell
tell application “Mail”
activate
end tell
tell application “System Events”
click menu item “Save Attachments.” of menu “File” of menu bar 1 of process “Mail”
→ menu item “Save Attachments.” of menu “File” of menu bar item “File” of menu bar 1 of application process “Mail”
keystroke “g” using {command down, shift down}
set value of text field 1 of sheet 1 of sheet 1 of window 1 of process “Mail” to POSIX path of alias “Macintosh HD:Users:mitch:Desktop:MailTest:”
click button “Save” of sheet 1 of window 1 of process “Mail”
→ button “Save” of sheet 1 of window “Peake ” MobileMe (62 messages)” of application process “Mail”
end tell
Result:
button “Save” of sheet 1 of window “Peake ” MobileMe (62 messages)” of application process “Mail” of application “System Events”
If your computer is slower (more applications running) then there will need to be a longer delay.
As for the error, I am not sure what you mean. You said there was an error saving a message, but the script saves the attachments of a message. If there are no attachments, there is an error.
As for the folder go-to box it doesn’t matter what is says; the script fills it with the appropriate path. Don’t worry about the strange folder. There are a lot of hidden odd folders on your comouter.
Sorry not clear it says it cannot save the attachment I did make sure the message I was testing had an attachment.
.
Just reloaded your script as I had been modifying the original one and now get a new error.
tell application “AppleScript Editor”
choose folder
→ alias “Macintosh HD:Users:mitch:Desktop:MailTest:”
end tell
tell application “Mail”
activate
end tell
tell application “AppleScript Editor”
«event prcsclic» «class menI» “Save Attachments.” of «class menE» “File” of «class mbar» 1 of «class prcs» “Mail”
→ menu item “Save Attachments.” of menu “File” of menu bar item “File” of menu bar 1 of application process “Mail”
«event prcskprs» “g” given «class faal»:{command down, shift down}
set «class valL» of «class txtf» 1 of «class sheE» 1 of «class sheE» 1 of window 1 of «class prcs» “Mail” to «class posx» of alias “Macintosh HD:Users:mitch:Desktop:MailTest:”
→ error number -1728 from «class posx» of alias “Macintosh HD:Users:mitch:Desktop:MailTest:”
Result:
error “System Events got an error: Can’t set POSIX path of alias "Macintosh HD:Users:mitch:Desktop:MailTest:" to POSIX path of alias "Macintosh HD:Users:mitch:Desktop:MailTest:".” number -10006 from POSIX path of alias “Macintosh HD:Users:mitch:Desktop:MailTest:”
I will repair permissions and start again.
thanks
Peter
I had the same error until I included the “tell sheet 1 of sheet 1 of window 1”. Then that part worked. The script still works for me. This is more confusing then I thought was possible in AppleScript.
Just ran the original script on my Laptop which is still using Snow Leopard and it worked perfectly, so like you I am confused.
Got stopped just then so will now run permissions repair and restart and let you know how I go.
Totally unrelated (I think) I am having trouble with my magic mouse a single click often (not always) opens dialog boxes like Icon, Icon Text etc. Have switched to the pad and that seems better.
Hi,
GUI scripting is a weird thing, because it’s time critical.
The static delay statements may work on the computer which the script has been written on.
But there’s no guarantee that the script works reliably on every computer.
Using dynamic delays (waiting for existence of certain UI elements) is much more reliable
set theFolder to (choose folder) --choose folder to save attachments (i did this just so my script would work)
activate application "Mail"
tell application "System Events"
tell process "Mail"
--here's where your script went slightly wrong. The three periods after "Save Attachments" are part of the name
click menu item "Save Attachments." of menu "File" of menu bar 1
tell window 1
repeat until exists sheet 1
delay 0.2
end repeat
keystroke "g" using {command down, shift down} --open go to folder sheet
repeat until exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 of sheet 1
set value of text field 1 to POSIX path of theFolder --the go to folder text field
click button "Go"
end tell
repeat while exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 --this automatically switches to the first sheet again
click button "Save" -- the button on the first sheet
end tell
end tell
end tell
end tell
Hi Stefan
and thanks once again you may not remember but you helped me with the original script. Should have thought of dynamic delays myself. Still very strange that the scrip worked on this machine under snow Leopard and does not under Lion.
anyway thanks again all I have to do is adapt that to my original scrip.
Peter
try
set POSIXPathOfTheFolder to POSIX path of (choose folder) --choose folder to save attachments (i did this just so my script would work)
.
set value of text field 1 to POSIXPathOfTheFolder --the go to folder text field
.
Hi Layne & Stefan
Works at last
clumsy in places but I can run it with a hotkey and it sure helps when tidying up mail. I wonder what other problems we will have with Lion.
thank you both for all your help.
Peter
--This script is underdevelopment 7/30/2011 to take a selected email message save it as a PDF file and then save any attachments to the same folder with the file name given to the attachment by the sender. Numbners files sequentially so rather than 1,11 we have 1,2 etc .
global listOfFiles, filecount, Subject_, destinationFolder, d
activate application "Finder"
tell application "Finder"
set destinationFolder to (choose folder)
set listOfFiles to the name of every file in destinationFolder
set filecount to count every item in listOfFiles
end tell
activate application "Mail" --Brings window to the front
tell application "Mail"
set themsg to selection
if (count of themsg) is less than 1 then --Makes sure a message has been selected in mail box
display dialog "Please select a message in Mail before running this script."
else
set themsg to item 1 of themsg
set theMsgCount to {}
tell themsg
set Subject_ to subject of item 1 of themsg
set Sender_ to sender of item 1 of themsg
set Recipients_ to to recipients
set Recipients_ to address of first recipient of item 1 of themsg
set Sender_ to extract name from Sender_
set brkpos to offset of "@" in Recipients_ --Finds position of start of URL in email address
--display dialog "Break " & brkpos
set Recipients_ to characters 1 through (brkpos - 1) of Recipients_
display dialog "Sent to " & Recipients_ & return & "Sent by " & Sender_
set theMsgCount to mail attachments of themsg
set theMsgCount to count every mail attachment of themsg
set Subject_ to text returned of (display dialog "From " & Sender_ & return & "Proposed file name is in the box" & return & "Select OK or start typing to erase and change" & return & "and then Select OK" buttons {"OK", "Change"} default button "OK" default answer Subject_)
set Subject_ to my FixFileName(Subject_) --Calls handler to strip bad characters
set Subject_ to my getEmptyPath((destinationFolder as string), Subject_)
activate application "Mail" --Brings window to the front
end tell
my FileasPDF() --Calls handler to process the save part of routine
my SaveAttach() -- Now using GUI in Lion to work
set check to the button returned of (display dialog "Delete " & return & Subject_ & return & " from Mail " buttons {"Yes", "No"} default button 1)
if check = "Yes" then
delete themsg
else
set check to the button returned of (display dialog "Standard proceuder is to flag the message to show it has been saved" & return buttons {"yes", "No"} default button 1)
if check = "Yes" then
tell application "Mail"
activate
tell application "System Events"
tell process "mail"
keystroke "l" using {command down, shift down}
end tell
end tell
end tell
end if
end if
end if
end tell
------------------------------------------
on FileasPDF()
tell application "System Events"
tell process "Mail"
keystroke "p" using command down
repeat until exists sheet 1 of window 1
delay 0.2
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.2
end repeat
tell window "Save"
keystroke "g" using {command down, shift down}
repeat until exists sheet 1
delay 0.2
end repeat
tell sheet 1
--Subject_Added Peter to test how to pass file name
set value of text field 1 to POSIX path of destinationFolder & "/" & Subject_
click button "Go"
end tell
repeat while exists sheet 1
delay 0.2
end repeat
click button "Save"
end tell
end tell
end tell
end FileasPDF
-----------------------------
on SaveAttach()
tell application "System Events"
tell process "Mail"
--here's where your script went slightly wrong. The three periods after "Save Attachments" are part of the name
click menu item "Save Attachments." of menu "File" of menu bar 1
tell window 1
repeat until exists sheet 1
delay 0.2
end repeat
keystroke "g" using {command down, shift down} --open go to folder sheet
repeat until exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 of sheet 1
set value of text field 1 to POSIX path of destinationFolder --the go to folder text field
click button "Go"
end tell
repeat while exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 --this automatically switches to the first sheet again
click button "Save" -- the button on the first sheet
end tell
end tell
end tell
end tell
end SaveAttach
-----------------------------------------
on FixFileName(str) --Deletes characters that cannot be used in file names
--display dialog "Check Filename for duplicates"
--display dialog "Fixed str " & str
set fixed_string to {}
set bad_char to {":", "/"}
repeat with c from 1 to (count every character in str)
if bad_char contains (character c of str) then
set end of fixed_string to "-"
else
set end of fixed_string to (character c of str)
end if
end repeat
fixed_string as string
set str to fixed_string
end FixFileName
---------------------------------------
-- See if a file with the passed name exists
-- If so, add an integer to the name, and check again
-- Keep checking until an unused name is found
-- Parameters: path of container as text
-- file name, no extension
-- This is only intended to save mail messages as PDF's so that extension is added in the handler
-- Returns: updated file name
on getEmptyPath(destinationFolder, Subject_)
set filePath to destinationFolder & Subject_
set Ext to ".PDF" --only checking for file type PDf as that is how the message is being saved
tell application "Finder"
if exists file (filePath & Ext) then
set x to 1 -- start counting
repeat
set cFilePath to filePath & " " & x & Ext
if exists file cFilePath then -- found empty spot?
set x to x + 1 -- no, keep counting
else -- yes, leave
exit repeat
end if
end repeat
return Subject_ & " " & x -- this name is safe to use
else
return Subject_ -- use original name
end if
end tell
end getEmptyPath
-----------------------------------------
on ParseName(str) --Takes attachment name and strips extension not in use
set d to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set str to every text item of str as list
set AppleScript's text item delimiters to " "
set str to every text item of str as text
set strcnt to count of words in str
set str3 to {}
set ext1 to last word of str
if strcnt is greater than 2 then
repeat with n from 1 to strcnt - 1
set str2 to word n of str
set str3 to str3 & str2
end repeat
else
set str3 to word 1 of str
display dialog "In Parse Name " & str3
end if
set str3 to str3 as string
set str to my replaceChars(str3)
end ParseName
---------------------------------
on replaceChars(thisText) --Removes spaces not in use called from ParseName
set d to AppleScript's text item delimiters
set AppleScript's text item delimiters to the " "
set the itemList to every text item of thisText
set AppleScript's text item delimiters to the ""
set thisText to the itemList as string
set AppleScript's text item delimiters to ""
return thisText
set AppleScript's text item delimiters to d
end replaceChars