Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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

share|improve this question
    
What's your actual goal here? Do you need to use the search, or do you just need to find and open a specific file? –  robmathers Sep 12 '12 at 18:54
    
Well, I need to find and open a folder and get the contents from it. –  daveMac Sep 12 '12 at 19:00
add comment

1 Answer 1

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.

share|improve this answer
    
I am not sure how to implement this. Copy and paste doesn't work. Any tips? –  daveMac Sep 13 '12 at 17:13
    
I've reformmatted the code snippet with line-feeds. That should help. At least it's readable now. –  Wayfaring Stranger Sep 13 '12 at 17:34
    
@WayfaringStranger I redid the code formatting so it looks right. –  CajunLuke Sep 13 '12 at 17:41
    
@daveMac Copy-and-pasting should work now. –  CajunLuke Sep 13 '12 at 17:42
1  
'Titan' is just the name of whatever Finder window you have open. If you don't want to use the window's name, you could always direct the command to the FRONTmost Finder window by telling the Finder: set value of text field 1 of group 5 of tool bar 1 of front window to "Here is some text" –  Wayfaring Stranger Sep 13 '12 at 20:17
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.