AppleScript修改Chrome窗口给定名称报错问题求助
AppleScript修改Chrome窗口Given Name问题排查
问题背景
两段AppleScript代码,第一段可正常遍历Chrome窗口并修改given name;第二段尝试遍历所有可见进程的窗口,但修改Chrome窗口given name时触发错误,需修复以实现和第一段相同的效果。
报错信息
运行第二段代码时触发如下错误:
tell current application say "changing window name..." end tell tell application "System Events" set «class GNam» of item 2 of every window of application process "Google Chrome" to "applescript - Google Chrome - changed" --> error number -1728 from «class GNam» of item 2 of every window of application process "Google Chrome" say "System Events got an error: Can’t set «class GNam» of item 2 of every window of application process \"Google Chrome\" to \"applescript - Google Chrome - changed\"." --> error number -10004 end tell
完整脚本代码
-- part 1 tell application "Google Chrome" set i to 0 repeat with theWindow in (every window) set i to i + 1 set windowGivenName to (given name of theWindow) set given name of theWindow to windowGivenName & " - window " & i end repeat end tell -- part 2 tell application "System Events" repeat with theProcess in (every application process whose (visible is true) and (background only is false) and (has scripting terminology is true)) set appName to (name of theProcess) if appName is "Google Chrome" then tell application process appName say "processing " & appName set windowsCount to count of windows if windowsCount > 0 then repeat with theWindow in every window try set theName to name of theWindow set theSize to size of theWindow set {x, y} to position of theWindow say "changing window name..." set windowName to theName & " - changed" tell application "Google Chrome" set given name of theWindow to (windowName as text) end tell say "new window name is " & windowName on error errorMessage say errorMessage end try end repeat end if end tell end if end repeat end tell
尝试过的变体及对应错误
- 使用
tell process appName:触发与原报错相同的-1728错误 - 使用
tell application appName:触发如下错误:
tell application "System Events" count every window of application "Google Chrome" --> error number -1728 from application "Google Chrome" Result: error "System Events got an error: Can’t get application \"Google Chrome\"." number -1728 from application "Google Chrome"
问题原因及解决方案
核心原因
System Events中获取的window对象是UI元素对象,和Chrome应用自身脚本接口返回的window对象不是同一个实体。Chrome的脚本接口只认自己的窗口引用,无法直接处理来自System Events的UI窗口对象,因此修改given name会报错。
修正后的代码
通过窗口ID关联System Events的UI窗口和Chrome的脚本窗口,实现修改:
-- part 2 修正版 tell application "System Events" repeat with theProcess in (every application process whose (visible is true) and (background only is false) and (has scripting terminology is true)) set appName to (name of theProcess) if appName is "Google Chrome" then tell application process appName say "processing " & appName set windowsCount to count of windows if windowsCount > 0 then repeat with theWindow in every window try -- 获取System Events窗口的ID(对应Chrome的窗口ID) set windowID to id of theWindow set theName to name of theWindow set theSize to size of theWindow set {x, y} to position of theWindow say "changing window name..." set windowName to theName & " - changed" -- 在Chrome的tell块中通过ID找到对应窗口并修改 tell application "Google Chrome" set given name of (first window whose id is windowID) to windowName end tell say "new window name is " & windowName on error errorMessage say errorMessage end try end repeat end if end tell end if end repeat end tell
说明
- 从System Events的UI窗口对象中提取
id,这个ID和Chrome脚本接口中窗口的id是一致的 - 在Chrome的
tell块中,通过first window whose id is windowID定位到对应的窗口,再修改given name - 这样既保留了System Events遍历进程窗口的逻辑,又能正确调用Chrome自身的脚本接口完成修改
内容的提问来源于stack exchange,提问作者mw_
相关产品推荐
相关产品推荐

