代码之家  ›  专栏  ›  技术社区  ›  Devarshi

简单的GUI脚本问题

  •  0
  • Devarshi  · 技术社区  · 15 年前

    我正在尝试这个简单的GUI脚本来打开Safari的一个新窗口:

    tell application "Safari"
        activate
    end tell
    
    tell application "System Events"
        tell process "Safari"
            try
                tell menu bar 1
                    tell menu bar item 3
                        click menu item 1
                    end tell
                end tell
            on error theError
                display dialog ("An error occurred while performing requested action" & theError) buttons "OK" default button "OK"
            end try
        end tell
    end tell
    

    应为行尾,但找到“”

    有人能告诉我哪里错了吗?

    谢谢,

    米拉杰

    1 回复  |  直到 15 年前
        1
  •  1
  •   regulus6633    15 年前

    哇,真奇怪。你的脚本破坏了AppleScript编辑器。在运行你的脚本,但它不工作。。。我试图重新编译脚本,然后你发布的错误开始出现。因此,不知何故,您的代码导致AppleScript编辑器中断,从而导致错误。我不得不退出并重新启动AppleScript编辑器以使其重新工作。

    不管怎样,你的代码应该是这样的。。。

    tell application "Safari"
        activate
    end tell
    
    tell application "System Events"
        tell process "Safari"
            try
                tell menu bar 1
                    tell menu bar item 3
                        click menu item 1 of menu 1
                    end tell
                end tell
            on error theError
                display dialog ("An error occurred while performing requested action " & theError) buttons "OK" default button "OK"
            end try
        end tell
    end tell
    

    编辑: 正如我在下面的评论中提到的,如果您无法从应用程序的字典中找到本机命令,那么下一个最可靠的方法就是使用键盘快捷键。大多数菜单项都有。例如,如果我想在一个窗口中打开一个新的选项卡,则该菜单项具有键盘快捷键命令-t。所以我们可以这样用。注意,有一个本机命令可以在不使用击键的情况下打开一个新选项卡,我只是举个例子。

    tell application "Safari" to activate
    tell application "System Events"
        keystroke "t" using command down
    end tell
    end