Search code examples
windowapplescriptshowfinder

Show something in a finder window


Ok, I am trying to make a script where you type something and a new finder window appears with what you typed in it. Similar to spotlight search, but in a script.

set theFind to text returned of (display dialog "What do you want to find?" default answer "" buttons {"Cancel", "Ok"} default button 2)
    tell application "Finder"
        reveal theFind
    end tell

Solution

  • There’s an AppKit method that does exactly what (I think) you’re asking for: -[NSWorkspace showSearchResultsForQueryString:], which means you can use it using AppleScriptObjC. So, in AppleScript Editor, File > New from Template > Cocoa-AppleScript Applet, then:

    property NSWorkspace : class "NSWorkspace"
    NSWorkspace's sharedWorkspace()'s showSearchResultsForQueryString_(theFind)
    

    Alternatively, you could skip AppleScriptObjC and use the hidden Finder command that that method uses:

    tell application "Finder" to «event aevtspot» theFind