简单的GUI脚本问题
我正在尝试这个简单的 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
但它给出了以下错误消息:
预期行尾但发现“””
任何人都可以建议我哪里可能错了吗?
谢谢,
Miraaj
I am trying this simple GUI script to open a new window of 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
but it is giving this error message:
Expected end of line but found """
Can anyone suggest me where I may be wrong?
Thanks,
Miraaj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇,太奇怪了。您的脚本破坏了 AppleScript 编辑器。运行您的脚本后它不起作用...我尝试重新编译脚本,然后您发布的错误开始出现。因此,不知何故,您的代码导致 AppleScript 编辑器崩溃,从而导致错误。我必须退出并重新启动 AppleScript Editor 才能使其再次运行。
我使用应用程序 UI 浏览器并发现了问题。您对菜单项的引用是错误的。那里有一个我们看不到的额外菜单...并且您没有引用该额外菜单。这是 gui 脚本的问题。即使 GUI 脚本可以工作,但随着应用程序的更新,它也可能在未来的某个日期中断。因此,如果可能的话,请避免使用 GUI 脚本。
不管怎样,你的代码应该是这样的......
编辑:
正如我在下面的评论中提到的,如果您无法从应用程序的字典中找到本机命令,那么下一个最可靠的方法是使用键盘快捷键。大多数菜单项都有它们。例如,如果我想在窗口中打开一个新选项卡,该菜单项具有键盘快捷键 command-t。所以我们可以像这样使用它。请注意,有一个本机命令可以在不使用击键的情况下打开新选项卡,我只是将此作为示例。
键盘命令通常不会在应用程序更新之间发生变化,而 gui 命令经常会发生变化,因为程序员在更新中重新设计其界面......而当这种情况发生时,gui 脚本就会变得混乱。 GUI 脚本和击键的问题之一是,有时脚本运行得太快,而这些技术无法跟上程序的速度,因此经常出错。发生这种情况时,您需要使用较小的延迟来减慢脚本速度,以使界面跟上脚本的速度。
Wow, that was weird. Your script broke AppleScript Editor. After running your script and it not working... I tried to recompile the script and then the error you posted starting showing up. So somehow your code caused AppleScript editor to break and thus the error. I had to quit and relaunch AppleScript Editor to get it working again.
I used the application UI Browser and found the problem. Your reference to the menu item was wrong. There's an extra menu in there that we can't see... and you didn't reference that extra menu. This is the problem with gui scripting. And even if a gui script works it may break at some future date as an application is updated. As such avoid gui scripting if at all possible.
Anyway, here's what your code should look like...
EDIT:
As I mentioned in my comment below, if you can't find a native command from an application's dictionary, the next most reliable method is using keyboard shortcuts. Most menu items have them. For example, if I wanted to open a new tab in a window that menu item has the keyboard shortcut command-t. So we can use that like this. Note there is a native command to open a new tab without using keystrokes, I'm just showing this as an example.
Keyboard commands don't usually change between application updates whereas gui commands often do because programmers redesign their interface in updates... and when that happens gui scripting goes haywire. One of the gotcha's with both gui scripting and keystrokes is that sometimes the script goes too fast and these techniques can't keep up with the speed of the program, so they often error. When this happens you need to slow down the script using small delays to allow the interface to keep up with the script.