【问题标题】:In applescript, is there any way to specify which desktop a file is opened in?在applescript中,有没有办法指定文件在哪个桌面上打开?
【发布时间】:2023-03-25 09:53:02
【问题描述】:

当我的系统启动时,我想运行在三个不同桌面/空间中打开文件的 applescript。

First Space: Mail and Things (my to do list program)
Second Space: Textmate and a Safari for my first project
Third Space: Textmate and a Safari for my second project

首先,在任务控制中,我创建了另外两个桌面,除非手动删除它们,否则下次系统启动时它们将保留在那里。我没有创建一个长脚本,而是链接了三个 applescript(boot1、boot2 和 boot3)以将其分解为更简单的代码块。在 boot1 结束时你会看到:

run script file "<drive name>:Users:<username>:boot2.scpt"

在 boot2 和 boot3 中,你会看到一堆 delay 行。我不喜欢 applescript 的一件事是它经常在操作系统完成对前一个命令的响应之前开始处理下一个命令。这会导致不一致和错误。延迟是一种迫使事情放慢速度的技巧。它们有帮助,但即使你使用它们,事情仍然有点冒险。在 boot2.script 中:

# this emulates the keyboard shortcut to move to desktop 2
# there doesn't seem to be any way to modify an `open` command to open a file on desktop 2
tell application "System Events"
    delay 2
    # key code 19 is the key code for the number 2. 
    # <cntl> 2 is the shortcut to get to desktop 2
    key code 19 using control down
end tell

tell application "TextMate"
    activate
    # 'sites' is the name of the directory my projects are in
    open "/users/<username>/sites/project1/"
end tell

tell application "Terminal"
    activate
    do script "cd /users/<username>/sites/project1/"

    delay 2
    do script "rails s" in front window

    delay 2
    tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
    tell application "System Events" to tell process "Terminal" to keystroke return

    delay 2
    do shell script "open -a Safari http://localhost:3000"
end tell

好的...所以这主要是为了让桌面 2 就位,除非延迟不够长时出现不一致。 Boot3.script 与 boot2 几乎相同,但当尝试在桌面 3 上打开应用程序时,由于桌面 2 上有一个窗口,系统会跳回该桌面。这是下一个问题。我该如何克服呢?

2305491 不再相关,因为空间偏好已消失。

谢谢。

【问题讨论】:

  • 我认为没有理由用 Applescript 来做这件事。您可以使用自动录音功能。

标签: applescript


【解决方案1】:

Boot3.script 与 boot2 几乎相同,但当尝试在桌面 3 上打开应用程序时,由于桌面 2 上有一个窗口,系统会跳回该桌面。

任务控制首选项中有一个选项称为“切换到应用程序时,切换到应用程序打开窗口的空间”。取消选中此项。

好的...所以这主要是为了让桌面 2 就位,除非延迟不够长时出现不一致。

更好的解决方案总是这样的

repeat until something exists
delay 0.1
end repeat

【讨论】:

    猜你喜欢
    • 2016-03-05
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    相关资源
    最近更新 更多