MacOS – Using UI scripting to enter text in a file dialog

applescriptmacos

I am very, very new to using AppleScript. I need to enter text into the search box. So far the script looks like this:

on run {input, parameters}

    tell application "myApplication" to activate

    tell application "System Events"
        keystroke "o" using {command down}
    end tell

    return input
end run

This launches the app and fires the command-o keyboard shortcut to show a file dialog. Now I just need to enter text in:enter image description here

Best Answer

The trick is figuring out how to address the correct control item in the window. See this hint for an explanation of how to get the needed information: http://hints.macworld.com/article.php?story=20111208191312748

This Applescript code will work on a Finder window named "Titan":

-- text field 1 of group 5 of tool bar 1 of window "Titan" of application process "Finder" of application "System Events"
-- button 1 of text field 1 of group 5 of tool bar 1 of window "Titan" of application process "Finder" of application "System Events"

set windowname to "Titan"
-- set this to name of open Finder window you want to deal with

tell application "Finder"
    activate
end tell

tell application "System Events"
    tell process "Finder"
        set value of text field 1 of group 5 of tool bar 1 of window windowname to "Here is some text"
    end tell
end tell

Anyway, the code'll insert "here is some text" into the Finder window's search box. The first 2 lines are comments showing the pieces of the search box. You can probably click on that button too.