-
Notifications
You must be signed in to change notification settings - Fork 7
Create And Read Files
fabiantheblind edited this page Jun 1, 2016
·
1 revision
main();
function main() {
var randomname = Number(new Date());
// get the text file
var filepath = "~/Desktop/" + randomname + ".txt";
var write_file = File(filepath);
if (!write_file.exists) {
// if the file does not exist create one
write_file = new File(filepath);
} else {
// if it exists ask the user if it should be overwritten
var res = confirm("The file already exists. Should I overwrite it", true, "titleWINonly");
// if the user hits no stop the script
if (res !== true) {
return;
}
}
var out; // our output
// we know already that the file exist
// but to be sure
if (write_file !== '') {
//Open the file for writing.
out = write_file.open('w', undefined, undefined);
write_file.encoding = "UTF-8";
write_file.lineFeed = "Unix"; //convert to UNIX lineFeed
// txtFile.lineFeed = "Windows";
// txtFile.lineFeed = "Macintosh";
}
// got an output?
if (out !== false) {
// loop the list and write each item to the file
write_file.writeln("Hello World!");
// always close files!
write_file.close();
}
var read_file = File(filepath);
read_file.open('r', undefined, undefined);
if (read_file !== '') {
alert("this is read only\n " + read_file.read());
read_file.close();
}
var append_file = File(filepath);
append_file.open('a', undefined, undefined);
if (append_file !== '') {
append_file.writeln("Hello I'm an appended line!");
append_file.close();
}
var read_again_file = File(filepath);
read_again_file.open('r', undefined, undefined);
if (read_again_file !== '') {
alert("this is read only again\n" + read_again_file.read());
read_again_file.close();
}
}
This wiki is mostly maintained by:
fabianmoronzirfas
Thanks to:
- JohnDarnell for fixing lots of typos.
- jsp for fixing lots of typos.
- ltfschoen for fixing typos.
- wridgers for adding more links.
- EugenTepin for several improvements.
- vamitul for improvements.
- abandonedbywolves for adding an example.
- Travis Weston
- Andy Dayton
Thanks to the students from my seminars for asking all those questions and making me start this wiki.
- adinaradke
- AnitaMei
- ce0311
- coerv
- felixharle
- FerdinandP
- Flave
- marche
- monkian
- natael
- OliverMatelowski
- PDXIII
- praktischend
- schlompf
- skaim
You are awesome.
- Arrays
- Classes
- Comments
- Conditionals
- Functions
- Inspect Properties
- Loops
- Objects
- Output And Interaction
- Recursive Functions
- Inspect Properties
- Variables And Operations
- Extended JavaScript Guide
- Bridge Talk
- Create And Read Files
- Executing Shell Commands
- ExtendScript Toolkit
- File
- Folder
- Includes JSX
- Object Watch
- Read CSV
- Read In JSON From File And DONT Eval
- Storing Data In A Target Engine
- Target an application
- XML
- Isolate Layers With the Shy Setting
- Layer Duration Change
- Render And System Call
- Rename Layers
- SourceText to Keyframes from Array
- SourceText
- System Call
- app
- Colorbrewer
- ColorGroups
- Colors And Swatches
- Delay And View
- Dialogs
- Documents
- Duplicate And Transform
- Event AfterSave
- Export IDML
- ExtendScript in InDesign Scripting DOM
- Fonts
- GeometricBounds and Coordinates
- Get named pageItems
- Graphic Lines
- Groups
- HSL Color Wheel
- Images
- Includes
- InsertionPoints
- Layers
- Line Feeds And Carrige Returns
- Locked PageItems
- loops vs everyItem
- Masterspreads
- Matrix
- Objectstyles
- Outlines Groups Alignment
- Pages And Margins
- Pathfinder
- Placeholder Text
- Rectangles Ovals Polygons
- RulerOrigin
- Scripting Labels
- Select words at insertionPoint
- Simple Find And Change Grep with FC Query
- Simple Find And Change Grep
- Simple Find And Change Text
- Spiro
- Styles
- Table Cells
- Text Analysis ID FC
- Text Analysis
- Text Find Locations
- Text
- Transformation Matricies
- TransparencySettings
- XML creation and import