How to paste files using Applescript (or bash)?
I want to paste a file copied by Finder in the terminal. Sth like pbpaste but for binaries. (pbpaste itself just returns the basename of the copied file.)
One way I can think of doing this is to use a little bit of AppleScriptObjC. It provides access to the various data type representations on the clipboard, and in multiple.
After retrieving the full file paths, these can then be passed to the cp
command.
Wrapping this in a bash function declaration:
pastefiles() {
IFS=$'\n'
fs=($( osascript -e "use framework \"Foundation\"
property this : a reference to the current application
property NSPasteboard : a reference to NSPasteboard of this
property NSURL : a reference to NSURL of this
property pb : a reference to NSPasteboard's generalPasteboard
property text item delimiters : linefeed
pb's readObjectsForClasses:[NSURL] options:[]
(result's valueForKey:\"path\") as list as text" ))
cp -r "${fs[@]}" "$1"
}
Usage: pastefiles
~/Example/Destination/Path
You can add this to your .bashrc
file to have the function available to each new shell session.