I'd like to make F5 be equivalent to clicking on the mail icon in the taskbar, and F6 equivalent to clicking minimise on the mail app main window (or the 'hide' action). Is there a way to achieve this?

I'm following the method here.

Below is my script which almost works. The problem is I'm clicking a menu item the name of which is not constant.

on run {input, parameters}
    tell application "System Events"
        tell process "Mail"
            set frontmost to true
            -- "Inbox (8 Messages)" will only work when 8 messages!:
            click menu item "Inbox (8 Messages)" of menu "Window" of menu bar 1
        end tell
    end tell
    return input
end run

2 Answers 2


Inbox has a 'get out of jail free' card - it's accessible by simple key command, by default
Cmd ⌘ 1

That means you can get away with

tell application "Mail" to activate
tell application "System Events" to keystroke "1" using command down

If you get a permissions error, then it seems the way to add perms is to put

do shell script "osascript -e 'tell application \"Mail\" to activate'"

at the head of the script one time & run it. You should then get the opportunity to add to the Automation perms in Security & Privacy. (I have no clue how/why this works, but it seems to.)

  • 1
    Turns out I did need the 'do shell script' as above (to trigger it from services, rather than just within automator), as only way to resolve constant permissions failures.
    – mwal
    Commented Jan 21, 2021 at 10:10
  • Glad you got it:) One day someone might tell me how that osascript part makes it work - but for now I'm just happy that it does.
    – Tetsujin
    Commented Jan 21, 2021 at 11:33

Object Specifiers

In AppleScript, the way an object is referred to is called the object specifier. In your code, you are using the name specifier.

Try a numeric value for the menu item, such as:

click (menu item 12) of menu "Window" of menu bar 1

Or a relative term, such as:

click the last menu item of menu "Window" of menu bar 1

You must log in to answer this question.

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