Skip to content

Instantly share code, notes, and snippets.

@marksharrison
Forked from kenkeiter/impbcopy.m
Created November 18, 2015 10:51
Show Gist options
  • Save marksharrison/e56368da2b85340e3e99 to your computer and use it in GitHub Desktop.
Save marksharrison/e56368da2b85340e3e99 to your computer and use it in GitHub Desktop.
Take a selfie, archive it, and copy it to the clipboard -- all from the terminal! (bash + os x)
/////////////////////////////////////////////////////////
// Copied from http://www.alecjacobson.com/weblog/?p=3816
/////////////////////////////////////////////////////////
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <unistd.h>
BOOL copy_to_clipboard(NSString *path)
{
// http://stackoverflow.com/questions/2681630/how-to-read-png-image-to-nsimage
NSImage * image;
if([path isEqualToString:@"-"])
{
// http://caiustheory.com/read-standard-input-using-objective-c
NSFileHandle *input = [NSFileHandle fileHandleWithStandardInput];
image = [[NSImage alloc] initWithData:[input readDataToEndOfFile]];
}else
{
image = [[NSImage alloc] initWithContentsOfFile:path];
}
// http://stackoverflow.com/a/18124824/148668
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];
if(argc<2)
{
printf("Usage:\n\n"
"Copy file to clipboard:\n ./impbcopy path/to/file\n\n"
"Copy stdin to clipboard:\n cat /path/to/file | ./impbcopy -");
return EXIT_FAILURE;
}
NSString *path= [NSString stringWithUTF8String:argv[1]];
BOOL success = copy_to_clipboard(path);
[pool release];
return (success?EXIT_SUCCESS:EXIT_FAILURE);
}

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
selfie(){
  mkdir -p $SELFIE_ARCHIVE_PATH
  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
}

Usage

Once you've reloaded your .bash_profile, simply run

selfie

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

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