AppleScript调用System Events时含撇号的法语菜单项无法定位求助
解决AppleScript中法语带撇号菜单项无法识别的问题
在法语系统环境下,使用AppleScript点击带撇号的菜单项(如Ouvrir l'élément récent)时,System Events会提示无法找到对应菜单项,本质原因是AppleScript对单撇号的解析规则:单个撇号会被视为字符串的闭合标记,导致实际传递的菜单项名称被截断。
解决方案1:转义撇号
将菜单项名称中的单个撇号'替换为两个连续的撇号'',这样AppleScript会正确识别为字符串内的单撇号字符。修改后的代码如下:
on run {input, parameters} tell application "System Events" set theApp to (get name of first process whose frontmost is true) click menu bar item "Fichier" of menu bar 1 of process theApp click menu item "Ouvrir l''élément récent" of menu 1 of menu bar item "Fichier" of menu bar 1 of process theApp end tell return input end run
解决方案2:通过菜单项索引访问
如果不想处理字符转义,也可以通过菜单项的位置索引来点击。这种方法无需依赖菜单项名称,还能避免系统语言切换带来的名称变化问题(前提是菜单项位置固定)。示例代码如下:
on run {input, parameters} tell application "System Events" set theApp to (get name of first process whose frontmost is true) set fileMenu to menu 1 of menu bar item "Fichier" of menu bar 1 of process theApp click fileMenu -- 请根据实际菜单结构调整索引值,比如"Ouvrir l'élément récent"是第5项就写5 click menu item 5 of fileMenu end tell return input end run
验证方法
修改后运行Automator工作流,System Events就能正确定位到目标菜单项,完成点击操作。
内容的提问来源于stack exchange,提问作者Jop van Dijk
相关产品推荐
相关产品推荐

