You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 22:18:09