Wednesday, January 24, 2018

#1 2006-06-10 04:37:16 pm

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

acting on selected messages in Mail

Hi,

For some reasons I won't detail here, I need to write a short applescript to save an alias (in the common Finder meaning) of the selected message in Mail (or if it's not feasible, just rename an .elmx mail file with it's subject, or author name for ex). I'm new to applescripting, and thought I could start from the "Get source of selected message" applescript provided by Apple in the "Mails scripts/Script Menu", as a good starting point, to learn how to act on a selected message. Unfortunately, I can't even get this script to work. It just does nothing. What am I missing?
To me, it looks like the "on perform mail action with messages theSelectedMessages" doesn't work as it should. As far I understand, this is the key to act on selected messages, so I'm basically stuck.
Any help is welcome.

Thanks a lot.


One mans trash is another mans treasure

Offline

 

#2 2006-06-11 10:06:02 pm

capitalj
Member
Registered: 2006-02-15
Posts: 420

Re: acting on selected messages in Mail

So this looks challenging.

Applescript:

tell application "Mail"
   set theMessage to selection
   end tell

This wil return {message id xxxx of mailbox "your mailbox here" of account "your account here"} and xxxx is the .emlx needing an alias.

I guess the next step woud be to get xxxx into somethig like

Applescript:

tell application "Finder" to make new alias to file "xxxx.emlx" of folder "Messages" of folder "your mailbox here.imapmbox" of folder "Mac-whatever"... --and so on

It would probably help to add something more recognizable like "subject_xxxx.emlx"

I haven't gotten very far yet, but it looks like it's possible... um, I think.

j

Last edited by capitalj (2006-06-12 02:18:15 pm)


Filed under: mail, Finder

Offline

 

#3 2006-06-11 10:38:19 pm

capitalj
Member
Registered: 2006-02-15
Posts: 420

Re: acting on selected messages in Mail

EDIT: avoid late night scripting. I inadvertantly left an elmx file selected - back to the drawing board. The following is not the breakthrough I thought it was.

I am giddy with the progress I've made - after much searching I found that this works on a selected message:

Applescript:

tell application "Finder"
   set myMessage to (get selection as list)
       make new alias to myMessage
end tell

I only ran it through Script Editor so far. It doesn't rename the file yet and the alias is on the desktop, not saved in a special folder.

j

Last edited by capitalj (2006-06-11 11:15:35 pm)


Filed under: Finder

Offline

 

#4 2006-06-11 11:23:01 pm

capitalj
Member
Registered: 2006-02-15
Posts: 420

Re: acting on selected messages in Mail

Before I give up for now, I had a thought.

Could you use a rule to move the messages to a mailbox and use a folder action to 1) make aliases of added files and 2) move the aliases to another folder?

Offline

 

#5 2006-06-12 12:00:35 pm

capitalj
Member
Registered: 2006-02-15
Posts: 420

Re: acting on selected messages in Mail

I'm still experimenting a bit, mostly out of curiosity.

I don't know it all the messages are stored in the same place, but this is the beginning of setting the path to the .emlx file. Eventually it would include  & {":xxx.emlx."} although I haven't worked out that part.

Do you want aliases of files scattered throughout mailboxes, or will they all be in the same place? Knowing that would simplify things.

j

Last edited by capitalj (2006-06-12 02:18:55 pm)

Offline

 

#6 2006-06-12 12:35:25 pm

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: acting on selected messages in Mail

Hi,

Thanks for the help.

First of all: I've meanwhile understood why I couldn't get the "get source of slected message " apple script to run: I hadn't understood that it had to be launched by a mail rule..which wasn't exactly what I wanted anyway.

Well....back to my main concern, here is what I've written so far, with help of your advices:

tell application "Mail"
    set theMessage to selection
    set eachMessage to item 1 of theMessage
    set theId to id of eachMessage
    my makealias(theId)
end tell

on makealias(theId)
    set rootPath to "Macintosh HD:Users:"
    set locPath to "mylogin:Documents:Taches:"
    set theDefaultPath to rootPath & locPath
    repeat
        display dialog "Enter path:" default answer theDefaultPath buttons {"Cancel", "OK"} default button 2
        set the theAliasPath to the text returned of the result
        if the theAliasPath is not "" then exit repeat
    end repeat
   
    set theDefaultName to (theId as string) & "_.emlx"
    repeat
        display dialog "Enter alias name:" default answer theDefaultName buttons {"Cancel", "OK"} default button 2
        set the theAliasName to the text returned of the result
        if the theAliasName is not "" then exit repeat
    end repeat
   
    set theLocation to "Macintosh HD:Users:mylogin:Library:Mail:Mailboxes:Taches.mbox:Messages:"
    set theTrueName to (theId as string) & ".emlx"
    set theTrueEmlx to theLocation & theTrueName
   
    tell application "Finder"
        make new alias to file theTrueEmlx at theAliasPath with properties {name:theAliasName}
    end tell
end makealias

So basically, to simplify the pb, one prerequisite of this script is to have the original .emlx files in one separate mailbox (not a big pb).
So: the script runs now and does what I want. Some cosmetics might be nice: instead of having this more or less painfull succession of dialog boxes to set the folder and path name of the alias, the top nice feature would be to get the usual 'finder drawer' that goes down upon a standard "Save as:" request. Or if not possible at least just one single dialog box with the two 'folder path' and 'file name' entry fields. (I could also do it in one single line, thoug, but I don't thing it's very nice).
But this is not really necessary (just my curiosity to learn more about applescript, and my taste for more nicer things). Anyway, the dialog asking for the folder path is not absolutely necessary too, since, as you mentionned in your e-mail, I will mainly (if not always) gather all aliases in one separate folder.

Thanks for the help
Sylvaine

PS: i've tried to investigate your "(get selection to list)" idea, to try to write the script in a more elegant way (mine is for sure quite painfull), but didn't go very far.


One mans trash is another mans treasure


Filed under: mail, Finder

Offline

 

#7 2006-06-12 12:53:04 pm

capitalj
Member
Registered: 2006-02-15
Posts: 420

Re: acting on selected messages in Mail

Am I glad to see your post - I couldn't figure this out to save my life (must be overtired):

Applescript:

tell application "Mail"
   set theMessage to selection
   set eachMessage to item 1 of theMessage
   set theId to id of eachMessage

The "get selection as list" bit works from Finder on a selected .emlx. I wanted to use the id as a variable to put in the path to the .emlx and use Finder to select it and make the alias. I also wanted to use the message subject as a variable to add to the name of the alias. The script would be shorter, but with lots of variables -  especially if the files were in different mailboxes - but no dialog boxes.

I'm going to keep trying to do it the hard way, but I'm glad you have a script that works.

j

Last edited by capitalj (2006-06-12 02:20:18 pm)


Filed under: mail

Offline

 

#8 2006-06-12 02:26:18 pm

capitalj
Member
Registered: 2006-02-15
Posts: 420

Re: acting on selected messages in Mail

I've made some progress on naming the alias - will this help?

Applescript:


tell application "Mail"
   set theMessage to selection
   set theMessage to item 1 of theMessage
   set theId to id of theMessage
   tell theMessage
       set theSubject to subject & "_"
   end tell
end tell


set theFile to (theId & ".emlx") as string
set aliasName to theSubject & theFile
display dialog aliasName--just for show


Filed under: mail

Offline

 

#9 2006-06-12 02:55:08 pm

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: acting on selected messages in Mail

Yes, thanks, that great. I'll tend to put the ID first in the alias name, to keep it somehow sorted (unless the id is not monotonically increasing, but up to what I've seen, it looks like it does monotonically increase with time).
At some point I'd thought of puting the received date in the aliasname (following this idea of natural sorting), but that may be a bit misleading and not 100% safe, if the same [subject,author,date] triplet (or just [subject,date] couple) occurs several times one day. It should be rare though, for the use I'll had, but one a general way, that's possible
-At this point, you may be curious to know what's the whole thing's good for: in my job, I'm getting 95% of my collaborators' requests per email, as most people, and I'd like to maintain a simple omnioutliner list of Todos, with using the ability of Omnioutliner to incorporate links toward files, real or aliases. Then, in each todo line, I'll have a live link pointing to the alias of the e-mail file, itself pointing to the true e-mail. That's convenient for reference, but also when the task is done: I'll just have to click on the link to get the e-mail in Mail, and be ready to reply that my job is done... One question may arise: what's the point of creating an alias, and not copying the raw e-mail somewhere, with any name: well, this would break the message from its content, and then it cannot be read anymore...

Cheers
S.


One mans trash is another mans treasure

Offline

 

#10 2006-06-12 04:16:42 pm

capitalj
Member
Registered: 2006-02-15
Posts: 420

Re: acting on selected messages in Mail

I was a little curious. Its a good idea. You could also duplicate the .emlx (I already experimented with that) but that is more difficult and takes up a little more space.

If I figure out how to deal with path problems, it could work either way. I'm still working on it because it's interesting, plus it might be nice to be able to automatically rename the original .emlx files or add Spotlight comments. Once, I was having hardware problems and an Apple tech told me to pull my mailboxes and files out and put them back later - very bad idea. I had to sort hundreds of e-mails with only a number for identification.

j

Offline

 

#11 2006-06-12 07:30:08 pm

capitalj
Member
Registered: 2006-02-15
Posts: 420

Re: acting on selected messages in Mail

Have you finished your script? I've tried a few things and I'm just about out of ideas, although I've learned some very useful things to use in other ways.

I was reading about file searches - one place is http://applescriptsourcebook.com/articles/149_0_3_0_C/ - but Mail folders are uncooperative; I've made no headway on fully automating the process.

Largely for practice, I tried to adapt your script to have a choice of using default settings - one dialog box - or your makealias handler. It's untested.

Applescript:


tell application "Mail"
   set theMessage to selection
   set theMessage to item 1 of theMessage
   set theId to id of theMessage
   tell theMessage
       set theSubject to "_" & subject
   end tell
end tell
myPathResponse(theId, theSubject)--Edit put this line in the wrong place the first time
-------------------------
on myPathResponse(theId, theSubject)
   
   set theResponse to button returned of (display dialog "Use default paths?" buttons {"Cancel", "No", "Yes"} default button "Yes")
   if theResponse is "Yes" then
       myDefaultPath(theId, theSubject)
   else
       if theResponse is "No" then
           myMakeAlias(theId)
       end if
   end if
   
end myPathResponse
-------------------------
on myDefaultPath(theId, theSubject)
   
   set theTrueName to (theId as string) & ".emlx"
   set theLocation to "Macintosh HD:Users:mylogin:Library:Mail:Mailboxes:Taches.mbox:Messages:"
   set theTrueEmlx to theLocation & theTrueName
   
   set aliasName to theTrueName & theSubject
   set theAliasPath to "Macintosh HD:Users:mylogin:Documents:Taches:"
   
   tell application "Finder"
       make new alias to file theTrueEmlx at theAliasPath with properties {name:aliasName}
   end tell
end myDefaultPath
-------------------------
on myMakeAlias(theId)
   set rootPath to "Macintosh HD:Users:"
   set locPath to "mylogin:Documents:Taches:"
   set theDefaultPath to rootPath & locPath
   repeat
       display dialog "Enter path:" default answer theDefaultPath buttons {"Cancel", "OK"} default button 2
       set the theAliasPath to the text returned of the result
       if the aliasPath is not "" then exit repeat
   end repeat
   
   set theDefaultName to (theId as string) & "_.emlx"
   repeat
       display dialog "Enter alias name:" default answer theDefaultName buttons {"Cancel", "OK"} default button 2
       set the aliasName to the text returned of the result
       if the aliasName is not "" then exit repeat
   end repeat
   
   set theLocation to "Macintosh HD:Users:mylogin:Library:Mail:Mailboxes:Taches.mbox:Messages:"
   set theTrueName to (theId as string) & ".emlx"
   set theTrueEmlx to theLocation & theTrueName
   
   tell application "Finder"
       make new alias to file theTrueEmlx at theAliasPath with properties {name:aliasName}
   end tell
end myMakeAlias

Last edited by capitalj (2006-06-14 07:33:39 pm)


Filed under: mail, Finder

Offline

 

#12 2006-06-15 02:47:45 pm

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: acting on selected messages in Mail

Hi,

Thanks for this modif. I've added a few things:
    -to avoid the potential trap of getting the ":" character into the subject string (--> causes a bug)
    -to remove the spaces, which I tend to avoid in file names, although this is not a bug by Apple's standards...

This time, I think that's it....

Bests
S.

Applescript:



tell application "Mail"
   set theMessage to selection
   set theMessage to item 1 of theMessage
   set theId to id of theMessage
   set theSubject to subject of theMessage
   set theSender to sender of theMessage
end tell
set theSubject to replace_chars(theSubject, ":", "_")
set theSubject to replace_chars(theSubject, " ", "_")
myPathResponse(theId, theSubject)

-------------------------
on myPathResponse(theId, theSubject)
   
   set theResponse to button returned of (display dialog "Use default paths?" buttons {"Cancel", "No", "Yes"} default button "Yes")
   if theResponse is "Yes" then
       myDefaultPath(theId, theSubject)
   else
       if theResponse is "No" then
           myMakeAlias(theId)
       end if
   end if
   
end myPathResponse
-------------------------
on myDefaultPath(theId, theSubject)
   
   set theTrueName to (theId as string) & ".emlx"
   set theLocation to "Macintosh HD:Users:mylogin:Library:Mail:Mailboxes:Taches.mbox:Messages:"
   set theTrueEmlx to theLocation & theTrueName
   set aliasName to theTrueName & "_" & theSubject & " .emlx"
   set theAliasPath to "Macintosh HD:Users:mylogin:Documents:Taches:"
   tell application "Finder"
       try
           make new alias to file theTrueEmlx at theAliasPath with properties {name:aliasName}
       end try
   end tell
end myDefaultPath
-------------------------
on myMakeAlias(theId)
   set rootPath to "Macintosh HD:Users:"
   set locPath to "mylogin:Documents:Taches:"
   set theDefaultPath to rootPath & locPath
   repeat
       display dialog "Enter path:" default answer theDefaultPath buttons {"Cancel", "OK"} default button 2
       set the theAliasPath to the text returned of the result
       if theAliasPath is not "" then exit repeat
   end repeat
   set theAliasPath to theDefaultPath
   
   set theDefaultName to (theId as string) & "_.emlx"
   repeat
       display dialog "Enter alias name:" default answer theDefaultName buttons {"Cancel", "OK"} default button 2
       set the aliasName to the text returned of the result
       if the aliasName is not "" then exit repeat
   end repeat
   
   set theLocation to "Macintosh HD:Users:mylogin:Library:Mail:Mailboxes:Taches.mbox:Messages:"
   set theTrueName to (theId as string) & ".emlx"
   set theTrueEmlx to theLocation & theTrueName
   tell application "Finder"
       try
           make new alias to file theTrueEmlx at theAliasPath with properties {name:aliasName}
       end try
   end tell
end myMakeAlias
-------------------------
on replace_chars(this_text, search_string, replacement_string)
   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 ""
   return this_text
end replace_chars


One mans trash is another mans treasure


Filed under: mail, Finder

Offline

 

#13 2006-06-15 02:54:42 pm

capitalj
Member
Registered: 2006-02-15
Posts: 420

Re: acting on selected messages in Mail

Nice

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)