1

我尝试使用以下方法打开 Spotlight 搜索框:

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

它只是尝试发出命令空间。它确实有效,但如果我将脚本保存为应用程序并运行它,Spotlight 窗口会出现,然后立即消失。

为什么会发生这种情况,我可以做些什么来保持 Spotlight 窗口打开?

或者:如何使用 Applescript 打开 Spotlight 搜索框?

4

2 回答 2

1

您的脚本会打开 Spotlight菜单。打开 Spotlight窗口的键盘快捷键是command+ option+ space...

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

更新:鉴于您修改后的答案,我编写了一个小脚本,应该可以满足您的需求...

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

您可以在 [1] 处执行以下操作之一:

  • 打开顶击:

    keystroke return using {command down}
    
  • 将选择移至下一个类别中的第一项:

    keystroke (ASCII character 31) using {command down} --down arrow
    
  • 将选择移动到上一个类别中的第一项:

    keystroke (ASCII character 30) using {command down} --up arrow
    
  • 将选择移动到整个菜单中的第一项:

    keystroke (ASCII character 31) using {control down}
    
  • 将选择移动到整个菜单中的最后一项:

    keystroke (ASCII character 30) using {control down}
    
于 2011-08-04T00:46:23.000 回答
0

简单添加延迟。

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

或者为了万无一失:

于 2017-02-06T12:24:00.073 回答