Skip to main content
Participant
October 1, 2023
質問

Creating then placing a textfile with script

  • October 1, 2023
  • 返信数 3.
  • 347 ビュー

Hello, 

I am trying to understand the behavior of a script I wrote.

The goal of this sample is to generate a temporary text file and then place it in a text frame. I understand that there are significantly better ways to move a string into a text frame, but I want to understand why this in particular is not working.  

 

On the first run, I get error 29446 saying the file does not exist, I do not have permission, or it may be in use by another application. 

On the second run—after the file has clearly been generated—it works fine. 

I checked to see if the file exists, and If I could open it via the script before placing it with the following snippit. 

    // Check if the file exists
    if (tempFile.exists) {
        // Try to open the file for writing
        if (tempFile.open("w")) {
            tempFile.write("Test");
            tempFile.close();
            //Place Throws error
            myFrame.place(tempFile);
        } else {
            alert("Could Not open.");
        }
    } else {
        alert("File not found.");
    }


Please help me understand why the place function is being disagreeable with my file. 

Script below 

var myDocument = app.activeDocument; 
var tempFile; 

tempFile = stringToTextFile("Hello world!","myTempFile");
resultFrame = app.activeWindow.activePage.textFrames.add();
resultFrame.properties =
{
    geometricBounds : [0,0,10,10],
    contents : ""
};
resultFrame.place(tempFile, true);

function stringToTextFile(str, fileNameStr){
    //Takes a string and creates a  text file on the desktop. Returns File
    var desktopPath = Folder.desktop;
    var myPath = desktopPath + "/" + fileNameStr + ".txt";
    var textFile = new File(myPath);
    // write the contents of the text file
    textFile.open("w");
    textFile.write(str);
    textFile.close();
    return textFile;
    }

 

このトピックへの返信は締め切られました。

返信数 3

rob day
Community Expert
Community Expert
October 2, 2023

Hi @U+1F40D , It’s working fine for me. Are you sure your Desktop has full permissions? Have you tried a different file path?

U+1F40D作成者
Participant
October 2, 2023

I am not sure how to check if my desktop has full permissions, but the file can be placed the second and any subsequent runnings of the script. 
Also I can open/write/close the file on an initial script (imediately before I get the error) 

        if (tempFile.open("w")) {
            tempFile.write("Test");
            tempFile.close();

 This is why I believe that the file both exists, and is acessible before I place it. 

rob day
Community Expert
Community Expert
October 2, 2023

What’s your OS? Are you running the script out of the InDesign Scripts panel, or your code editor? Have you tried a different file path—maybe the document’s parent folder (app.activeDocument.filePath)?

 

Community Expert
October 2, 2023

This is a bit random for me. If the file does not exists then it creates the file and places it fine, after that the script runs fine again multiple times. However, after multiple runs if you delete the text file and then run the code, it fails quite often with the file in use error though it creates the txt file. I tried adding in sleep and also removing the file before opening it so that a fresh file is created everytime but still it doesn't work. However, if I created a unique file name each time it worked fine.

-Manan

-Manan
Robert at ID-Tasker
Legend
October 1, 2023

I'm not JS expert - but isn't it because your script is working so fast - that system is still keeping lock on this temp file and for InDesign - it is still in use ?

 

What if you add try()...catch() or simply wait few seconds ?

 

U+1F40D作成者
Participant
October 2, 2023

I forgot to include this in the original post, but I had that thought and wrote a small function to wait a few seconds. This did not help. The file exists, it just doesn't cooperate for reasons I don't understand.