开发者

Applescript launching of Spotlight?

开发者 https://www.devze.com 2023-03-25 20:53 出处:网络
I tried to use the following to open the Sp开发者_如何学Cotlight search box: tell application \"System Events\"

I tried to use the following to open the Sp开发者_如何学Cotlight search box:

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

It simply tries to issue command-space. It does work, but if I save the script as an Application and run it, the Spotlight window shows up and then promptly disappears afterwards.

Why does this happen and what can I do to keep the Spotlight window open?

Alternatively: How do I open the Spotlight search box using Applescript?


Your script opens the Spotlight menu. The keyboard shortcut for opening the Spotlight window is command + option + space...

tell application "System Events" to keystroke space using {command down, option down}

UPDATE: Given your revised answer, I have composed a little script that should do what you want...

set the searchText to the text returned of (display dialog "Enter the name of an item you wish to search for in Spotlight:" default answer "")
tell application "System Events"
    keystroke space using {command down}
    keystroke the searchText
    --[1]
end tell

You can do one of the following things at [1]:

  • Open the top hit:

    keystroke return using {command down}
    
  • Move the selection to the first item in the next category:

    keystroke (ASCII character 31) using {command down} --down arrow
    
  • Move the selection to the first item in the previous category:

    keystroke (ASCII character 30) using {command down} --up arrow
    
  • Move the selection to the first item in the whole menu:

    keystroke (ASCII character 31) using {control down}
    
  • Move the selection to the last item in the whole menu:

    keystroke (ASCII character 30) using {control down}
    


Simple add a delay.

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

Or to foolproof this:

0

精彩评论

暂无评论...
验证码 换一张
取 消