Was wondering how I would go about creating a new file through AE script....Like something generic like a text file?
I know I declare the object
var myFile = ("c:\\newFile\\MyFile.txt");
Calling this I can successfully change, copy, rename, delete the file if it exists....but how do I create a blank one?
thanks,
Creating a new file
Moderator: Paul Tuersley
-
- Posts: 22
- Joined: April 2nd, 2010, 1:14 am
Adam Ghering
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion
This will create a new file if it doesn't exist already at the current path.
Code: Select all
var myfile = new File("c:\filepath\MyFile.txt");
David Torno
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)
-
- Posts: 22
- Joined: April 2nd, 2010, 1:14 am
Yep tried that....doesn't work....
I think you can't use regular backslashes either...has to be either double \\ or invert / to work
I think the code you gave me just creates the object....but I found something
file.open() that says if you use the write command and no file exists then it creates it but I have still had no luck
myfile = ("c:\\LocalFolder\\myfile.txt"):
myfile.open(w);
but I can't get the syntax right.
I think you can't use regular backslashes either...has to be either double \\ or invert / to work
I think the code you gave me just creates the object....but I found something
file.open() that says if you use the write command and no file exists then it creates it but I have still had no luck
myfile = ("c:\\LocalFolder\\myfile.txt"):
myfile.open(w);
but I can't get the syntax right.
Adam Ghering
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion
This is what I use to create a new file on Mac.
var document = new File(~/Desktop/MyNewFile.txt);
document.open("w");
document.write(yournewtext);
document.close();
Since PC URI's are formated differently, this may help you. It will return the exact path that you choose so you know how it should be written.
For folders:
For files:
var document = new File(~/Desktop/MyNewFile.txt);
document.open("w");
document.write(yournewtext);
document.close();
Since PC URI's are formated differently, this may help you. It will return the exact path that you choose so you know how it should be written.
For folders:
Code: Select all
var folderLoc = Folder.selectDialog("Please select your plugin folder");
alert(File.decode(folderLoc));
Code: Select all
var path = File.openDialog();
alert(File.decode(path));
David Torno
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)
-
- Posts: 22
- Joined: April 2nd, 2010, 1:14 am
OOOh Snap!! worked again...thanks a lot
the problem was within the write call in the file.open("w")
I was trying to call it the way it is represented in the scripting reference without quotes. The open command is actually what creates the file if it doesn't exist. Why they don't include examples I will never know.
Here is what it looks like in the scripting reference.
open()
fileObj.open (mode[,type][,creator])
➤ r: (read) Opens for reading. If the file does not exist or cannot be found, the call
fails.
➤ w: (write) Opens a file for writing. If the file exists, its contents are destroyed. If
the file does not exist, creates a new, empty file.
➤ e: (edit) Opens an existing file for reading and writing.
➤ a: (append) Opens the file in Append mode, and moves the current position to
the end of the file.
It doesn't even give3 the proper syntax...
This is what I ended up with.
var document = new File("c:/!work/Test.txt");
document.open("w")
document.write("pause");
document.close();
thanks again works great!
the problem was within the write call in the file.open("w")
I was trying to call it the way it is represented in the scripting reference without quotes. The open command is actually what creates the file if it doesn't exist. Why they don't include examples I will never know.
Here is what it looks like in the scripting reference.
open()
fileObj.open (mode[,type][,creator])
➤ r: (read) Opens for reading. If the file does not exist or cannot be found, the call
fails.
➤ w: (write) Opens a file for writing. If the file exists, its contents are destroyed. If
the file does not exist, creates a new, empty file.
➤ e: (edit) Opens an existing file for reading and writing.
➤ a: (append) Opens the file in Append mode, and moves the current position to
the end of the file.
It doesn't even give3 the proper syntax...
This is what I ended up with.
var document = new File("c:/!work/Test.txt");
document.open("w")
document.write("pause");
document.close();
thanks again works great!
Adam Ghering
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion
You're welcome, and thank you too. I wasn't aware of "a" or "e" options I've been looping through reading in contents to arrays, doing the replace "," with "\r" and then adding that to the head of my new contents. I've got some experimenting to do now.
If this works, you've just saved me tons of time on my latest scripts.

David Torno
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)
Did a test a moment ago and it worked perfectly. You just saved me half the code writing adamghering. Thanks! 
Side note: And THIS is why I keep harping on Adobe to get a complete scripting guide together. Let's skip the political BS and get something together that the AE community can actually use!....... I actually just found myself going on a rant just now and deleted about six sentences. I will bite my tongue as this is not the spot for this rant.
Below are my new and now old way of creating a new file.
New way: (simple, straight forward and shorter)
Old way: (still works, but a much more convoluted way)

Side note: And THIS is why I keep harping on Adobe to get a complete scripting guide together. Let's skip the political BS and get something together that the AE community can actually use!....... I actually just found myself going on a rant just now and deleted about six sentences. I will bite my tongue as this is not the spot for this rant.

Below are my new and now old way of creating a new file.
New way: (simple, straight forward and shorter)
Code: Select all
//Create New txt document
var document = new File("~/Desktop/newfile.txt");
//Variable to hold carriage return character
var carReturn = "\r";
//Check if document already exists
if(document.exists){ //If it does, append more text to it
document.open("a");
document.write(carReturn + "Howdy");
document.close();
alert("New text was appended");
}else if(!document.exists){ //If it doesn't, create a brand new document and add this text
document.open("w");
document.write("This is the initial text.\rSome more text.\rYet another line.");
document.close();
alert("Brand new document saved.");
}
Code: Select all
var oldText = new Array();
var document = new File("~/Desktop/newfile.txt");
var carReturn = "\r";
//Check if document already exists
if(document.exists){ //If yes...
document.open("r"); //Open document for reading
while (!document.eof){ //Read old text until End Of File
oldText[oldText.length] = document.readln(); //Add old text to an array
}
document.close(); //Close document
oldTextString = oldText.toString(); //Convert old text to a string
oldTextFixed = oldTextString.replace(new RegExp( ",", "g" ), "\r"); //Fix old text by replacing all "," with carriage returns
document.open("w"); //Open document for writing
document.writeln(oldTextFixed + carReturn + "My new text"); //Write old fixed text then add my new text
document.close(); //Close document
alert("New text was appended");
}else if(!document.exists){ //If no...
document.open("w"); //Open document for writing
document.write("This is the initial text.\rSome more text.\rYet another line."); //Create a brand new document and add this text
document.close(); //Close document
alert("Brand new document saved.");
}
David Torno
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)
-
- Posts: 22
- Joined: April 2nd, 2010, 1:14 am
I was not aware of the carriage return value "\r" I always used the "\n" value for new line. Do you know the difference or are they the same.
Adam Ghering
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion
I have come across better universal compatibility with "\r" instead of "\n". I use them both, but if I need PC and Mac versions in one, I'll go with "\r" most times.
David Torno
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)
https://vimeo.com/davidtorno (work samples & breakdowns)
https://gumroad.com/davidtorno
http://provideocoalition.com/dtorno (tutorials)