Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.
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
6 changes: 6 additions & 0 deletions MacGap.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
1495814F15C15CCC00E1CFE5 /* Notice.m in Sources */ = {isa = PBXBuildFile; fileRef = 1495814E15C15CCC00E1CFE5 /* Notice.m */; };
70CE270018D5B52B00024D3D /* Localfile.m in Sources */ = {isa = PBXBuildFile; fileRef = 70CE26FF18D5B52B00024D3D /* Localfile.m */; };
88746BEE14CCA435001E160E /* JSEventHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 88746BED14CCA435001E160E /* JSEventHelper.m */; };
88C0646014BDE10A00E4BCE2 /* Window.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C0645F14BDE10A00E4BCE2 /* Window.m */; };
88C0646614BDEC5800E4BCE2 /* Window.xib in Resources */ = {isa = PBXBuildFile; fileRef = 88C0646414BDEC5800E4BCE2 /* Window.xib */; };
Expand Down Expand Up @@ -50,6 +51,8 @@
/* Begin PBXFileReference section */
1495814D15C15CCC00E1CFE5 /* Notice.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Notice.h; path = Classes/Commands/Notice.h; sourceTree = "<group>"; };
1495814E15C15CCC00E1CFE5 /* Notice.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Notice.m; path = Classes/Commands/Notice.m; sourceTree = "<group>"; };
70CE26FE18D5B52B00024D3D /* Localfile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Localfile.h; path = Classes/Commands/Localfile.h; sourceTree = "<group>"; };
70CE26FF18D5B52B00024D3D /* Localfile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Localfile.m; path = Classes/Commands/Localfile.m; sourceTree = "<group>"; };
88746BEC14CCA435001E160E /* JSEventHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JSEventHelper.h; path = Classes/JSEventHelper.h; sourceTree = "<group>"; };
88746BED14CCA435001E160E /* JSEventHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = JSEventHelper.m; path = Classes/JSEventHelper.m; sourceTree = "<group>"; };
88C0645E14BDE10A00E4BCE2 /* Window.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Window.h; path = Classes/Window.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -149,6 +152,8 @@
88746BED14CCA435001E160E /* JSEventHelper.m */,
F2B80014179E0FC100B069A8 /* Clipboard.h */,
F2B80015179E0FC100B069A8 /* Clipboard.m */,
70CE26FE18D5B52B00024D3D /* Localfile.h */,
70CE26FF18D5B52B00024D3D /* Localfile.m */,
);
name = Commands;
sourceTree = "<group>";
Expand Down Expand Up @@ -301,6 +306,7 @@
88C0646014BDE10A00E4BCE2 /* Window.m in Sources */,
88C0646D14BDF6A600E4BCE2 /* WindowController.m in Sources */,
88746BEE14CCA435001E160E /* JSEventHelper.m in Sources */,
70CE270018D5B52B00024D3D /* Localfile.m in Sources */,
1495814F15C15CCC00E1CFE5 /* Notice.m in Sources */,
F2B80016179E0FC100B069A8 /* Clipboard.m in Sources */,
);
Expand Down
15 changes: 15 additions & 0 deletions MacGap/Classes/Commands/Localfile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Localfile.h
// Kato
//
// Created by JLarky on 16.03.14.
// Copyright (c) 2014 LeChat, Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Localfile : NSObject

- (NSString *) read:(NSString*)fileNeeded;

@end
78 changes: 78 additions & 0 deletions MacGap/Classes/Commands/Localfile.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// Localfile.m
// Kato
//
// Created by JLarky on 16.03.14.
// Copyright (c) 2014 LeChat, Inc. All rights reserved.
//

#import "Localfile.h"

@implementation Localfile

- (NSString *) read:(NSString*)fileNeeded {
NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:@"unknown", @"error", nil];
NSString *whitelistFile = [[Utils sharedInstance] pathForResource:@"public/localfiles.whitelist"];
if (!whitelistFile) {
result = [NSDictionary dictionaryWithObjectsAndKeys:@"localfiles.whitelist can't be read", @"error", nil];
} else {
NSString* fileContents =
[NSString stringWithContentsOfFile:whitelistFile
encoding:NSUTF8StringEncoding error:nil];
// separate by new line
NSArray* allLinedStrings =
[fileContents componentsSeparatedByCharactersInSet:
[NSCharacterSet newlineCharacterSet]];
// search for fileNeeded in whitelist
if ([allLinedStrings indexOfObject:fileNeeded] == NSNotFound) {
result = [NSDictionary dictionaryWithObjectsAndKeys:@"requested file not in localfiles.whitelist", @"error", nil];
} else {
NSString *filename;
NSString *resourceFile = [[Utils sharedInstance] pathForResource:fileNeeded];
if (resourceFile) {
filename = resourceFile;
} else {
filename =[fileNeeded stringByExpandingTildeInPath];
}
NSError *readError = nil;
NSString* content =
[NSString stringWithContentsOfFile:filename
encoding:NSUTF8StringEncoding
error:&readError];
if (readError) {
result = [NSDictionary dictionaryWithObjectsAndKeys:[readError localizedDescription], @"error", nil];
} else {
result = [NSDictionary dictionaryWithObjectsAndKeys:content, @"content", nil];
}
}
}
NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return jsonString;
}

// macgap.localfile.read("~/Library/Application Support/Kato/userstyle.css")

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

if (selector == @selector(read:)) {
result = @"read";
}

return result;
}

+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector
{
return NO;
}

+ (BOOL) isKeyExcludedFromWebScript:(const char*)name
{
return YES;
}

@end
3 changes: 3 additions & 0 deletions MacGap/Classes/WebViewDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@class Growl;
@class Notice;
@class Path;
@class Localfile;
@class App;
@class Window;
@class Clipboard;
Expand All @@ -18,6 +19,7 @@
Growl* growl;
Notice* notice;
Path* path;
Localfile* localfile;
App* app;
Window* window;
Clipboard* clipboard;
Expand All @@ -30,6 +32,7 @@
@property (nonatomic, retain) Growl* growl;
@property (nonatomic, retain) Notice* notice;
@property (nonatomic, retain) Path* path;
@property (nonatomic, retain) Localfile* localfile;
@property (nonatomic, retain) App* app;
@property (nonatomic, retain) Window* window;
@property (nonatomic, retain) Clipboard* clipboard;
Expand Down
3 changes: 3 additions & 0 deletions MacGap/Classes/WebViewDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "Growl.h"
#import "Notice.h"
#import "Path.h"
#import "Localfile.h"
#import "App.h"
#import "Window.h"
#import "WindowController.h"
Expand All @@ -15,6 +16,7 @@ @implementation WebViewDelegate
@synthesize growl;
@synthesize notice;
@synthesize path;
@synthesize localfile;
@synthesize app;
@synthesize window;
@synthesize requestedWindow;
Expand All @@ -27,6 +29,7 @@ - (void) webView:(WebView*)webView didClearWindowObject:(WebScriptObject*)window
if (self.growl == nil) { self.growl = [Growl new]; }
if (self.notice == nil && [Notice available] == YES) { self.notice = [Notice new]; }
if (self.path == nil) { self.path = [Path new]; }
if (self.localfile == nil) { self.localfile = [Localfile new]; }
if (self.clipboard == nil) { self.clipboard = [Clipboard new]; }

if (self.app == nil) {
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Path:
// Path to application's resources
macgap.path.resource;

Local files:
// read local file (must be included in public/localfiles.whitelist)
macgap.localfile.read('~/file.txt');

Dock:

// Set the Dock's badge
Expand Down
2 changes: 2 additions & 0 deletions public/localfiles.whitelist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public/userstyle.css
public/userscript.js