Script que genera un número de correos electrónicos en Mail.App Bandeja de entrada

apple.stackexchange https://apple.stackexchange.com/questions/7897

  •  22-10-2019
  •  | 
  •  

Pregunta

Me gustaría tener un script de línea de comandos que pueda invocar en el terminal que generará el número total de mensajes en mi bandeja de entrada de "trabajo" en Mail.App. El uso se vería así:

$ inbox-count
48

¿Cómo se ve el código AppleScript para hacer algo como esto?

¿Fue útil?

Solución

Si desea obtener el número de mensajes en dicha bandeja de entrada, la forma breve del código es

tell application "Mail" to ¬
  get the count of messages of mailbox "INBOX" of account "Work"

Si desea la bandeja de entrada global, puede usar get the count of messages of inbox en cambio. Si solo desea mensajes no leídos, entonces puede usar get the unread count of mailbox "INBOX" of account "Work".

Y si quieres un script más completo, esto hará el truco:

#!/usr/bin/osascript
property defaultAccount : "Work"
property defaultMailbox : "INBOX"

on run args
  set justUnread to false
  set theAccount to missing value
  set theMailbox to missing value

  if defaultAccount = missing value then set defaultAccount to "-g"
  if defaultMailbox = missing value then set defaultMailbox to "INBOX"

  set theCount to the count of args

  if theCount > 0 then
    if item 1 of args = "-u" then
      set justUnread to true
      set theCount to theCount - 1
      set args to the rest of args
    else if item 1 of args = "-ug" or item 1 of args = "-gu" then
      set justUnread to true
      set item 1 of args to "-g"
    else if theCount > 1 and ¬
            item 1 of args = "-g" and item 2 of args = "-u" then
      set justUnread to true
      set theCount to theCount - 1
      set args to the rest of args
      set item 1 of args to "-g"
    end if
  end if

  tell application "Mail"
    if theCount = 0 then
      set theAccount to defaultAccount
      set theMailbox to defaultMailbox
    else if theCount = 1 then
      set theAccount to item 1 of args
      set theMailbox to defaultMailbox
    else if theCount = 2 then
      set theAccount to item 1 of args
      set theMailbox to item 2 of args
    else
      error character id 10 ¬
          & "Usage: inbox-count [-u] [[account] mailbox]" & character id 10 ¬
          & "       inbox-count [-u] -g [mailbox]"
    end if

    set mailboxValue to missing value
    if theAccount = "-g" then
      if theMailbox = "INBOX" then
        set mailboxValue to inbox
      else
        set mailboxValue to mailbox theMailbox
      end if
    else
      set mailboxValue to mailbox theMailbox of account theAccount
    end if

    if justUnread then
      return the unread count of mailboxValue
    else
      return the count of messages of mailboxValue
    end if
  end tell
end run

La mayor parte de eso es un análisis de la línea de comandos, porque es un dolor en AppleScript. Pero el resultado es que con ese script en tu camino como inbox-count, entonces funcionan los siguientes comandos:

  • inbox-count Para verificar el número de mensajes en el par de buzón/cuenta predeterminado.
  • inbox-count -g Para verificar el número de mensajes en la bandeja de entrada global (combinada).
  • inbox-count Play Para verificar el número de mensajes en el buzón predeterminado para la cuenta "Reproducir".
  • inbox-count -g Important Para verificar el número de mensajes en el buzón global "importante".
  • inbox-count Play Facebook Para verificar el número de mensajes en el buzón "Facebook" para la cuenta "Reproducir".

También puedes prever un -u a cualquiera de esos comandos (p.ej, inbox-count -u, inbox-count -ug, inbox-count -u Play Facebook) para obtener el recuento no leído. Para cambiar la cuenta predeterminada y el buzón, cambie las líneas property defaultAccount : "Work" y property defaultMailbox : "INBOX". Si defaultAccount es missing value o "-g", entonces el valor predeterminado será no usar una cuenta; si defaultMailbox es missing value o "INBOX", entonces el valor predeterminado será usar un buzón con nombre "INBOX" o, si la cuenta es "-g", para usar la bandeja de entrada global.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a apple.stackexchange
scroll top