• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

[JS] About Files

New Here ,
Jun 04, 2008 Jun 04, 2008

Copy link to clipboard

Copied

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,
TOPICS
Scripting

Views

5.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 08, 2014 Apr 08, 2014

Copy link to clipboard

Copied

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();

  }

}

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 08, 2014 Apr 08, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 08, 2014 Apr 08, 2014

Copy link to clipboard

Copied

I thought scripts that listen to any handlers should be start up scripts. As you see

var myEventListener = app.eventListeners.add("afterOpen", ReziseWindow, false); the script should only run if any document is opened.

I thought about your idea of having multiple variations of the script, but i only need one window-position to fit my workspace near my pallettes, so it should be applied to any opened window after open.

But, curse me for not having any skills in JS, i cant get it to work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 08, 2014 Apr 08, 2014

Copy link to clipboard

Copied

I've written so few event-handling scripts that I'm not much use here, but I suspect that your problem could well be that afterOpen happens after the document opens but before the window is drawn.

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 08, 2014 Apr 08, 2014

Copy link to clipboard

Copied

I don't really know where I read this today, but I did some research today on topics regarding the "afterOpen"-handler not working before CS4 and fireing befor the window is drawn. Kasyan often suggested to use an eventhandler from APIDToolAssistant and provided a script for this (RememberDocumentsPosition.spln), but somehow it doesnt work.

In a later thread he acknowledge that the InD-handler isnt broke, but has to be used with var myDoc = app.documents[0];

But since I can script java, i dont know where to house this in your given script (which, to give you kudos here, is great the way it is)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 08, 2014 Apr 08, 2014

Copy link to clipboard

Copied

Thanks for the compliment.

re: var myDoc = app.documents[0];

The first statement of the ResizeWindow function is precisely that.

I'll try some experiments to see if I can get a handle on this, but don't hold your breath!

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 08, 2014 Apr 08, 2014

Copy link to clipboard

Copied

LATEST

I started a new topic which you may have seen, but just in case, this seems to work (I was wrong about there not being a window to work with):

#target indesign

#targetengine 'dave'

(function(){

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

          function testFunction() {

                    app.layoutWindows[0].bounds = [50, 32, 1143, 944];

          }

}())

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines