Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sketch Plugin functions for working with files.
var writeTextToFile = function(text, filePath) {
var t = [NSString stringWithFormat:@"%@", text],
f = [NSString stringWithFormat:@"%@", filePath];
return [t writeToFile:f atomically:true encoding:NSUTF8StringEncoding error:nil];
}
var readTextFromFile = function(filePath) {
var fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filePath]) {
return [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
}
return nil;
}
var jsonFromFile = function(filePath, mutable) {
var data = [NSData dataWithContentsOfFile:filePath];
var options = mutable == true ? NSJSONReadingMutableContainers : 0
return [NSJSONSerialization JSONObjectWithData:data options:options error:nil];
}
var saveJsonToFile = function(jsonObj, filePath) {
writeTextToFile(stringify(jsonObj), filePath);
}
var stringify = function(obj, prettyPrinted) {
var prettySetting = prettyPrinted ? NSJSONWritingPrettyPrinted : 0,
jsonData = [NSJSONSerialization dataWithJSONObject:obj options:prettySetting error:nil];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
var createTempFolderNamed = function(name) {
var tempPath = getTempFolderPath(name);
createFolderAtPath(tempPath);
return tempPath;
}
var getTempFolderPath = function(withName) {
var fileManager = [NSFileManager defaultManager],
cachesURL = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject],
withName = (typeof withName !== 'undefined') ? withName : (Date.now() / 1000),
folderName = [NSString stringWithFormat:"%@", withName];
return [[cachesURL URLByAppendingPathComponent:folderName] path];
}
var createFolderAtPath = function(pathString) {
var fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:pathString]) return true;
return [fileManager createDirectoryAtPath:pathString withIntermediateDirectories:true attributes:nil error:nil];
}
var removeFileOrFolder = function(filePath) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
@nolastan

This comment has been minimized.

Copy link

@nolastan nolastan commented Oct 4, 2016

Thanks for sharing!

@pravdomil

This comment has been minimized.

Copy link

@pravdomil pravdomil commented Nov 18, 2017

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