Skip to main content
Known Participant
June 4, 2008
Question

[JS] About Files

  • June 4, 2008
  • 5 replies
  • 6337 views
My name is avi. I have some difficulties in my script.
My problem is i want to create a file and i want to read and write that file.
For creating a file i am using following code but it is not working. Can anyone please look at it.

var myFile = new File("~/Desktop/amit.rtf");
myFile.create();

Currently i am working on two scripts. I want to create and write a text file using first program and then i need to read that text file using second program. Can anyone please tell me how to create, read and write a file using javascript(InDesign).

I just want to write a number in that file which i can read using second program. Please help me.

Thank you,
This topic has been closed for replies.

5 replies

New Participant
May 22, 2009

I share Ann's frustration a little, though I was able to get the script going with the detailed instructs -- many thanks to Dave, this was bugging me to no end each time I opened something, at least now I can resize with a keystroke.

Why doesn't Adobe just let us set our own default screen-opening size in the Preferences?

Brainiac
April 8, 2014

Hey there,

i have to bump this thread again out of oblivion:

Was anyone able to house this script in startup stripts with an event-handler after open? I read several threads about APID, afterOpen and var myDoc = app.documents[0]; but i'm not able to get it done, due lack of JS-skills.

Has anyone done this/can provide a clue to achieving this?

Here is what didn't worked so far:

#target indesign

#targetengine "session"

main();

          

function main()

{

var myEventListener = app.eventListeners.add("afterOpen", ReziseWindow, false);

}

          

function ReziseWindow()

{

var myDoc = app.documents[0];

myPath = app.activeScript;

myParts = myPath.toString().split("/");

myParts[(myParts.length - 1)] =  "WindowDefault" + app.version.slice(0,1) + ".txt";

myNewPath = myParts.join("/");

mySettingsFile = File(myNewPath);

if (app.windows.length < 1) {

beep();

if (confirm("Kein Fenster geöffnet; Soll die Einstellungsdatei gelöscht werden?")) {

  if(mySettingsFile.exists) {

   mySettingsFile.remove();

  }

}

} else {

if (mySettingsFile.open("r")) {

  savedBounds = mySettingsFile.read();

  mySettingsFile.close();

  myBounds = savedBounds.split(",");

  for (i = 0; i<myBounds.length; i++) {

   myBounds = Number(myBounds);

  }

  app.windows[0].bounds = myBounds;

} else {

  beep();

  if (confirm("Einstellungsdatei nicht vorhanden; Aktives Fenster nutzen um Position zu speichern?")) {

   new File(mySettingsFile);

   mySettingsFile.open("w");

   mySettingsFile.write(app.windows[0].bounds);

   mySettingsFile.close();

  }

}

}

}

Inspiring
April 8, 2014

I think Startup is too early for this script to do anything. There's no window for it to operate on at that point.

You might be able to make it responsive to an onOpen event, although even that might be too early because the window comes after the event.

I've never felt the urge to do this kind of thing because I actually use a pair of instances of this script each with a shortcut to toggle between full and half screen. I even have another instance that will push the document on to my second monitor, so having any of them operate automatically is of marginal value.

Dave

_Avi_Author
Known Participant
June 5, 2008
Hello KvD

Thank you KvD.
This is what i want. This is much easier way to do the thing what i am doing.
Can you tell me where the this ScriptArgs store the value. It will store the value in system buffer or some other place.
Inspiring
April 13, 2009

Hello, Unfortunately, I am not understanding this at all. I don't know scripts. From the previous posts a while ago (which unfortunately don't work any more). I need to do some steps to rename/copy/paste the script so that my work area stays that size instead of going the whole width of the desktop and behind the palettes. But unfortunately, I don't understand what is being said here in these postings re. scripts. 

Is there someway to get a script, change it's ending and then just put it somewhere in InDesign in CS4 and it will work. I understand that I also have to do another step of making sure something is not active any more (unchecking some box).

Thank you again for all your help.

Inspiring
April 13, 2009

My windows scripts don't work in Windows ;-( because in Windows you can't turn off the Application Frame and with that on, the script can't change the positioning of the windows.

So, before we go any further, can you confirm that you are a Mac user?

Dave

Participating Frequently
June 5, 2008
Hi Avi,

Perhaps you don't need to read and write to a file.
You could also use scriptArgs.
Use app.scriptArgs.set("myNumber", myNumber) in the first script and
app.scriptArgs,get("myNumber") to get the number in your second script.

Grtn, Kees
Inspiring
June 4, 2008
Hmm, I'd use a different approach to the first bit these days:
myPath = app.activeScript;

mySettingsFile = File((myPath.parent) + "/WindowDefault" + app.version.slice(0,1) + ".txt");
Dave
Inspiring
June 4, 2008
Here's a script I use to create/read from/write to/delete a file:
//DESCRIPTION: Resets active window size or uses front window to set default size.


// Check for WindowDefault.txt; if present use it to set front window
// if not, use front window to set it after checking with user.

myPath = app.activeScript;
myParts = myPath.toString().split("/");
myParts[(myParts.length - 1)] = "WindowDefault" + app.version.slice(0,1) + ".txt";
myNewPath = myParts.join("/");

mySettingsFile = File(myNewPath);

// Before proceeding, check that there is a front window
// If not, offer user the chance to delete the current settings
if (app.windows.length < 1) {
beep();
if (confirm("No window is open; would you like to delete the settings file?")) {
// User said yes; check that it exists
if(mySettingsFile.exists) {
mySettingsFile.remove();
}
}
} else {

if (mySettingsFile.open("r")) {

savedBounds = mySettingsFile.read();
mySettingsFile.close();

myBounds = savedBounds.split(",");
for (i = 0; i<myBounds.length; i++) {
myBounds = Number(myBounds);
}
app.windows[0].bounds = myBounds;
} else {
beep();
if (confirm("Settings file is missing. Use current front window to set default?")) {
// User said: go to it.
new File(mySettingsFile);
mySettingsFile.open("w");
mySettingsFile.write(app.windows[0].bounds);
mySettingsFile.close();
}
}
}
Dave