AppleScript遍历Stickies窗口异常:相同开头便签返回重复内容
AppleScript遍历Stickies便签窗口内容异常问题解决
问题描述
使用AppleScript遍历苹果Stickies应用窗口时,循环次数与实际便签数量一致,但当存在多个开头文本相同的便签时,仅能获取第一个便签的属性内容。
原问题代码
tell application "Stickies" activate tell application "System Events" tell application process "Stickies" set allWindows to every window try repeat with awindow in allWindows set m to value of text area 1 of scroll area 1 of awindow log m & " " end repeat end try end tell end tell end tell
问题原因
原代码通过System Events操作UI元素获取便签内容,当多个便签窗口的文本区域UI属性高度相似时,系统UI元素识别会出现偏差,导致重复读取第一个窗口的内容。
解决方案
直接调用Stickies应用原生的AppleScript接口,无需通过UI元素操作——Stickies本身支持直接访问便签对象及其内容属性。
修正后的代码
tell application "Stickies" activate set allNotes to every note repeat with aNote in allNotes set noteContent to content of aNote log noteContent & linefeed & linefeed end repeat end tell
说明
这段代码直接与Stickies应用交互,获取所有note对象,通过content属性直接读取每个便签的文本内容,完全避开UI元素识别的问题,无论便签开头文本是否相同,都能准确遍历获取每个便签的内容。
内容的提问来源于stack exchange,提问作者MisterMushn
相关产品推荐
相关产品推荐

