Last active
October 4, 2016 02:48
-
-
Save uhunkler/5472609 to your computer and use it in GitHub Desktop.
read and write TextToFile - JSTalk Sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Read text from a file at the given path | |
* | |
* @param {string} path given file path | |
* @return {mixed} string with text if successful, false if error | |
*/ | |
function readTextFromFile( path ) | |
{ | |
var result = false; | |
if( typeof path !== 'string' ) | |
return result; | |
// use stringWithContentsOfFile to get the text from the file | |
var getString = NSString.stringWithContentsOfFile_encoding_error( path, NSUTF8StringEncoding, null ); | |
if( getString ) | |
result = getString.toString(); | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global NSString:false, NSUTF8StringEncoding:false, doc:true */ | |
var filepath = '/Users/urshunkler2/temp', | |
filename = 'text.txt', | |
path = filepath + '/' + filename, | |
string = "Cras vestibulum nulla et eros adipiscing porta.\n" + new Date(); | |
/** | |
* Map the JSTalk print method to the Sketch doc.showMessage method | |
* @param {string} message The given message text | |
* @return {void} | |
*/ | |
var print = function (message) { | |
doc.showMessage(message); | |
}; | |
/** | |
* Write a given text to a file at the given path | |
* | |
* @param {string} text given text | |
* @param {string} path given file path | |
* @return {boolean} true if successful | |
*/ | |
function writeTextToFile(text, path) { | |
var result = false; | |
if (typeof text !== 'string') { | |
return result; | |
} | |
if (typeof path !== 'string') { | |
return result; | |
} | |
// create a NSString object from the given text | |
var nsstring = NSString.instanceWithUTF8String(text); | |
// use the writeToFile method of the NSString object to write the text to the given URL | |
result = [nsstring writeToFile:path atomically:1 encoding:NSUTF8StringEncoding error:nil]; | |
if (!result) { | |
result = false; | |
} else { | |
result = true; | |
} | |
return result; | |
} | |
/** | |
* Read text from a file at the given path | |
* | |
* @param {string} path given file path | |
* @return {mixed} string with text if successful, false if error | |
*/ | |
function readTextFromFile (path) { | |
var result = false; | |
if (typeof path !== 'string') { | |
return result; | |
} | |
// use stringWithContentsOfFile to get the text from the file | |
var getString = NSString.stringWithContentsOfFile_encoding_error(path, NSUTF8StringEncoding, nil); | |
if (getString) { | |
result = getString.toString(); | |
} | |
return result; | |
} | |
// write the text to a file | |
var ok = writeTextToFile(string, path); | |
if (!ok) { | |
print("Error writing file to " + path); | |
} else { | |
// if write was ok read the text back | |
var returnedText = readTextFromFile(path); | |
if (!returnedText) { | |
print("Error reading text from file " + path); | |
} else { | |
print(returnedText.substr(0, 15)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Write a given text to a file at the given path | |
* | |
* @param {string} text given text | |
* @param {string} path given file path | |
* @return {boolean} true if successful | |
*/ | |
function writeTextToFile( text, path ) | |
{ | |
var result = false; | |
if( typeof text !== 'string' ) | |
return result; | |
if( typeof path !== 'string' ) | |
return result; | |
// create a NSString object from the given text | |
var nsstring = NSString.instanceWithUTF8String( text ); | |
// use the writeToFile method of the NSString object to write the text to the given URL | |
result = [nsstring writeToFile:path atomically:1 encoding:NSUTF8StringEncoding error:null]; | |
if( !result ) | |
{ | |
result = false; | |
} | |
else | |
{ | |
result = true; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment