Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Cannot open "On My Mac" mailboxes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cannot open "On My Mac" mailboxes



Thanks, Chris.  That did the trick.  Being new to applescript, I opted for the simpler solution by using your first script.  It does what I need with just a couple of small tweaks.

Is there an applescript reference that might list all of the possible interactions applicable to say, Mail, Safari, etc.?

Tom

On 05/04/2012, at 06:19, Christopher Stone wrote:

On May 03, 2012, at 22:24, Tom Lyon wrote:
Very new to Applescript.  Here is a script that I have which offers a list of mailboxes "On My Mac".  I'm stuck as to how to actually open the selected mailbox for viewing.
______________________________________________________________________

Hey Tom,

This gets fun - especially on Lion.

This first script is basic, vanilla Applescript.  It won't sort the mailbox names, although they are in fact sorted by their root container folder like so:

Archives/AWK
Archives/General Archive
Archives/Health
Archives/Humorous
Archives/Lynx
Archives/Tutorials
Email Lists/Applescript Users List/Branching Cleverness
Email Lists/Applescript Users List/Date-Time
Email Lists/Applescript Users List/Finder
Email Lists/Applescript Users List/Fun with Lists & References

------------------------------------------------------------------------------------------------
try
  tell application "Mail"
    
    set mailboxList to mailboxes whose ¬
      name is not "Drafts" and ¬
      name is not "Notes" and ¬
      name is not "Deleted Messages" and ¬
      name is not "Outbox"
    
    set mailboxNameList to name of mailboxes whose ¬
      name is not "Drafts" and ¬
      name is not "Notes" and ¬
      name is not "Deleted Messages" and ¬
      name is not "Outbox"
    
    tell me to set selectedMailboxName to ¬
      choose from list mailboxNameList with empty selection allowed
    
    if selectedMailboxName is not false and selectedMailboxName is not {} then
      set selectedMailboxName to item 1 of selectedMailboxName
      
      repeat with i from 1 to length of mailboxNameList
        if item i of mailboxNameList = selectedMailboxName then exit repeat
      end repeat
      
      set selectedMailbox to item i of mailboxList
      
      tell (some message viewer whose index is 1)
        set selected mailboxes to {selectedMailbox}
      end tell
      
    end if
    
  end tell
on error eMsg number eNum
  set {c, s} to {return, "------------------------------------------"}
  set e to s & c & "Error: " & eMsg & c & s & c & "Error Number: " & eNum & c & s
  beep
  display dialog e
end try
------------------------------------------------------------------------------------------------

This second script is my preferred method, because I have more control over how the list is displayed.

It's not real pretty, but it's fast.

The mailbox list is sorted by name.

It would not be hard to show the hierarchy either, so you could see sub-groupings of mail folders.

 Archives/AWK

The functions handled by the osax could be done with Perl, Python, Ruby, etc., or you could use Shane Stanley's ASObjC Runner (of course the syntax would be different).

Normally the handlers are in a library.

------------------------------------------------------------------------------------------------
# Requires the Satimage.osax for 'find text', 'join', 'sortlist', 'splittext'
------------------------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------------------------
on cng(findText, changeText, srcData)
  return change findText into changeText in srcData with regexp without case sensitive
end cng
------------------------------------------------------------------------------------------------
on fnd(findStr, srcData, caseSensitive, allOccurrences, stringResult)
  try
    set findResult to find text findStr in srcData case sensitive caseSensitive all occurrences allOccurrences ¬
      string result stringResult with regexp
    return findResult
  on error
    return false
  end try
end fnd
------------------------------------------------------------------------------------------------
on fndUsing(fndStr, returnStr, srcData, caseSensitive, regExFlag, wholeWord, allOccurrences, stringResult)
  try
    set findResult to find text fndStr in srcData case sensitive caseSensitive regexp regExFlag ¬
      whole word wholeWord using returnStr all occurrences allOccurrences string result stringResult
    return findResult
  on error
    return false
  end try
end fndUsing
------------------------------------------------------------------------------------------------
--» MAIN
------------------------------------------------------------------------------------------------
try
  set mbx to {}
  tell application "Mail"
    try
      mailboxes / 0
    on error e
    end try
    set e to cng("\\A.[^{]+{\\s*|} into type real\\.", "", e) of me
    set e to cng("(?:, )?«class mbxp» ", "\\r", e) of me
    set e to cng("\\s*of application.+", "", e) of me
    set e to cng("\"", "", e) of me
    set e to cng("^(Deleted Messages|Notes|Outbox)$\\r{,1}", "", e) of me
    set e to cng("\\A\\s+|\\s+\\Z", "", e) of me
    set mailboxNames to join fnd("[^/]+$", e, false, true, true) of me using return
    set mailboxNames to splittext mailboxNames using return
    set mailboxNames to sortlist mailboxNames comparison 2
    set selectedMailboxName to choose from list mailboxNames with empty selection allowed
    if selectedMailboxName is not false and selectedMailboxName is not {} then
      set selectedMailboxName to item 1 of selectedMailboxName
      set selectedMailboxName to cng("([?.*()\\[\\]{}])", "\\\\\\1", selectedMailboxName) of me
      set mailBoxRef to fnd("^.*" & selectedMailboxName & "$", e, false, false, true) of me
      set mailBoxRef to mailbox mailBoxRef
      tell (some message viewer whose index is 1)
        set selected mailboxes to {mailBoxRef}
      end tell
    end if
  end tell
on error eMsg number eNum
  set {c, s} to {return, "------------------------------------------"}
  set e to s & c & "Error: " & eMsg & c & s & c & "Error Number: " & eNum & c & s
  beep
  display dialog e
end try
------------------------------------------------------------------------------------------------

--
Best Regards,
Chris

 _______________________________________________
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

References: 
 >Cannot open "On My Mac" mailboxes (From: Tom Lyon <email@hidden>)
 >Re: Cannot open "On My Mac" mailboxes (From: Christopher Stone <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.