Previous  |  Main  |  Next
AppleScript Logo

Get Selection

Script for: Mailsmith 1.x, 2.x (OS 9 & OS X)
Copyright: Matthias Steffens, use at your own risk!
Description: Script framework that will return the current selection as list
as well as the type of the selection. Use this routine within your
own scripts...

Background:
  It's simple to get the current selection from Mailsmith:
      tell application "Mailsmith 1.1" to get selection
  But depending on your selection you'll get different result types returned: If you've selected a single message or mailbox you'll get a reference to this particular message or mailbox. In contrast to that, if multiple messages/mailboxes are selected you'll get a list of references back. Again, if there's some text selected instead Mailsmith will return a string. And even worse, if there's no selection at all you'll be faced with an error.
  All scripts that deal with selected objects have to deal with that sort of problems. They have to implement code to account for different kinds of selections.
  But instead of re-inventing the wheel for every new script it's a good idea to build one routine that is able to manage all kinds of selections and that will produce a standardized output (which can be easily dealt with). This is exactly what this script framework does.
 
Routine Output:
  The routine will return three variables, selType, selList and selString:
selType:
  This variable describes the type of the selection. Depending on your selection, selType will be one out of the following identifier strings:
  • single message
  • multiple messages
  • single mailbox
  • multiple mailboxes
  • text
  • undef
Note: The "single message" identifier will get returned also if any To-address or enclosure is selected. With any From-address, subject or notes being selected "text" will be returned as identifier. However, the reference to the related message can be always obtained from selList (see below).

The "undef" identifier will be returned if it is either not possible to resolve the selection or if there's simply no selection. Specifically, "undef" will be returned in the following cases:
  • no open window
  • type of window is not message window, mail list window, mailbox list or mail browser
  • focus ring is within a mailbox list but either nothing or "(outgoing mail)" is selected
  • focus ring is within a message list but nothing is selected
  • focus ring is within text pane but without any (single) message being selected
selList:
  If selType isn't "undef", selList will always contain a list of references to the selected item(s).
selString:
  This variable will contain any selected text or the body text of any single selected message (otherwise it will be empty, i.e. ""). Note that it still might be empty, if the cursor is positioned within some text but no text is actually selected.
 
Notes:
  This routine won't do anything if the front window isn't one of the following:
  • message window
  • mail list window
  • mailbox list
  • mail browser
  Query results windows are not supported yet since their behaviour (compared to the above window types) is somewhat different if something other than mailbox or message is selected.
  The "Label Selected Messages" script gives an example how to implement this routine.
Necessary: None
Download: Get_Selection.hqx
History: v1.2  -  still uses raw events since code execution time is notably faster;
            supports Mailsmith 2.x under OS X
v1.1  -  uses raw events for window type detection; this circumvents a bug where
            'class of window 1 as string' (when called from within Mailsmith) will
            return raw events instead of strings
v1.0  -  initial version released on this site
The Script:
set {selType, selList, selString} to my GetSel()
-- now do your stuff depending on selType and selList...


on GetSel() -- this routine will return the selection as well as the type of the selection:
	try
		tell application "Mailsmith"
			if exists window 1 then
				set winType to class of window 1
				set Proceed to true
			else
				error "No open window!"
			end if
		end tell
		
		if Proceed then
			-- check if the frontmost window is "message window", "mail list window", "mailbox list" or "mail browser window":
			-- Notes: - the use of raw events for window type detection was introduced in v1.1 due to a bug in Mailsmith 1.x
			--              but is kept for Mailsmith 2.x since code execution is notably faster!
			--           - mail browser window is «class MBrw» in Mailsmith 2.x, «class Post» in Mailsmith 1.x!
			--           - mail list window is «class MBox» in Mailsmith 2.x, «class MMBx» in Mailsmith 1.x!
			if winType is in {«class mWnd», «class mLst», «class MBox», «class MMBx», «class MBrw», «class Post»} then
				-- get the current selection as list:
				try
					tell application "Mailsmith"
						set sel to selection -- 'set sel to selection as list' bombs Mailsmith 1.1.6 if the selection's class is 'string'!!
						set selList to sel as list
					end tell
				on error -- errors if an address or enclosure is selected
					-- check if the frontmost window is "message window", "mail list window" or "mail browser window":
					if winType is in {«class mWnd», «class mLst», «class MBrw», «class Post»} then --exclude «class MBox» / «class MMBx» ("mailbox list") here
						tell application "Mailsmith"
							set selList to (message of window 1) as list -- attempt to get the message to which the selected item (e.g. address or enclosure) belongs to
						end tell
					else -- "mailbox list" does not contain any message info
						error number -1700
					end if
				end try
				set ct to length of selList -- get the number of list items
				
				tell application "Mailsmith"
					if ct is 0 then -- focus ring is within a message list but nothing is selected
						set selList to {}
						set selType to "undef"
						set selString to ""
					else -- there's something selected...
						set selType to class of item 1 of selList -- get the type of the selection
						
						if selType is string then -- cursor is within text pane
							-- errors on following line if focus ring is within text pane but without any (single) message being selected:
							set selList to (message of window 1) as list -- get the message to which the selected string belongs to
							set selType to "text"
							set selString to selection as text -- get the selected string
							
						else if selType is message then -- one or more messages selected
							if ct = 1 then -- single message selected
								set selType to "single message"
								set selString to (item 1 of selList) as text -- get the body text of the currently selected message
							else -- ct > 1, i.e. multiple messages selected
								set selType to "multiple messages"
								set selString to ""
							end if
							
						else if selType is mailbox then -- one or more mailboxes selected
							if ct = 1 then -- single mailbox selected
								set selType to "single mailbox"
								set selString to ""
							else -- ct > 1, i.e. multiple mailboxes selected
								set selType to "multiple mailboxes"
								set selString to ""
							end if
							
						else -- class of selection is not string, message or mailbox
							error
						end if
					end if
				end tell
			else
				error "Can't get selection, since front window isn't one of the following:" & return & return & ¬
					"    message window" & return & "    mail list window" & return & "    mailbox list" & return & "    mail browser window"
			end if
		end if
	on error ErrTxt number ErrNum
		set selList to {}
		set selType to "undef"
		set selString to ""
		
		if ErrNum is not in {-128, -1711} then -- -128: "User canceled", -1711: "User canceled out of wait loop for reply or receipt."
			if ErrNum is in {-1700, -1728} then
				-- display dialog "Can't get the selection!" buttons {"Cancel"} default button 1 with icon caution
			else
				display dialog ErrTxt & return & return & "(" & ErrNum & ")" buttons {"Cancel"} default button 1 with icon caution
			end if
		end if
	end try
	
	return {selType, selList, selString}
end GetSel

 


Contact: Matthias Steffens  |  Previous  |  Main  |  Next  |  Last Updated: 30-May-03