Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Often I find myself at the command line wanting to copy files to the clipboard, so that I can paste them as attachments in Mail. If I have an image file, image1.png, I can accomplish this with

osascript -e 'tell app "Finder" to set the clipboard to ( POSIX file "image1.png" )'

If I execute the above command and then open a new message in Mail and press command V, the image file is pasted as an attachment. Note that pbcopy is apparently not a solution here, because this would copy the contents of the file and result in junk when pasted with command V.

My question is how can I do the same thing with multiple files? Suppose I want to paste both image1.png and image2.png. This is trivial from Finder (select them both, command C, then switch to Mail and command V; voila, both files show up as attachments)… but how can I do this from the command line?

share|improve this question
1  
This is SUCH a pain in the tuckus from AppleScript. I would recommend choosing one of the scriptable clipboard managers or using Cocoa NSPasteboard via your favorite scripting language. One other option may be to create a folder action, so you can copy files to the folder, then have Finder select all items in the folder, copy them to the clipboard, and empty the folder. If you just want to email the files as attachments, one alternative is to iterate through your list and create Mail → new outgoing message → content → mail attachments. –  Art Taylor Oct 6 '12 at 7:43
1  
@ArtTaylor, thank you for the options. I'm working on an Applescript that does the last thing you mentioned, basically something that will tell Mail to make new attachment with properties {file name:this_file} at after last paragraph, using a repeat loop over argv. I'm wondering if the answer to the original question, however, is indeed simply "you can't do this with Applescript". –  JCOidl Oct 6 '12 at 20:00
    
I really have that feeling, but I hate to say "can't". I think the difficulty is really in creating a general solution since the Pasteboard contains several versions of the clipboard contents, allowing the paste recipient to choose an appropriate representation. –  Art Taylor Oct 7 '12 at 7:46

3 Answers 3

This isn't quite perfect for what you want, but I think it might get you closer. Quicksilver has a command line tool. When installed, you can pipe files from the command line to Quicksilver app, and from there send them as attachments in Mail.

To install Quicksilver command line tool, activate Quicksilver. Then navigate to Quicksilver > Preferences to open preferences window. Click on Preferences on top right of window. On the left, click on Command Line Tool and install.

In Terminal, you'll be able to type

qs path/to/file1 path/to/file2 path/to/file3 path/to/file4

Pressing enter will send those files to Quicksilver's first pane. Press tab to move to the action pane and choose the Mail/New Email With Attachment action. Press enter, and a new email with your selected files attached should appear.

Note that you will also have to install the Mail and Command Line plugins in Quicksilver (Quicksilver > Preferences > Plug Ins).

share|improve this answer

You can do something like :

set f to {(POSIX file "/path/to/a/folder/a.png"), (POSIX file "/path/to/another/folder/b.png")}
tell application "Finder"
    try -- to delete any old temp folder
        delete folder "AS_mailCopy" of (path to temporary items)
    end try
    set tmp to make new folder at (path to temporary items) with properties {name:"AS_mailCopy"}
    duplicate f to tmp
    select files of tmp
    activate
    tell application "System Events" to keystroke "c" using command down
    delete tmp
end tell

Now your pasteboard contains multiple files (from various folders) that you can paste into your mail.

share|improve this answer

Why not just drag and drop? Sometimes techies get overly complex and forget the simple things a Mac can do.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.