Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Add pasteImage API to handle paste image #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MacGap/Clipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

- (void) copy:(NSString*)text;
- (NSString *) paste;
- (NSString *) pasteImage;

@end
18 changes: 18 additions & 0 deletions MacGap/Clipboard.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ - (NSString *) paste {
return @"";
}

- (NSString *) pasteImage {
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];
[image lockFocus];
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, image.size.width, image.size.height)];
[image unlockFocus];
NSData *imageData = [bitmapRep representationUsingType:NSPNGFileType properties:nil];;
NSString *base64String = [imageData base64EncodedStringWithOptions:0];
return base64String;
}
return @"";
}

+ (NSString*) webScriptNameForSelector:(SEL)selector
{
id result = nil;
Expand Down