Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mwender
Last active April 15, 2024 03:20
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save mwender/49609a18be41b45b2ae4 to your computer and use it in GitHub Desktop.
Save mwender/49609a18be41b45b2ae4 to your computer and use it in GitHub Desktop.
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)
{
// 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);
}
@mwender
Copy link
Author

mwender commented Feb 13, 2015

Instructions:

  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.

@michaelpass
Copy link

Thank you very much for this program. Runs flawlessly in mimicking pbcopy for image files. Brilliant.

@y-lee
Copy link

y-lee commented Oct 3, 2022

My thanks as well for this. The solutions I saw elsewhere using AppleScript did not work.

@lcrespilho
Copy link

My thanks too. I can confirm that in nov/2022 it's still working flawlessly.

@tigger04
Copy link

Wonderful, I've been trying to crack this nut for a long time. Works perfectly - thank you!

@ktlacaelel
Copy link

works like a charm, thank you for this 👍

@coryfklein
Copy link

I've been using this as part of a flow for using keyboard shortcuts to add drop shadows (and do other changes) to images that are in the clipboard and it has been super helpful for that. Thanks so much for sharing. I wish I knew more about how brew packages work and we could upload it to brew so you don't have to compile it on new workstations.

@mingl1
Copy link

mingl1 commented Jan 10, 2024

Thank you for this, is there a way to copy GIF as well? When I copy a GIF I just get an png file.

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