Question

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
Was it helpful?

Solution

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.)

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top