1
votes

anyone know how to use search bar in Mail application using AppleScript? I want to automate the process of searching subject(instead of search entire message) using search bar in Mail application. Since i want to search all the mailboxes(except inbox, there are other smart mailboxes), i cannot use the common way, which is set the mailbox then go through all the messages inside. Thanks for your help.

2

2 Answers

2
votes

You can use a technique called GUI scripting, it enables you to directly address ui widgets and perform actions with them.

Give this script a try: (changing the search text to what you need)

activate application "Mail"

tell application "System Events"
    tell process "Mail"
        tell window 1

            set search_text_field to text field 1 of the last group of tool bar 1

            tell search_text_field
                set the value of attribute "AXValue" to "subject:congratulations"
                key code 36 -- press RETURN key
            end tell

        end tell
    end tell
end tell

(It works for me on 10.7, may not on more recent OS)

0
votes

For me, this did not work in Yosemite. I used this workaround:

activate application "Mail"

tell application "System Events"

    tell process "Mail"

        tell window 1

            keystroke "f" using {command down, option down}

            keystroke *your variable* as string

        end tell

    end tell
end tell