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.)
1 Answer
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.
-
In macOS 10.15 Catalina, Apple has replace the previous default shell,
bash
, withzsh
.Do you know if this will this work as is if placed in the e.g. users.zprofile
or.zshrc
file? Jul 24, 2019 at 15:55 -
@user3439894 I don't know, I'm afraid. Not used
zsh
before. I useFiSh
as my default shell.– CJKJul 25, 2019 at 14:30
pastefile DIR
to paste the file into DIR.cp $(pbpaste) DIR
then?