Newbie: Global objects in scripts
I realize this is probably a very simple question, but I have searched the forums to no avail. I would like to accomplish these steps:
1. Open the file, write some header information
2. For each pattern application, write the result
3. Close the file
I thought I could do this with three scripts, one for each step, each with a different "When to run" setting.
However, it seems that if I open a file in (Interpreted Java) script 1 (out = new FileWriter(...)), the object "out" is unknown in script 2. In some sense this is logical, but I´m sure you can see why this result is frustrating to me.
So, my question is: Can the "out" object (file) be kept open from script to script? If not, is there another way to achieve what I am outlining above?
PS: I realize that I can probably open and close the file in each script, but that doesn't seem like the "right" way to do it.
Newbie: Global objects in scripts
I tried Joel's suggestion. This is what I came up with, and it works:
// Create a FileWriter object and set session variable (excuted before pattern application).
out = new FileWriter( "filename.txt" );
session.setVariable("FILE_WRITER", out);
// Write each line (excuted after every pattern application)
out = session.getVariable("FILE_WRITER");
out.write(session.getVariable("BLABLA")+"\r\n");
// Close file (excuted after pattern application).
out = session.getVariable("FILE_WRITER");
out.close();
Newbie: Global objects in scripts
Thanks Joel for posting that. We've been meaning to post more of these types of things--scripts and such that we tend to use over and over in-house.
Here is a script that may do more than you need but it is for writing the data as a CSV.
[b]General Write to File Template[/b]
String fixString(String value)
{
if (value != null)
{
value = value.replaceAll("\"", "\'");
value = value.replaceAll("&", "&");
value = value.replaceAll("
Newbie: Global objects in scripts
Thanks for the reply, this looks like just what I was looking for. I'll experiment some more and post the result.
Newbie: Global objects in scripts
Hi,
This is how i have been doing the 3 steps you mentioned, it sounds like you might not be setting the out var to a session variable and then calling it from within step 2. Keep in mind this is to write out an xml file but you should be able to start with this -
//script to initiate the file in the SS directory
xmlWriter = new com.screenscraper.xml.XmlWriter( "MyTest.xml", "info");
session.setVariable("XML_WRITER", xmlWriter);
//script that you loop over after each pattern application to write the data
import com.screenscraper.xml.XmlWriter;
xmlWriter = session.getVariable("XML_WRITER");
MyStuff = xmlWriter.addElement("MyStuff");
xmlWriter.addElement(MyStuff, "MyItem", "This works great!");
//script to close up the file
xmlWriter = session.getVariable("XML_WRITER");
xmlWriter.close();
Hopefully this will get you off and running, good luck.
Joel