Skip to content

Instantly share code, notes, and snippets.

Last active Apr 27, 2022
What would you like to do?
Command line copy an image file to the clipboard in Mac OS X. See first comment for install instructions.
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <unistd.h>
BOOL copy_to_clipboard(NSString *path)
NSImage * image;
if([path isEqualToString:@"-"])
NSFileHandle *input = [NSFileHandle fileHandleWithStandardInput];
image = [[NSImage alloc] initWithData:[input readDataToEndOfFile]];
image = [[NSImage alloc] initWithContentsOfFile:path];
BOOL copied = false;
if (image != nil)
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
NSArray *copiedObjects = [NSArray arrayWithObject:image];
copied = [pasteboard writeObjects:copiedObjects];
[pasteboard release];
[image release];
return copied;
int main(int argc, char * const argv[])
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
"Copy file to clipboard:\n ./impbcopy path/to/file\n\n"
"Copy stdin to clipboard:\n cat /path/to/file | ./impbcopy -");
NSString *path= [NSString stringWithUTF8String:argv[1]];
BOOL success = copy_to_clipboard(path);
[pool release];
Copy link

mwender commented Feb 13, 2015


  1. Save this gist into a file called impbcopy.m under your $PATH (e.g. /usr/local/bin).
  2. Compile it with gcc -Wall -g -O3 -ObjC -framework Foundation -framework AppKit -o impbcopy impbcopy.m
  3. Now you should be able to run it with impbcopy path/to/file.png

This code courtesy of Alex Jacobson.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment