Applescript 推出 Spotlight?

发布于 2024-11-27 15:44:45 字数 317 浏览 1 评论 0原文

我尝试使用以下命令打开 Spotlight 搜索框:

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

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

为什么会发生这种情况?我该怎么做才能保持 Spotlight 窗口打开?

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

I tried to use the following to open the Spotlight 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2024-12-04 15:44:45

您的脚本将打开 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] 中执行以下操作之一:

  • 打开顶部点击:

    使用 {command down} 按键返回
    
  • 将选择移至下一个类别中的第一项:

    击键(ASCII 字符 31)使用 {command down} --down arrow
    
  • 将所选内容移至上一类别中的第一项:

    击键(ASCII 字符 30)使用 {command down} --up arrow
    
  • 将所选内容移至整个菜单中的第一项:

    使用 {control down} 击键(ASCII 字符 31)
    
  • 将所选内容移至整个菜单中的最后一项:

    使用 {control down} 击键(ASCII 字符 30)
    

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}
    
迷雾森÷林ヴ 2024-12-04 15:44:45

简单地添加一个延迟。

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

或者为了万无一失:

Simple add a delay.

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

Or to foolproof this:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文