AppleScript获取Preview所有打开文档路径的编译错误求助
解决AppleScript遍历Preview所有文档路径的问题
错误原因分析
- AppleScript中Preview的所有文档集合是
documents,而非你写的all documents,这是编译报错的直接原因。 - 循环里的
front document i是错误写法,循环变量i本身就是单个document对象,直接获取它的路径即可。
修正后的遍历Preview所有文档路径代码
tell application "Preview" repeat with doc in documents -- 打印每个文档的路径到控制台 log path of doc end repeat end tell
完整实现:将路径写入文本文件
如果要把所有路径保存到文本文件(比如桌面的PreviewDocuments.txt),可以用下面的代码:
set outputPath to (path to desktop as text) & "PreviewDocuments.txt" set pathList to {} tell application "Preview" repeat with doc in documents set end of pathList to (path of doc as text) & linefeed end repeat end tell -- 将路径列表合并成字符串并写入文件 set fileHandle to open for access outputPath with write permission write (pathList as text) to fileHandle close access fileHandle
扩展:获取其他应用的窗口/标签页内容
如果需要处理其他应用(比如Safari的所有标签页URL),可以参考这个代码:
set outputPath to (path to desktop as text) & "SafariTabs.txt" set urlList to {} tell application "Safari" repeat with win in windows repeat with tabItem in tabs of win set end of urlList to (URL of tabItem as text) & linefeed end repeat end repeat end tell set fileHandle to open for access outputPath with write permission write (urlList as text) to fileHandle close access fileHandle
内容的提问来源于stack exchange,提问作者Nobumi Iyanaga
相关产品推荐
相关产品推荐

