AE ENHANCERS

Expressions/Scripts/Presets
It is currently Wed May 22, 2013 3:33 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Creating a new file
PostPosted: Fri May 28, 2010 11:55 am 
Offline

Joined: Fri Apr 02, 2010 1:14 am
Posts: 22
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,

_________________
Adam Ghering
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion


Top
 Profile  
 
 Post subject: Re: Creating a new file
PostPosted: Fri May 28, 2010 11:33 pm 
Offline
User avatar

Joined: Fri Feb 23, 2007 7:00 pm
Posts: 49
Location: California
This will create a new file if it doesn't exist already at the current path.
Code:
var myfile = new File("c:\filepath\MyFile.txt");

_________________
--
David Torno
Visual Effects Artist & Supervisor
http://www.ghosttownmedia.com
—————————————————
http://www.sydefxink.com
http://aeioweyou.blogspot.com
http://mactex.blogspot.com


Top
 Profile  
 
 Post subject: Re: Creating a new file
PostPosted: Sat May 29, 2010 12:27 pm 
Offline

Joined: Fri Apr 02, 2010 1:14 am
Posts: 22
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.

_________________
Adam Ghering
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion


Top
 Profile  
 
 Post subject: Re: Creating a new file
PostPosted: Sat May 29, 2010 10:27 pm 
Offline
User avatar

Joined: Fri Feb 23, 2007 7:00 pm
Posts: 49
Location: California
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:
Code:
var folderLoc = Folder.selectDialog("Please select your plugin folder");
alert(File.decode(folderLoc));


For files:
Code:
var path = File.openDialog();
alert(File.decode(path));

_________________
--
David Torno
Visual Effects Artist & Supervisor
http://www.ghosttownmedia.com
—————————————————
http://www.sydefxink.com
http://aeioweyou.blogspot.com
http://mactex.blogspot.com


Top
 Profile  
 
 Post subject: Re: Creating a new file
PostPosted: Sun May 30, 2010 9:32 am 
Offline

Joined: Fri Apr 02, 2010 1:14 am
Posts: 22
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!

_________________
Adam Ghering
Compositing Supervisor
Legend Films inc.
2D/3D Stereo Conversion


Top
 Profile  
 
 Post subject: Re: Creating a new file
PostPosted: Sun May 30, 2010 10:16 am 
Offline
User avatar

Joined: Fri Feb 23, 2007 7:00 pm
Posts: 49
Location: California
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
Visual Effects Artist & Supervisor
http://www.ghosttownmedia.com
—————————————————
http://www.sydefxink.com
http://aeioweyou.blogspot.com
http://mactex.blogspot.com


Top
 Profile  
 
 Post subject: Re: Creating a new file
PostPosted: Sun May 30, 2010 1:47 pm 
Offline
User avatar

Joined: Fri Feb 23, 2007 7:00 pm
Posts: 49
Location: California
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)
Code:
//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.");
}


Old way: (still works, but a much more convoluted way)
Code:
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
Visual Effects Artist & Supervisor
http://www.ghosttownmedia.com
—————————————————
http://www.sydefxink.com
http://aeioweyou.blogspot.com
http://mactex.blogspot.com


Top
 Profile  
 
 Post subject: Re: Creating a new file
PostPosted: Sun May 30, 2010 5:07 pm 
Offline

Joined: Fri Apr 02, 2010 1:14 am
Posts: 22
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


Top
 Profile  
 
 Post subject: Re: Creating a new file
PostPosted: Sun May 30, 2010 5:13 pm 
Offline
User avatar

Joined: Fri Feb 23, 2007 7:00 pm
Posts: 49
Location: California
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
Visual Effects Artist & Supervisor
http://www.ghosttownmedia.com
—————————————————
http://www.sydefxink.com
http://aeioweyou.blogspot.com
http://mactex.blogspot.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group