如何用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
nameproperty (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
linefeedto 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
相关产品推荐
相关产品推荐

