diff --git a/MacGap.xcodeproj/project.pbxproj b/MacGap.xcodeproj/project.pbxproj index fccd784..3867774 100644 --- a/MacGap.xcodeproj/project.pbxproj +++ b/MacGap.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 88C0646014BDE10A00E4BCE2 /* Window.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C0645F14BDE10A00E4BCE2 /* Window.m */; }; 88C0646614BDEC5800E4BCE2 /* Window.xib in Resources */ = {isa = PBXBuildFile; fileRef = 88C0646414BDEC5800E4BCE2 /* Window.xib */; }; 88C0646D14BDF6A600E4BCE2 /* WindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C0646C14BDF6A600E4BCE2 /* WindowController.m */; }; + 88CAD87714D3C6A800B45DB2 /* File.m in Sources */ = {isa = PBXBuildFile; fileRef = 88CAD87614D3C6A800B45DB2 /* File.m */; }; FA32509D14BA813600BF0781 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA32509C14BA813600BF0781 /* WebKit.framework */; }; FA3250C314BA85E700BF0781 /* ContentView.m in Sources */ = {isa = PBXBuildFile; fileRef = FA3250BC14BA85E700BF0781 /* ContentView.m */; }; FA3250C514BA85E700BF0781 /* Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = FA3250BE14BA85E700BF0781 /* Utils.m */; }; @@ -53,6 +54,8 @@ 88C0646514BDEC5800E4BCE2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/Window.xib; sourceTree = ""; }; 88C0646B14BDF6A600E4BCE2 /* WindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WindowController.h; sourceTree = ""; }; 88C0646C14BDF6A600E4BCE2 /* WindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WindowController.m; sourceTree = ""; }; + 88CAD87514D3C6A800B45DB2 /* File.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = File.h; path = Classes/Commands/File.h; sourceTree = ""; }; + 88CAD87614D3C6A800B45DB2 /* File.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = File.m; path = Classes/Commands/File.m; sourceTree = ""; }; FA32509C14BA813600BF0781 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; }; FA32509E14BA816E00BF0781 /* Growl.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Growl.framework; path = MacGap/Growl.framework; sourceTree = ""; }; FA3250BA14BA85E700BF0781 /* Constants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Constants.h; path = Classes/Constants.h; sourceTree = ""; }; @@ -138,6 +141,8 @@ 88C0645F14BDE10A00E4BCE2 /* Window.m */, 88746BEC14CCA435001E160E /* JSEventHelper.h */, 88746BED14CCA435001E160E /* JSEventHelper.m */, + 88CAD87514D3C6A800B45DB2 /* File.h */, + 88CAD87614D3C6A800B45DB2 /* File.m */, ); name = Commands; sourceTree = ""; @@ -289,6 +294,7 @@ 88C0646014BDE10A00E4BCE2 /* Window.m in Sources */, 88C0646D14BDF6A600E4BCE2 /* WindowController.m in Sources */, 88746BEE14CCA435001E160E /* JSEventHelper.m in Sources */, + 88CAD87714D3C6A800B45DB2 /* File.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/MacGap.xcodeproj/project.xcworkspace/xcuserdata/liamks.xcuserdatad/UserInterfaceState.xcuserstate b/MacGap.xcodeproj/project.xcworkspace/xcuserdata/liamks.xcuserdatad/UserInterfaceState.xcuserstate index 822ed3c..518ab4f 100644 Binary files a/MacGap.xcodeproj/project.xcworkspace/xcuserdata/liamks.xcuserdatad/UserInterfaceState.xcuserstate and b/MacGap.xcodeproj/project.xcworkspace/xcuserdata/liamks.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/MacGap.xcodeproj/xcuserdata/liamks.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist b/MacGap.xcodeproj/xcuserdata/liamks.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist index 8e541ea..ba48312 100644 --- a/MacGap.xcodeproj/xcuserdata/liamks.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist +++ b/MacGap.xcodeproj/xcuserdata/liamks.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist @@ -9,11 +9,11 @@ continueAfterRunningActions = "No" isPathRelative = "1" filePath = "MacGap/Classes/WebViewDelegate.m" - timestampString = "349029710.277675" + timestampString = "349561774.548839" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "107" - endingLineNumber = "107" + startingLineNumber = "150" + endingLineNumber = "150" landmarkName = "-webView:contextMenuItemsForElement:defaultMenuItems:" landmarkType = "5"> @@ -23,11 +23,11 @@ continueAfterRunningActions = "No" isPathRelative = "1" filePath = "MacGap/Classes/WebViewDelegate.m" - timestampString = "349029710.277675" + timestampString = "349561774.548839" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "95" - endingLineNumber = "95" + startingLineNumber = "138" + endingLineNumber = "138" landmarkName = "-webView:contextMenuItemsForElement:defaultMenuItems:" landmarkType = "5"> @@ -79,11 +79,11 @@ continueAfterRunningActions = "No" isPathRelative = "1" filePath = "MacGap/Classes/WebViewDelegate.m" - timestampString = "348021074.943219" + timestampString = "349561774.548839" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "21" - endingLineNumber = "21" + startingLineNumber = "26" + endingLineNumber = "26" landmarkName = "-webView:didClearWindowObject:forFrame:" landmarkType = "5"> @@ -93,11 +93,11 @@ continueAfterRunningActions = "No" isPathRelative = "1" filePath = "MacGap/Classes/WebViewDelegate.m" - timestampString = "349029710.277675" + timestampString = "349561774.548839" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "128" - endingLineNumber = "128" + startingLineNumber = "171" + endingLineNumber = "171" landmarkName = "-webView:createWebViewWithRequest:" landmarkType = "5"> @@ -107,11 +107,11 @@ continueAfterRunningActions = "No" isPathRelative = "1" filePath = "MacGap/Classes/WebViewDelegate.m" - timestampString = "349029710.277675" + timestampString = "349561774.548839" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "133" - endingLineNumber = "133" + startingLineNumber = "176" + endingLineNumber = "176" landmarkName = "-webViewShow:" landmarkType = "5"> @@ -149,13 +149,139 @@ continueAfterRunningActions = "No" isPathRelative = "1" filePath = "MacGap/Classes/WebViewDelegate.m" - timestampString = "348947710.747279" + timestampString = "349561774.548839" + startingColumnNumber = "9223372036854775807" + endingColumnNumber = "9223372036854775807" + startingLineNumber = "84" + endingLineNumber = "84" + landmarkName = "-webView:runOpenPanelForFileButtonWithResultListener:allowMultipleFiles:" + landmarkType = "5"> + + + + + + + + + + + + + + + + + + diff --git a/MacGap/Classes/Commands/File.h b/MacGap/Classes/Commands/File.h new file mode 100644 index 0000000..06239e8 --- /dev/null +++ b/MacGap/Classes/Commands/File.h @@ -0,0 +1,23 @@ +// +// File.h +// MacGap +// +// Created by Liam Kaufman Simpkins on 12-01-28. +// Copyright (c) 2012 Twitter. All rights reserved. +// + +#import +#import "WindowController.h" + +@interface File : NSObject{ + +} + +@property (nonatomic, retain) WebView *webView; +- (id) initWithWebView:(WebView *)view; +-(NSArray *) files; +-(void) readAsText:(NSString *) path; +-(void) readAsDataURL:(NSString *) path; + +@property (retain) NSArray* selectedFiles; +@end diff --git a/MacGap/Classes/Commands/File.m b/MacGap/Classes/Commands/File.m new file mode 100644 index 0000000..907940d --- /dev/null +++ b/MacGap/Classes/Commands/File.m @@ -0,0 +1,151 @@ +// +// File.m +// MacGap +// +// Created by Liam Kaufman Simpkins on 12-01-28. +// Copyright (c) 2012 Twitter. All rights reserved. +// + +#import "File.h" +#import "Utils.h" +#import "JSEventHelper.h" + +#define NOT_READABLE_ERR 4 +#define TEXT 1 +#define BASE64 2 + +@interface File() +-(NSString *) addMimeTypeToFileOutput: (NSString *) fileOutput withFile:(NSString *) file; +-(void) readFileError; +-(void) readFileSuccessForOutput: (NSString *) fileOutput withOutputType: (int) outputType; +-(void) readFile: (NSString *) file withOutputType:(int) outputType; +-(void) readText:(NSString *) path; +-(void) readData: (NSString *) path; +@end + +@implementation File + +@synthesize selectedFiles, webView; + + +- (id) initWithWebView:(WebView*)view +{ + if(self = [super init]) { + self.webView = view; + } + return self; +} + +-(NSArray *) files{ + return self.selectedFiles; +} + +/* + The File API adds a prefix to base64 encoded files that includes its mimetype. If + the mimetype is not present, it can be ommited. + */ +-(NSString *) addMimeTypeToFileOutput: (NSString *) fileOutput withFile:(NSString *) file{ + NSString *output; + NSString * mimeType = [Utils getMIMETypesFromFile:file]; + if ([mimeType isEqualToString:@""]) { + output = [NSString stringWithFormat:@"data:base64,%@",fileOutput]; + }else{ + output = [NSString stringWithFormat:@"data:%@;base64,%@",mimeType,fileOutput]; + } + return output; +} + +/* + Since the file error is dependent on the function that is reading the file, and the same error, + from two different functions might result in a different error code, we simply return + NOT_READABLE_ERR, which is part of the File API. + */ +-(void) readFileError{ + NSString *eventWithError = [NSString stringWithFormat:@"e.error=%d", NOT_READABLE_ERR]; + [JSEventHelper triggerEvent:@"onerror" withExtraJS:eventWithError forWebView:self.webView]; +} + +/* + Once the file reading is successful, we assign it to the event object in the JS string. + */ +-(void) readFileSuccessForOutput: (NSString *) fileOutput withOutputType: (int) outputType{ + NSString * result; + if (outputType == TEXT) { + //If file's output is not escaped it results in a JavaScript EOF error + result = [NSString stringWithFormat:@"e.result=decodeURIComponent('%@')", + [fileOutput stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; + }else if(outputType == BASE64){ + result = [NSString stringWithFormat:@"e.result='%@'",fileOutput]; + } + + [JSEventHelper triggerEvent:@"onload" withExtraJS:result forWebView:self.webView]; +} + +-(void) readFile: (NSString *) file withOutputType:(int) outputType{ + NSString *fileOutput; + [JSEventHelper triggerEvent:@"onloadstart" forWebView:self.webView]; + + if (outputType == TEXT) { + NSStringEncoding *encoding; + fileOutput = [NSString stringWithContentsOfFile:file usedEncoding:encoding error:nil]; + }else if (outputType == BASE64){ + fileOutput = [self addMimeTypeToFileOutput:[Utils encodeBase64WithData:[NSData dataWithContentsOfFile:file]] withFile:file]; + } + + if (fileOutput) { + //onload + [self readFileSuccessForOutput:fileOutput withOutputType:outputType]; + }else{ + //onerror + [self readFileError]; + } + + [JSEventHelper triggerEvent:@"onloadend" forWebView:self.webView]; +} + + +-(void) readText:(NSString *) path{ + [self readFile:path withOutputType:TEXT]; +} + +-(void) readAsText:(NSString *)path{ + //Make read nonblocking! + [self performSelectorOnMainThread:@selector(readText:) withObject:path waitUntilDone:NO]; +} + +- (void) readData: (NSString *) path{ + [self readFile:path withOutputType:BASE64]; +} + +-(void) readAsDataURL:(NSString *) path{ + [self performSelectorOnMainThread:@selector(readData:) withObject:path waitUntilDone:NO]; +} + +#pragma mark WebScripting Protocol + +/* checks whether a selector is acceptable to be called from JavaScript */ ++ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector +{ + return NO; +} + + ++ (NSString*) webScriptNameForSelector:(SEL)selector +{ + id result = nil; + + if (selector == @selector(readAsText:)) { + result = @"readAsText"; + }else if (selector == @selector(readAsDataURL:)){ + result = @"readAsDataURL"; + } + + return result; +} + +// right now exclude all properties (eg keys) ++ (BOOL) isKeyExcludedFromWebScript:(const char*)name +{ + return NO; +} +@end diff --git a/MacGap/Classes/ContentView.m b/MacGap/Classes/ContentView.m index 07af9d9..b0b6650 100644 --- a/MacGap/Classes/ContentView.m +++ b/MacGap/Classes/ContentView.m @@ -6,6 +6,7 @@ @interface WebPreferences (WebPreferencesPrivate) - (void)_setLocalStorageDatabasePath:(NSString *)path; - (void) setLocalStorageEnabled: (BOOL) localStorageEnabled; - (void) setDatabasesEnabled:(BOOL)databasesEnabled; + - (void) setDeveloperExtrasEnabled:(BOOL)developerExtrasEnabled; @end @implementation ContentView @@ -21,6 +22,8 @@ - (void) awakeFromNib [webPrefs _setLocalStorageDatabasePath:savePath]; [webPrefs setLocalStorageEnabled:YES]; [webPrefs setDatabasesEnabled:YES]; + [webPrefs setDatabasesEnabled:YES]; + [webPrefs setDeveloperExtrasEnabled:YES]; [self.webView setPreferences:webPrefs]; diff --git a/MacGap/Classes/JSEventHelper.h b/MacGap/Classes/JSEventHelper.h index f9782e5..b8fa2c6 100644 --- a/MacGap/Classes/JSEventHelper.h +++ b/MacGap/Classes/JSEventHelper.h @@ -11,6 +11,7 @@ @interface JSEventHelper : NSObject ++ (void) triggerEvent:(NSString *) event withExtraJS: (NSString *) extraJS forWebView: (WebView *) webView; + (void) triggerEvent:(NSString *) event forWebView: (WebView *) webView; @end diff --git a/MacGap/Classes/JSEventHelper.m b/MacGap/Classes/JSEventHelper.m index c73a334..310366c 100644 --- a/MacGap/Classes/JSEventHelper.m +++ b/MacGap/Classes/JSEventHelper.m @@ -10,9 +10,16 @@ @implementation JSEventHelper -+ (void) triggerEvent:(NSString *) event forWebView: (WebView *) webView{ - NSString * str = [NSString stringWithFormat:@"var e = document.createEvent('Events'); e.initEvent('%@', true, false); document.dispatchEvent(e); ", event]; + ++ (void) triggerEvent:(NSString *) event withExtraJS: (NSString *) extraJS forWebView: (WebView *) webView{ + NSString * str = [NSString stringWithFormat:@"var e = document.createEvent('Events'); e.initEvent('%@', true, false); %@; document.dispatchEvent(e); ", event, extraJS]; [webView stringByEvaluatingJavaScriptFromString:str]; } ++ (void) triggerEvent:(NSString *) event forWebView: (WebView *) webView{ + [JSEventHelper triggerEvent:event withExtraJS:@"" forWebView:webView]; +} + + + @end diff --git a/MacGap/Classes/Utils.h b/MacGap/Classes/Utils.h index 1bb2986..5804303 100644 --- a/MacGap/Classes/Utils.h +++ b/MacGap/Classes/Utils.h @@ -4,6 +4,9 @@ #define fequal(a,b) (fabs((a) - (b)) < DEG_EPS) #define fequalzero(a) (fabs(a) < DEG_EPS) + + + @class LoadingView; @interface Utils : NSObject { @@ -13,5 +16,6 @@ - (NSString*) pathForResource:(NSString*)resourcepath; + (Utils*) sharedInstance; - ++ (NSString *) getMIMETypesFromFile: (NSString *) path; ++ (NSString *)encodeBase64WithData:(NSData *)objData; @end diff --git a/MacGap/Classes/Utils.m b/MacGap/Classes/Utils.m index 06dfe75..29403d1 100644 --- a/MacGap/Classes/Utils.m +++ b/MacGap/Classes/Utils.m @@ -55,4 +55,73 @@ - (id) copyWithZone:(NSZone *)zone return self; } ++ (NSString *) getMIMETypesFromFile: (NSString *) path{ + NSString * uniformTypeIndentifier = [[NSWorkspace sharedWorkspace] typeOfFile:path error:nil]; + + //List of Uniform Type Identifiers + // http://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html + + NSDictionary * mimeTypes = [NSDictionary dictionaryWithObjectsAndKeys: + @"",@"public.camera-raw-image", + @"image/bmp",@"com.microsoft.bmp", + @"image/gif",@"com.compuserve.gif ", + @"image/jpeg",@"public.jpeg", + @"image/png",@"public.png", + @"image/svg+xml",@"public.svg-image", + @"image/tiff",@"public.tiff", + @"image/vnd.microsoft.icon",@"com.microsoft.ico", + nil]; + + return (NSString *)[mimeTypes valueForKey:uniformTypeIndentifier]; +} + +static const char _base64EncodingTable[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +// Code from: https://github.com/mikeho/QSUtilities/blob/master/QSStrings.m ++ (NSString *)encodeBase64WithData:(NSData *)objData { + const unsigned char * objRawData = [objData bytes]; + char * objPointer; + char * strResult; + + // Get the Raw Data length and ensure we actually have data + //original code (int intLength = [objData length];) gave warnings. + unsigned long intLength = [objData length]; + if (intLength == 0) return nil; + // Setup the String-based Result placeholder and pointer within that placeholder + strResult = (char *)calloc(((intLength + 2) / 3) * 4, sizeof(char)); + objPointer = strResult; + // Iterate through everything + while (intLength > 2) { // keep going until we have less than 24 bits + *objPointer++ = _base64EncodingTable[objRawData[0] >> 2]; + *objPointer++ = _base64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)]; + *objPointer++ = _base64EncodingTable[((objRawData[1] & 0x0f) << 2) + (objRawData[2] >> 6)]; + *objPointer++ = _base64EncodingTable[objRawData[2] & 0x3f]; + + // we just handled 3 octets (24 bits) of data + objRawData += 3; + intLength -= 3; + } + + // now deal with the tail end of things + if (intLength != 0) { + *objPointer++ = _base64EncodingTable[objRawData[0] >> 2]; + if (intLength > 1) { + *objPointer++ = _base64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)]; + *objPointer++ = _base64EncodingTable[(objRawData[1] & 0x0f) << 2]; + *objPointer++ = '='; + } else { + *objPointer++ = _base64EncodingTable[(objRawData[0] & 0x03) << 4]; + *objPointer++ = '='; + *objPointer++ = '='; + } + } + + // Terminate the string-based result + *objPointer = '\0'; + + // Return the results as an NSString object + return [NSString stringWithCString:strResult encoding:NSASCIIStringEncoding]; +} + + @end \ No newline at end of file diff --git a/MacGap/Classes/WebViewDelegate.h b/MacGap/Classes/WebViewDelegate.h index 9b09529..ce9854d 100644 --- a/MacGap/Classes/WebViewDelegate.h +++ b/MacGap/Classes/WebViewDelegate.h @@ -7,6 +7,7 @@ @class Path; @class App; @class Window; +@class File; @class WindowController; @@ -17,6 +18,7 @@ Path* path; App* app; Window* window; + File* file; } @@ -27,6 +29,7 @@ @property (nonatomic, retain) Path* path; @property (nonatomic, retain) App* app; @property (nonatomic, retain) Window* window; +@property (nonatomic, retain) File* file; @property (nonatomic, retain) WindowController *requestedWindow; diff --git a/MacGap/Classes/WebViewDelegate.m b/MacGap/Classes/WebViewDelegate.m index 4493c40..47d884c 100644 --- a/MacGap/Classes/WebViewDelegate.m +++ b/MacGap/Classes/WebViewDelegate.m @@ -5,7 +5,11 @@ #import "Path.h" #import "App.h" #import "Window.h" +#import "File.h" #import "WindowController.h" + +#define MUTABLE_ARRAY_START_SIZE 10 + @implementation WebViewDelegate @synthesize sound; @@ -14,6 +18,7 @@ @implementation WebViewDelegate @synthesize path; @synthesize app; @synthesize window; +@synthesize file; @synthesize requestedWindow; - (void) webView:(WebView*)webView didClearWindowObject:(WebScriptObject*)windowScriptObject forFrame:(WebFrame *)frame @@ -30,20 +35,58 @@ - (void) webView:(WebView*)webView didClearWindowObject:(WebScriptObject*)window if (self.window == nil) { self.window = [[Window alloc] initWithWebView:webView]; } + if (self.file == nil) { + self.file = [[File alloc] initWithWebView:webView]; + } [windowScriptObject setValue:self forKey:kWebScriptNamespace]; } +- (void)webView: (WebView *) sender willPerformDragDestinationAction:(WebDragDestinationAction)action forDraggingInfo:(id)draggingInfo{ + NSString * localHostPrefix = @"file://localhost"; + NSMutableArray *files = [NSMutableArray arrayWithCapacity:MUTABLE_ARRAY_START_SIZE]; + if (action == WebDragDestinationActionDHTML) { + NSPasteboard *pboard = [draggingInfo draggingPasteboard]; + + if ( [[pboard types] containsObject:NSURLPboardType] ) { + //NSURL *fileURL = [NSURL URLFromPasteboard:pboard]; + NSArray *pbItems = [pboard pasteboardItems]; + for(NSPasteboardItem *pi in pbItems){ + NSArray * types = [pi types]; + NSString *url; + for(NSString *t in types){ + if ([t hasSuffix:@"url"]) { + url = [pi stringForType:t]; + break; + } + } + + if([url hasPrefix:localHostPrefix]){ + [files addObject: [url substringFromIndex:[localHostPrefix length]]]; + }else{ + [files addObject: url]; + } + } + + } + + } + self.file.selectedFiles = files; +} + + - (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener allowMultipleFiles:(BOOL)allowMultipleFiles{ NSOpenPanel * openDlg = [NSOpenPanel openPanel]; [openDlg setCanChooseFiles:YES]; [openDlg setCanChooseDirectories:NO]; + [openDlg setAllowsMultipleSelection:allowMultipleFiles]; [openDlg beginWithCompletionHandler:^(NSInteger result){ NSArray * files = [[openDlg URLs] valueForKey: @"relativePath"]; + self.file.selectedFiles = files; [resultListener chooseFilenames: files] ; }]; } diff --git a/README.md b/README.md index c24149e..c5a98df 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,32 @@ Window: // Move window (Bottom left is x:0 and y:0) macgap.window.move({x:0, y: 200}); + +File: + + // After a user has selected files from a either ``, or with drag and drop, + // their absolute paths can be obtained with + macgap.file.files(); + + //The following two methods will start reading the contents of a file. + //On successful completion the 'onload' event will be triggered. + //The 'onload' event object will contain the data from the file (event.results). + + // Results in String (add event listener to 'onload' to get results) + macgap.file.readAsText('/path/to/file'); + + // Results in Base64 String (add event listener to 'onload' to get results) + macgap.file.readAsDataURL('/path/to/file'); + + //Available events: onerror, onload, onloadend, onloadstart + //Example: + + var fileContent = function(evt){ + $("img").src = evt.result; + }; + + document.addEventListener('onload', fileContent, true); + macgap.file.readAsDataURL('/path/to/imag.png'); Path: diff --git a/public/index.html b/public/index.html index 37820cf..45bb178 100644 --- a/public/index.html +++ b/public/index.html @@ -21,7 +21,7 @@ } - +