Abs
Abs

Reputation: 57966

Open Mail with AppleScript

I thought I could just do this:

tell application "Mail"
activate
end tell

Although I can see the Mail toolbar at the top. It hasn't brought the mail app to the front to be fully visible.

My recorder doesn't work for some reason too, so I can't use that to find out what I need to do.

How can I do this?

Upvotes: 2

Views: 3716

Answers (3)

regulus6633
regulus6633

Reputation: 19040

You probably have closed the mail window so when you activate the application there isn't any open windows to show. As such you have to open one yourself. Note that the main window in mail is called a "message viewer". Try this...

AppleScript:

tell application "Mail"
    activate
    if (count of message viewers) is 0 then
        make new message viewer at front
        set selected mailboxes of message viewer 1 to {inbox}
    end if
end tell

JXA:

var Mail = Application("Mail")
Mail.activate()
if (Mail.messageViewers().length === 0) {
  Mail.MessageViewer().make()
  var mv = Mail.messageViewers()[0]
  mv.selectedMailboxes.set(mv.inbox())
}

By the way, the recorder doesn't work in most applications so I'm not surprised to hear that it doesn't work for Mail. An application author has to code this ability into the program and most developers (including Apple) do not do this.

Upvotes: 7

kindall
kindall

Reputation: 184405

Well, you can always try activating it a second time. Alternatively, you can just tell the Finder to launch it and see if that works any better:

tell application "Finder" to open application file id "com.apple.mail"

Upvotes: 0

Ken Aspeslagh
Ken Aspeslagh

Reputation: 11594

What you're doing should work. Not sure why it's not working for you.

Upvotes: 0

Related Questions