Created
May 1, 2025 08:47
-
-
Save htlin222/4dfad7e88386abcb80e4c9c8dd8e246b to your computer and use it in GitHub Desktop.
Smart Paste for Finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
π Smart Paste for Finder | |
π¨βπ» Author: Hsieh-Ting Lin | |
ποΈ Last Updated: 2025-05-01 | |
π Description: | |
This AppleScript acts as a smart "Cmd + V" in macOS Finder. | |
Depending on clipboard content, it will: | |
πΈ Paste images as PNG files in the active Finder folder | |
π Paste plain text as a .txt file | |
π If a file is copied (e.g., PDF, DOCX), it triggers normal paste (Cmd + V) | |
β¨ Features: | |
- Timestamped filenames: yyyy-mm-dd-hh-mm.png or .txt | |
- Auto-renames to avoid overwriting (e.g., ...-1.txt) | |
- Native macOS notifications for feedback | |
βοΈ Usage (Quick Action Setup): | |
1. Open the **Automator** app. | |
2. Create a new **Quick Action**. | |
3. Set "Workflow receives" to **no input** in **Finder**. | |
4. Add **Run AppleScript** as the only action. | |
5. Paste this entire script. | |
6. Save as "Smart Paste". | |
7. Go to **System Settings β Keyboard β Keyboard Shortcuts β Services**, find "Smart Paste" and assign a shortcut (e.g. ββ§V). | |
π¦ Enjoy smarter pasting in Finder! | |
*) | |
use framework "AppKit" | |
use framework "Foundation" | |
use scripting additions | |
-- π§ Helper: Generate unique file path | |
on uniqueFilePath(basePath, extension) | |
set fileManager to current application's NSFileManager's defaultManager() | |
set counter to 0 | |
set finalPath to basePath & "." & extension | |
repeat while (fileManager's fileExistsAtPath:(finalPath)) | |
set counter to counter + 1 | |
set finalPath to basePath & "-" & counter & "." & extension | |
end repeat | |
return finalPath | |
end uniqueFilePath | |
-- π Access clipboard | |
set thePasteboard to current application's NSPasteboard's generalPasteboard() | |
set imgData to thePasteboard's dataForType:(current application's NSPasteboardTypePNG) | |
set txtData to thePasteboard's stringForType:(current application's NSPasteboardTypeString) | |
set fileURLs to thePasteboard's propertyListForType:"public.file-url" | |
-- π Check if clipboard contains file(s) | |
if fileURLs β missing value then | |
tell application "System Events" to keystroke "v" using {command down} | |
return | |
end if | |
-- π Get active Finder folder | |
tell application "Finder" | |
if (count of windows) = 0 then | |
display alert "No Finder window is open" | |
return | |
end if | |
set targetFolder to (target of front Finder window) as alias | |
end tell | |
-- π Timestamp for filename | |
set timestamp to do shell script "date +%Y-%m-%d-%H-%M" | |
set basePath to (POSIX path of targetFolder) & timestamp | |
-- πΈ Handle image paste | |
if imgData β missing value then | |
display notification "Image in the Clipboard" with title "Smart Paste" | |
set filePath to my uniqueFilePath(basePath, "png") | |
set theURL to current application's |NSURL|'s fileURLWithPath:filePath | |
imgData's writeToURL:theURL atomically:true | |
-- π Handle plain text paste | |
else if txtData β missing value then | |
set plainText to (txtData as string) | |
display notification "Text in the Clipboard" with title "Smart Paste" | |
set filePath to my uniqueFilePath(basePath, "txt") | |
set theText to current application's NSString's stringWithString:plainText | |
set theURL to current application's |NSURL|'s fileURLWithPath:filePath | |
theText's writeToURL:theURL atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value) | |
-- β Fallback to normal paste | |
else | |
tell application "System Events" to keystroke "v" using {command down} | |
end if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment