Copy file from Terminal and paste in Mail

applescriptcopy/pasteterminal

I would like to

  • copy a file to the clipboard in terminal (as a reference like Finder does it) and then
  • when writing an email, paste it into Mail as an attachment (CMD + V).

I posted an example solution below but I'd prefer a script that does not need to open Finder.

Best Answer

The following script works but it needs to open (and close) a Finder window.

Save this to maccopy:

#!/usr/bin/osascript
on run args
  set theFile to (POSIX file (first item of args)) as alias
  tell application "Finder"
      reopen
      activate
      set selection to {}
      reveal theFile
  end tell
  delay 1
  tell app "System Events"
      keystroke "c" using command down
  end tell
  tell application "Finder"
      close window 1
  end tell
  tell application "iTerm2"
      activate
  end tell
end

and chmod +x maccopy.

Use with maccopy PATH-TO-FILE.