Skip to content

Instantly share code, notes, and snippets.

@programus
Forked from yuchuanfeng/impbcopy.m
Last active November 7, 2022 09:02
Show Gist options
  • Save programus/f66dcd95dc0549d2c22f97a5605f45f3 to your computer and use it in GitHub Desktop.
Save programus/f66dcd95dc0549d2c22f97a5605f45f3 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);
}
// compile command
// gcc -Wall -g -O3 -ObjC -framework Foundation -framework AppKit -o impbcopy impbcopy.m
// usage:
// impbcopy image.png
// or
// cat img.jpg | impbcopy
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <unistd.h>
@interface NSImage(saveAsPNGWithName)
- (void) saveAsPNGWithName:(NSString*) fileName;
@end
@implementation NSImage(saveAsPNGWithName)
- (void) saveAsPNGWithName:(NSString*) fileName
{
// Cache the reduced image
NSData *imageData = [self TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps];
[imageData writeToFile:fileName atomically:NO];
}
@end
BOOL paste_from_clipboard(NSString *path)
{
// http://stackoverflow.com/a/18124824/148668
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classArray = [NSArray arrayWithObject:[NSImage class]];
NSDictionary *options = [NSDictionary dictionary];
BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
if(ok)
{
NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
NSImage *image = [objectsToPaste objectAtIndex:0];
// http://stackoverflow.com/a/3213017/148668
[image release];
}else
{
printf("Error: Clipboard doesn't seem to contain an image.\n");
}
return ok;
}
int main(int argc, char * const argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(argc<2)
{
printf("Usage:\n\n"
"Paste clipboard to file:\n ./impbcopy path/to/file\n\n");
return EXIT_FAILURE;
}
NSString *path= [NSString stringWithUTF8String:argv[1]];
BOOL success = paste_from_clipboard(path);
[pool release];
return (success?EXIT_SUCCESS:EXIT_FAILURE);
}
// compile command
// clang -Wall -g -O3 -ObjC -framework Foundation -framework AppKit -o impbpaste impbpaste.m
// usage: impbpaste image.png
@programus
Copy link
Author

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