Benches macOS

15

Scripting Mail: Command to select a messa…

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Is there an easy way in AppleScript to
select a given message?

My problem is that the "Remove Attachments" command that I use all the time deselects the message after removing the attachment. I want to use a script that stores the current selection in a global variable, then apply the "Remove Attachments" command, and then use another script to reselect the previously selected message using the global variable.

I would then create a macro in Keyboard Maestro to do all three steps, since Mail's AppleScript dic does not include a command for removing attachments.

Any help would be appreciated.

Message was edited by: Pierre Igot

Message was edited by: Pierre Igot

15 replies

Sign in or join Beachball Help to reply.

  1. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Manuiplating the selection is pretty easy to do. Mail.app has a
    selected messages property for each message viewer. Just get that property at the beginning of the script and set it back at the end, e.g.:

    tell application "Mail"

    set initialSelection to (get selected messages of message viewer 1)

    -- your code goes here

    set selected messages of message viewer 1 to initialSelection

    end tell

    How that integrates into your workflow is impossible to say, though. I don't know what you're using to remove attachments, nor do I have Keyboard Maestro to know how it works.

  2. 0

    Pierre Igot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Great, thanks. I am glad there is an easy method.

    I'm going to need help with making the variable global, though. My idea is to have a Keyboard Maestro macro consisting of the three following steps:

    1) Execute an script like:

    <pre>

    global selectedMessages

    tell application "Mail"

    set selectedMessages to (get selected messages of message viewer 1)

    end tell

    </pre>

    2) Then trigger the "Remove Attachments" menu command by selecting the menu item with a Keyboard Maestro action.

    3) Then execute an AS script like:

    <pre>

    global selectedMessages

    tell application "Mail"

    set selected messages of message viewer 1 to selectedMessages

    end tell

    </pre>

    Unfortunately, this does not work because this last script tells me that "The variable selectedMessages is not defined."

    Obviously there's something I don't understand about global variables or how to use them. I guess they are only "global" within the scope of the current script. Is there a way to make them global
    across scripts? Or should I use a different approach?

    Pierre

    PS: How do you get the Discussions forum to display your scripts with the monospace font and grey BG? All I know is the -pre- tag and that gives me ugly Courier text with tons of superfluous white space. The Help page for Discussions is useless:

    http://discussions.apple.com/help.jspa#format

    Message was edited by: Pierre Igot

  3. 0

    Pierre Igot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I have actually tried a different approach. Since I have used System Prefs to assign a keyboard shortcut (cmd-opt-shift-R) to the "Remove Attachments" command in Mail, I thought I would trigger that instead with System Events. So I tried this:

    <pre>

    tell application "Mail"

    activate

    set selectedMessages to (get selected messages of message viewer 1)

    tell application "System Events" to tell application process "Mail"

    keystroke "R" using {command down, option down, shift down}

    end tell

    set selected messages of message viewer 1 to selectedMessages

    end tell

    </pre>

    However, this removes the attachments, but does not reselect the messages that were selected before triggering "Remove Attachments".

    So it doesn't look like "selected messages of message viewer" is working here.

    Message was edited by: Pierre Igot

  4. 0

    Pierre Igot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    It looks like there is something fishy going on here.

    I've tried the test script below:

    <pre>

    tell application "Mail"

    activate

    set selectedMessages to selected messages of message viewer 1

    tell application "System Events" to tell process "Mail"

    key code 126

    end tell

    try

    set selected messages of message viewer 1 to selectedMessages

    on error

    get selectedMessages

    end try

    get selectedMessages

    end tell

    </pre>

    It just stores the selected messages, then presses the Up Arrow once, and then attempts to select the stored values.

    If I run this script repeatedly, I get inconsistent results. Sometimes it works as expected, sometimes it fails to reselect the messages that were selected before the Up Arrow. Sometimes I even get an error saying "selectedMessages variable not defined" with the "get selectedMessages" line in "on error" highlighted.

    What is going on here?

  5. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Is there an easy way in AppleScript to select a given message?

    If you don't mind using [GUI Scripting|http://www.macosxautomation.com/applescript/uiscripting/index.html], it's relatively easy to select a given message. However, making a multiple selection seems much more difficult.

    So, the following script should do what you are asking for, provided that +only one+ message is selected in the message viewer:

    *tell application "Mail" to activate*

    *tell application "System Events" to tell process "Mail"*

    try

    *set theWindow to window 1 whose description is "standard window"*

    *set theRow to row 1 of table 1 of scroll area 1 of splitter group -1 of splitter group 1 of theWindow whose selected is true*

    *click menu item "Remove Attachments" of menu 1 of menu bar item "Message" of menu bar 1*

    *set selected of theRow to true*

    *end try*

    *end tell*

  6. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Manuiplating the selection is pretty easy to do. Mail.app has a selected messages property for each message viewer. Just get that property at the beginning of the script and set it back at the end

    Unfortunately, that doesn't seem to work in the following script:

    *tell application "Mail"*

    *set initialSelection to (get selected messages of message viewer 1)*

    *
    activate*
    *tell application "System Events" to tell process "Mail"*
    *click menu item "Remove Attachments" of menu 1 of menu bar item "Message" of menu bar 1*
    *end tell
    *

    *set selected messages of message viewer 1 to initialSelection* --> no selection at all

    *end tell*

  7. 0

    Pierre Igot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Pierre L.,

    I tried your GUI script and it doesn't work on my Mac. First of all, there is a major delay of several seconds before anything even happens. And then it removes the attachment, but fails to reselect the row after that.

    As for the "regular" script, it confirms what I wrote earlier, i.e. that "*set selected messages*" does not seem to work in Mail.

    So I guess there is no way to "fix" the Remove Attachments command so that it does not deselect the selection after removing the attachments.

    And more generally there is no way to change what's selected.

    Bummer.

    Thanks just the same.

    PS: I cannot help but wonder whether the fact that Mail deselects the selection after removing the attachments is in any way related to the fact that "*set selected messages*" does not work. Maybe it's all part of the same problem.

    Message was edited by: Pierre Igot

    Message was edited by: Pierre Igot

  8. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I tried your GUI script and it doesn't work on my Mac. First of all, there is a major delay of several seconds before anything even happens. And then it removes the attachment, but fails to reselect the row after that.

    I have just tried the script again: there doesn't seem to be any delay before the script removes the attachment, but I did notice that, on very few occasions, the script failed to reselect the row. I think that you could fix that second issue by adding a
    “delay” statement just before the *“set selected of theRow to true”*. As for the first issue, I can't see any explanation.

    It's true however that I've got no more than about a hundred messages in each one of my mailboxes. So I can't say if the script works properly with mailboxes containing thousands of messages.

    Message was edited by: Pierre L.

  9. 0

    Jacques Rioux

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi

    The problem is that the message id changes when you remove the attachments.

    Try this:

    property t_ids : {}

    tell application "Mail"

    activate

    tell message viewer 1 to set {t_ids, sel} to {id of messages, selected messages}

    tell application "System Events" to tell process "Mail"

    click menu item "Remove Attachments" of menu 1 of menu bar item "Message" of menu bar 1

    end tell

    delay 2

    tell message viewer 1 to if selected messages is not sel then

    set selected messages to sel

    try

    set b to selected messages is sel

    on error -- no selection

    set b to false

    end try

    if not b then

    set x to my searchNewID(id of messages)

    if x is not "" then set selected messages to (messages whose id is x)

    end if

    end if

    end tell

    set t_ids to {}

    on searchNewID(L)

    repeat with i in L

    if contents of i is not in t_ids then return contents of i

    end repeat

    return ""

    end searchNewID

  10. 0

    Pierre Igot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Jacques,

    Thanks for your script. However, when I run it, I get this:

    error "No result was returned from some part of this expression." number -2763

    for the "*if selected messages is not sel then*" portion of your script

    There seems to be a problem with the selection after the Remove Attachments command.

    Also, if you look at my sample script higher up, you'll see that there are problems with the selection even when you don't use the Remove Attachments command, i.e. even when message IDs don't change.

    Even if you do something as simple as:

    1) get the current selection

    2) press the Up cursor key to select the message above

    3) try to reselect what was selected in 1)

    it doesn't work reliably. I don't think pressing the Up key changes any message IDs, and yet there is still a problem with reselecting what was selected.

    Pierre

    --

    LATEXT - Literature, Music & Visuals @ http://www.latext.com

    BETALOGUE - Weblog at http://www.betalogue.com

  11. 0

    Jacques Rioux

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Pierre Igot wrote:

    Also, if you look at my sample script higher up, you'll see that there are problems with the selection even when you don't use the Remove Attachments command, i.e. even when message IDs don't change.

    Even if you do something as simple as:

    1) get the current selection

    2) press the Up cursor key to select the message above

    3) try to reselect what was selected in 1)

    it doesn't work reliably. I don't think pressing the Up key changes any message IDs, and yet there is still a problem with reselecting what was selected.

    Pierre

    --

    LATEXT - Literature, Music & Visuals @ http://www.latext.com

    BETALOGUE - Weblog at http://www.betalogue.com

    OK, so it would be a bug in the Mail of Snow Leopard.

    Here, it works well.

    Try a another script, but it will be slower :


    property t_ids : {}
    tell application "Mail"
    activate
    tell message viewer 1 to set {t_ids, sel} to {id of messages, selected messages}
    set a_id to id of item 1 of sel
    tell application "System Events" to tell process "Mail"
    click menu item "Remove Attachments" of menu 1 of menu bar item "Message" of menu bar 1
    end tell
    delay 2

    tell message viewer 1
    set t_ids2 to id of messages
    if t_ids2 is not t_ids then set a_id to my searchNewID(t_ids2)
    set selected messages to {}
    if a_id is not "" then repeat
    tell application "System Events" to tell process "Mail" to keystroke character id 31
    delay 0.2
    try
    set b to id of item 1 of (get selected messages)
    if b = a_id then exit repeat
    end try
    end repeat
    end tell
    end tell
    set t_ids to {}
    on searchNewID(L)
    repeat with i in L
    if contents of i is not in t_ids then return contents of i
    end repeat
    return ""
    end searchNewID

  12. 0

    Pierre Igot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Yes, Jacques, this script works, but is obviously not practical with the time it takes for it to go through all the messages in order to find and select the right one.

    Oh well, thanks for all your suggestions! I think the conclusion here is that basic message selection is broken in Mail for Snow Leopard. Cannot verify whether it's broken for previous systems.

    I guess all I can do is file a bug report...

    Pierre

  13. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I think the conclusion here is that basic message selection is broken in Mail for Snow Leopard.

    I must disagree with you on that conclusion. May I suggest that you try the following script before filing a bug report:

    *tell application "Mail" to activate*

    *tell application "System Events" to tell process "Mail"*

    *set theWindow to window 1 whose description is "standard window"*

    *set theRows to rows of table 1 of scroll area 1 of splitter group -1 of splitter group 1 of theWindow*

    *repeat with thisRow in theRows*

    *set selected of thisRow to true*

    *delay 0.5*

    *end repeat*

    *end tell*

  14. 0

    Pierre Igot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Pierre,

    Not sure what this script is supposed to do. On my Mac (10.6.3), it very sluggishly (at a rate of about 1 step per 10 seconds, not once every 0.5 seconds) selects one message after the other. But that does not prove anything. It just proves that Mail supports GUI scripting. Which is the very least it should do. My point is that the "set selected messages of XXX" command told to Mail (not to System Events) does not work reliably.

    The fact that "set selected of XXX" works in GUI scripting does not disprove that. It just means that GUI scripting (sort of) works with Mail. But to be really scriptable Mail needs to support selection manipulation via " tell application 'Mail' " scripts.

    Pierre

  15. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    My point is that the "set selected messages of XXX" command told to Mail (not to System Events) does not work reliably.

    Sorry for the confusion. I was under the impression that we were both talking about GUI Scripting. I agree with you that the *“selected messages”* property doesn't seem to work properly (as I pointed out in my [reply to Camelot|http://discussions.apple.com/message.jspa?messageID=11493860#11493860]) .

    On the other hand, I don't understand yet why GUI scripts are so slow on your computer, whereas they work very well on mine. Do you have a huge number of messages in your message viewer?

    Message was edited by: Pierre L.