I set my mail agent tool to send and to receive mails with many accounts such as a gmail account.

I want to read the mail via command line for triggering cronjob purpose.

For example, | grep "Subject: " && echo "I got it." this one is my purpose.

Is there also a way to switch among those accounts' mail box for reading purpose?

  • You can set mail rules in macos mail to invoke a script. I know it's not from the command line, but should be able to do what you want. Mar 6, 2019 at 4:29
  • Your going to need to install your own command line mail client. Mar 6, 2019 at 4:31
  • Thanks. @historystamp I got it. I post an answer.
    – jefferyear
    Mar 10, 2019 at 11:48

1 Answer 1


This is to me to know AppleScript with shell script.

My purpose is to use this to check the hard drive size.

I answer here.

var=$(df -h / | tail -n 1 | awk '{print $4}' | sed 's|Gi||g'); 

if [ ${var} -lt 40 ]; then 
  echo "The space is less than 40 Gb."
  echo "The space is more than 40 Gb."

if [ ${checker}=1 ]; then
  osascript <<'EOD'
tell application "Mail"
    set theFrom to “someone@gmail.com"
    set recipientName to ""
    set recipientAddress to “nobody@gmail.com"
    set theSubject to "The rest of hard drive space is less 40Gb"
    set theMessage to make new outgoing message with properties {sender:theFrom, subject:theSubject, visible:false}
    tell theMessage
        make new to recipient with properties {name:recipientName, address:recipientAddress}
    end tell
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .