Skip to content

Instantly share code, notes, and snippets.

@htlin222
Created May 1, 2025 08:47
Show Gist options
  • Save htlin222/4dfad7e88386abcb80e4c9c8dd8e246b to your computer and use it in GitHub Desktop.
Save htlin222/4dfad7e88386abcb80e4c9c8dd8e246b to your computer and use it in GitHub Desktop.
Smart Paste for Finder
(*
πŸ“‹ 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