• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Mail navigation script
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mail navigation script


  • Subject: Re: Mail navigation script
  • From: Axel Luttgens <email@hidden>
  • Date: Sat, 03 Nov 2012 20:28:18 +0100

Le 2 nov. 2012 à 08:21, Dave C a écrit :

> Hello,
> I am trying to develop (well, actually modify) a script to provide navigation in an Apple Mail mailbox.
>
> When a message window is open, Mail does not provide a means to move to the next message without doing this little dance:
>
> 1. close message window
> 2. press arrow key to navigate to next message
> 3. press Return
>
> These have been combined into a script (below), but it navigates to the next unread message. I want to navigate to the next message, regardless read OR unread.
>
> Any help getting me pointed in the right direction will be gratefully appreciated.
>
> Thanks,
> Dave
>
> ---- begin script ----
> tell application "Mail" to try
> 	if (count of windows) > 1 then
> 		tell window 1 to close
> 		tell message viewer 1 to set selected messages to {first message of beginning of (get selected mailboxes) whose read status is false}
> 		tell message viewer 1 to open selected messages
> 	end if
> on error
> 	beep
> end try
> ---- end script ----

Hello Dave,

This is an interesting one. :-)

Consecutive runs of your script rely exclusively on the state maintained by Mail: the selected mailboxes in a message viewer and the read status of the messages.

So, I've tried to do the same, but now by relying on the selected mailboxes in a message viewer and the selected message.

You've already got several replies, so this one should just be viewed as an additional input.

Axel


property msgs : missing value

tell application "Mail"

	-- Get current front message viewer.
	-- No need to go further if there is none.
	set frontMessageViewer to my getFrontMessageViewer()
	if frontMessageViewer is missing value then return

	tell frontMessageViewer

		-- The message viewer's messages elements seem to provide all messages of all selected
		-- mailboxes, in the same order as the one displayed in the GUI.

		-- Take a local copy of message viewer's messages; this will allow for speedier operations.
		-- No need to go further if none (i.e. no selected mailbox).
		set my msgs to messages
		set msgCount to (length of my msgs)
		if msgCount is 0 then return

		-- Close the window that has possibly been opened on a previous run.
		-- Not sure how to do this reliably, without taking the risk to close windows unrelated
		-- to the navigation performed thru this script...

		if (count of selected messages) > 0 then -- At least one message was message upon entry.

			-- A multiple messages selection may happen either because the user has selected
			-- multiple messages, or because a closed thread has been selected. In all cases,
			-- we'll pick a single message as reference, as the current message.
			if sorted ascending then
				set currMsg to first item of (get selected messages)
			else
				set currMsg to last item of (get selected messages)
			end if

			-- Locate the current message in the viewer's messages list, i.e. get its index.
			-- There's possibly some room for optimization here.
			repeat with i from 1 to msgCount
				if item i of my msgs is currMsg then
					set currMsgIndex to i
					exit repeat
				end if
			end repeat

			-- Derive the index for the candidate next message.
			if sorted ascending then
				if currMsgIndex < msgCount then
					set nextMsgIndex to currMsgIndex + 1
				else
					set nextMsgIndex to 1
				end if
			else
				if currMsgIndex > 1 then
					set nextMsgIndex to currMsgIndex - 1
				else
					set nextMsgIndex to msgCount
				end if
			end if

		else -- No message was selected upon entry.

			-- Define the candidate next message's index.
			if sorted ascending then
				set nextMsgIndex to 1
			else
				set nextMsgIndex to msgCount
			end if

		end if

		-- And here's why one has to consider a "candidate next message": if that
		-- message belongs to a closed thread, one should select all messages of
		-- that thread. But this doesn't seem to be functional.
		-- As a result, one has to be prepared to skip several consecutive messages...
		if sorted ascending then
			repeat with i from nextMsgIndex to msgCount
				set selected messages to item i of my msgs
				if selected messages is not missing value then exit repeat
			end repeat
		else
			repeat with i from nextMsgIndex to 1 by -1
				set selected messages to item i of my msgs
				if selected messages is not missing value then exit repeat
			end repeat
		end if

		-- Open a window for the message that has been selected (if any).
		-- Since I don't know how to reliably close it upon the next run... ;-)
		-- So, I'll assume that the preview pane is open and display the
		-- selected message.

	end tell

end tell

-- Perhaps too careful, but may prove important if windows are to be opened/closed
-- from within this script.
on getFrontMessageViewer()
	local frontMessageViewer, messageViewer
	tell application "Mail"
		if (count of message viewers) is 0 then return missing value
		set frontMessageViewer to message viewer 1
		repeat with messageViewer in message viewers
			if index of window of messageViewer < index of window of frontMessageViewer then
				set frontMessageViewer to contents of messageViewer
			end if
		end repeat
		return frontMessageViewer
	end tell
end getFrontMessageViewer





 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden


  • Follow-Ups:
    • Re: Mail navigation script
      • From: Brian Christmas <email@hidden>
References: 
 >Mail navigation script (From: Dave C <email@hidden>)

  • Prev by Date: Re: How can I control invisible files
  • Next by Date: Re: How can I control invisible files
  • Previous by thread: Re: Mail navigation script
  • Next by thread: Re: Mail navigation script
  • Index(es):
    • Date
    • Thread