I know how to target a particular message within the inbox.
tell application "Mail"
set selectedMsgs to first message of inbox
end tell
But I don’t know how to target an open mail message and any contents or attachments of it.
For example if I were to scroll down to my 20th email in my inbox and open that message, that is the message and whatever attachments that I want to assign a variable to. Does anybody know how?
Maybe it might help to see my full script? However, right now I am forced to using the first email of the inbox.
The recipient is going to double-click an application script and the script will then copy files to their machine. sort of like an installer.
set dupe_folder to ((path to current user folder as Unicode text) & "Desktop:.Open_This_Folder:")
tell application "Finder"
try
delete folder dupe_folder
end try
end tell
tell application "Finder" to if not (exists folder dupe_folder) then do shell script "mkdir -p " & quoted form of POSIX path of dupe_folder
tell application "Mail"
set selectedMsgs to first message of inbox
repeat with counter from 1 to (count of selectedMsgs)
set theAttachments to (count of mail attachments of selectedMsgs)
display dialog "There are " & theAttachments & " attachments."
repeat with i from 1 to theAttachments
set att to mail attachment i of selectedMsgs
set {name:theName, MIME type:mimeType} to att
save att in (((path to current user folder) as Unicode text) & "Desktop:.Open_This_Folder:" & theName)
end repeat
end repeat
end tell
---------------------------------------------------------------------
--Copy QuicKey file to Shortcuts
tell application "Finder"
try
set file_QuicKey to file ((path to current user folder as Unicode text) & "Desktop:Open_This_Folder:ImageFind")
set file_QuicKey2 to file ((path to current user folder as Unicode text) & "Desktop:Open_This_Folder:PDF_Find_Only")
set file_QuicKey3 to file ((path to current user folder as Unicode text) & "Desktop:Open_This_Folder:Illustrator")
set file_QuicKey4 to file ((path to current user folder as Unicode text) & "Desktop:Open_This_Folder:Illustrator CS")
set file_QuicKey5 to file ((path to current user folder as Unicode text) & "Desktop:Open_This_Folder:SaveAsEPSorAI CS4")
try
set file_QuicKey6 to file ((path to current user folder as Unicode text) & "Desktop:Open_This_Folder:iTunes_ReplacePrefsStartupItem.app")
end try
on error
try
set file_QuicKey to file ((path to current user folder as Unicode text) & "Downloads:Open_This_Folder:ImageFind")
set file_QuicKey2 to file ((path to current user folder as Unicode text) & "Downloads:Open_This_Folder:PDF_Find_Only")
set file_QuicKey3 to file ((path to current user folder as Unicode text) & "Downloads:Open_This_Folder:Illustrator")
set file_QuicKey4 to file ((path to current user folder as Unicode text) & "Downloads:Open_This_Folder:Illustrator CS")
set file_QuicKey5 to file ((path to current user folder as Unicode text) & "Downloads:Open_This_Folder:SaveAsEPSorAI CS4")
try
set file_QuicKey6 to file ((path to current user folder as Unicode text) & "Downloads:Open_This_Folder:iTunes_ReplacePrefsStartupItem.app")
end try
end try
end try
try
set dupe_folderAdmin to ((path to current user folder as Unicode text) & "Library:QuicKeys:Shortcuts:")
set dupe_folder2Admin to ((path to current user folder as Unicode text) & "Library:Application Support:Cindy_PList:")
duplicate file_QuicKey to dupe_folderAdmin with replacing
duplicate file_QuicKey2 to dupe_folderAdmin with replacing
duplicate file_QuicKey3 to dupe_folderAdmin with replacing
duplicate file_QuicKey4 to dupe_folderAdmin with replacing
duplicate file_QuicKey5 to dupe_folderAdmin with replacing
try
duplicate file_QuicKey6 to dupe_folder2Admin with replacing
end try
end try
end tell
try
tell application "KillQKeys2"
activate
end tell
end try
tell application "SystemUIServer"
activate
display dialog "Congratulations the QuicKeys were copied Successfully." buttons {"OK"} with icon 1 giving up after 4
end tell
Here is an outline of a solution to detect the message in the frontmost window in Mail.app
This code is by no means tested, this is a sketch/outline.
tell application "Mail.app"
local msgid
set msgid to 0
set myWin to its window 1
if id of window of front message viewer is id of myWin then return
” The message viewer was the front window.
set wnNm to name of myWin
set selMessages to front message viewer's selected messages
repeat with aMessage in selMessages
if subject of aMessage is wnNm then
set msgid to id of aMessage
exit repeat
end if
end repeat
if msgid ≠0 then
” there is some stuff to be done by your script.
else
” there is even more stuff to be done, as you have to trail the mailboxes for the message.
end if
end tell
I think this was how I related an open message window to a message.
This method isn’t foolproof because the user can fool around between message window and the viewer.
And two mails can have the same subject when trying to find the mail with the same title in the window.
If I were you I’d really have the user close the message window with cmd w then when having the message
highlighted in the message viewer; then executing your script
Maybe this post; (the inRSSFeed() handler) also might help you.
-And Ben Waldie has written a great article on Scripting Mail.app which can be found at Mactech The Journal Of Apple Technology
McUsr,
Your code was excellent! I have the script doing exactly what I want now. I may be wrong but I think the only change that was required was changing.
if msgid ≠0 then
to
if msgid ≠{} then
Here is my complete script. I may have some unnecessary code, e.g. “using terms from”, because I am not so savvy on when that is required.
Thank you so very much for replying to my post.
-Jeff
--set the names of the files that reside in the folder "DoNotOpen" of the mail attachment. These are the files that will be copied to the invisible file on the desktop and then to the QuicKeys:Shortcuts folder. It's presently set up to copy a maximum of 5 files.
set file1 to "ImageFind"
set file2 to "Illustrator CS"
set file3 to "Illustrator"
set file4 to "PDF_Find_Only"
set file5 to "SaveAsEPSorAI CS4"
---------------------------------------
---------------------------------------
---------------------------------------
--First we make an invisible folder on the desktop
using terms from application "Finder"
set dupe_folder to ((path to current user folder as Unicode text) & "Desktop:.Open_This_Folder:")
tell application "Finder"
try
do shell script ("rm -rf " & quoted form of (POSIX path of dupe_folder))
end try
end tell
end using terms from
--------------------
tell application "Finder" to if not (exists folder dupe_folder) then do shell script "mkdir -p " & quoted form of POSIX path of dupe_folder
tell application "Mail"
tell application "Mail"
local msgid
set msgid to 0
set myWin to its window 1
set wnNm to name of myWin
if wnNm does not contain "Updates to Copy" then display dialog "Make sure you click on the email in the inbox so the email opens in its own window. Then double-click the .app file" buttons {"Cancel"} default button "Cancel" with icon stop
if id of window of front message viewer is id of myWin then return
-- The message viewer was the front window.
set wnNm to name of myWin
--display dialog wnNm
set selMessages to front message viewer's selected messages
repeat with aMessage in selMessages
if subject of aMessage is wnNm then
set msgid to id of aMessage
exit repeat
end if
end repeat
if msgid ≠{} then
repeat with counter from 1 to (count of aMessage)
set theAttachments to (count of mail attachments of aMessage)
--display dialog "There are " & theAttachments & " attachments."
repeat with i from 1 to theAttachments
set att to mail attachment i of aMessage
set {name:theName, MIME type:mimeType} to att
--display dialog theName as string
--test to make sure the email is indeed open with the correct attachments that match our criteria
if theName does not end with ".app" and theName does not contain "DoNotOpen" then display dialog "Make sure you click on the email in the inbox so the email opens in its own window. Then double-click the .app file" buttons {"Cancel"} default button "Cancel" with icon stop
--test to make sure we don't copy the application script
if theName does not end with ".app" then
save att in (((path to current user folder) as Unicode text) & "Desktop:.Open_This_Folder:" & theName)
end if
end repeat
end repeat
end if
end tell
---------------------------------------------------------------------
--Copy QuicKey file to Shortcuts
--tell application "Finder"
using terms from application "Finder"
tell application "Finder"
--set the path to where you want to copy the files to on the local machine of the email recipient
try
set dupe_folderAdmin to ((path to current user folder as Unicode text) & "Library:QuicKeys:Shortcuts:")
end try
----------------------------------
----------------------------------
----------------------------------
----------------------------------
--set variable(s) to the files and then copy them to the destination folder.
try
set file_QuicKey to file ((path to current user folder as Unicode text) & "Desktop:.Open_This_Folder:DoNotOpen:" & file1)
try
duplicate file_QuicKey to dupe_folderAdmin with replacing
end try
end try
----------------------------------
try
set file_QuicKey2 to file ((path to current user folder as Unicode text) & "Desktop:.Open_This_Folder:DoNotOpen:" & file2)
try
duplicate file_QuicKey2 to dupe_folderAdmin with replacing
end try
end try
----------------------------------
try
set file_QuicKey3 to file ((path to current user folder as Unicode text) & "Desktop:.Open_This_Folder:DoNotOpen:" & file3)
try
duplicate file_QuicKey3 to dupe_folderAdmin with replacing
end try
end try
----------------------------------
try
set file_QuicKey4 to file ((path to current user folder as Unicode text) & "Desktop:.Open_This_Folder:DoNotOpen:" & file4)
try
duplicate file_QuicKey4 to dupe_folderAdmin with replacing
end try
end try
----------------------------------
try
set file_QuicKey5 to file ((path to current user folder as Unicode text) & "Desktop:.Open_This_Folder:DoNotOpen:" & file5)
try
duplicate file_QuicKey5 to dupe_folderAdmin with replacing
end try
end try
----------------------------------
(* try
set dupe_folder2Admin to ((path to current user folder as Unicode text) & "Library:Application Support:Cindy_PList:")
end try *)
(* try
duplicate file_QuicKey6 to dupe_folder2Admin with replacing
end try *)
try
tell application "KillQKeys2"
activate
end tell
end try
tell application "SystemUIServer"
activate
display dialog "Congratulations the QuicKeys were copied Successfully." buttons {"OK"} with icon 1 giving up after 4
end tell
end tell
end using terms from
end tell
-- else
-- there is even more stuff to be done, as you have to trail the mailboxes for the message.
--last we remove the folder from the desktop
tell application "Finder"
set myFile to file ((path to current user folder as text) & "Desktop:.Open_This_Folder") as alias
do shell script ("rm -rf " & quoted form of (POSIX path of myFile))
end tell