我正在尝试使用以下脚本在 Mac 上同步 Safari 的书签和 Chrome 的书签。 但是,我收到此错误: “系统事件出现错误:无法获取菜单项“Google Chrome.app ...”菜单项“导入自”菜单项“导入自”菜单项“文件”菜单栏 1 的菜单项“文件”处理“Safari”。 我认为菜单名称是正确的,不知道哪里出了问题...请给我任何建议。谢谢。
tell application "Safari"
activate
tell application "System Events"
tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
tell menu item "Import From"
tell menu "Import From"
click menu item "Google Chrome.app…"
delay 0.5
keystroke return
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
菜单项没有
.app
,所以应该是:
click menu item "Google Chrome…"
到目前为止所说的一切还不够
GUI 脚本应该准确地重复用户手动执行的clicks。在脚本单击相应的菜单栏项或相应的菜单项之前,不会打开任何菜单。
打开每个菜单就像打开一个窗口。因此,您需要添加一个时间延迟 - 为了稳定。在这里,我添加了自动延迟:
tell application "System Events" to tell process "Safari"
set frontmost to true
tell menu bar 1 to tell menu bar item "File"
click it -- important click
repeat until menu "File" exists -- important delay
delay 0.2
end repeat
tell menu "File" to tell menu item "Import From"
click it -- important click
repeat until menu "Import From" exists -- important delay
delay 0.2
end repeat
tell menu "Import From" to click menu item "Google Chrome.app…"
end tell
end tell
end tell