Skip to content

Instantly share code, notes, and snippets.

@beng beng/impbcopy.m forked from kenkeiter/impbcopy.m
Last active Sep 4, 2018

What would you like to do?
// Copied from
#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];

Terminal Selfies

Sometimes you need to take a quick selfie while you're working, and get it on the clipboard (er, pasteboard). With a single command, you can take a selfie and:

  • paste it into Slack to annoy your coworkers
  • send it to your significant other via iMessage
  • or just simply record your expressions of horror while computering.

Setting It Up

Use homebrew to install imagesnap, which will allow you to quickly snap an image using your built-in camera:

brew install imagesnap

Next you'll need to build a tool that allows you to copy images to the clipboard from the command line (the pbcopy command doesn't support this). Luckily, Alec Jacobson has written such a tool, and it's super easy to build. I've mirrored the code for you here; simply download the file impbcopy.m below, and run the following to compile it:

gcc -Wall -g -O3 -ObjC -framework Foundation -framework AppKit -o impbcopy impbcopy.m

You'll need to move the resulting impbcopy binary to somewhere within your PATH. I'll let you figure that out.

Final step is adding a quick function to your .bash_profile to take the photo, archive it (optionally), and copy it to the clipboard.

# directory in which to amass collection of selfies
export SELFIE_ARCHIVE_PATH="$HOME/selfies"

# function to actually take a selfie
  local filepath="$SELFIE_ARCHIVE_PATH/$(date +%y%m%d%H%M%S).png"
  imagesnap $filepath -w 1.0 # the -w flag gives the camera time to warm up
  impbcopy $filepath


Once you've reloaded your .bash_profile, simply run


and you'll have a selfie waiting for you on the clipboard, and archived for later use!


This comment has been minimized.

Copy link

commented Sep 7, 2015


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.