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

如何用AppleScript复制Preview窗口中选中图片的文件名?

Fix: Copy Only Filenames of Selected Images in Preview

Got it, let's sort out this Preview filename copying issue for you. The problem with your original script is that using keystroke "c" triggers Preview's default copy behavior, which copies file references (not plain text filenames)—that's why you're getting full paths instead of just the names.

Here's a targeted AppleScript that will grab only the filenames of your selected images and copy them to your clipboard as plain text:

tell application "Preview"
    activate
    set selectedDocuments to selection
    
    if selectedDocuments is not empty then
        set fileNameList to {}
        repeat with doc in selectedDocuments
            -- Extract just the filename (including extension) from the selected document
            set end of fileNameList to name of doc
        end repeat
        
        -- Format filenames (use line breaks for multiple selections; adjust delimiter if needed)
        set AppleScript's text item delimiters to linefeed
        set the clipboard to fileNameList as string
        set AppleScript's text item delimiters to ""
    else
        display alert "Oops! No images are selected in Preview." message "Please select at least one image first."
    end if
end tell

How this works:

  • It first checks if you have any images selected in Preview (so you don't get an error if nothing's picked).
  • For each selected image, it extracts just the name property (this is exactly the filename with its extension, no path attached).
  • It combines all filenames into a single string with line breaks (great for pasting into a list). If you prefer a different separator (like commas or spaces), just change linefeed to something else (e.g., ", " for comma-separated names).
  • Finally, it puts the formatted string directly into your clipboard as plain text—no more full paths!

Bonus tip:

If you want to use this script regularly, you can save it as an application in Script Editor, then add it to your Dock or assign a keyboard shortcut via System Settings > Keyboard > Shortcuts > Services.

内容的提问来源于stack exchange,提问作者DomiNic

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:56:43