Copy file from Terminal and paste in Mail

by laktak   Last Updated June 12, 2019 20:12 PM

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.

Answers 1

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

Save this to maccopy:

on run args
  set theFile to (POSIX file (first item of args)) as alias
  tell application "Finder"
      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"
  end tell

and chmod +x maccopy.

Use with maccopy PATH-TO-FILE.

June 12, 2019 19:35 PM

Related Questions

Updated October 10, 2018 17:12 PM

Updated August 12, 2017 20:12 PM

Updated April 04, 2017 08:12 AM

Updated May 04, 2016 08:05 AM