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

Script to Open Multiple Docs from a Text File

Community Expert ,
Oct 11, 2023 Oct 11, 2023

I'm having problems scripting the opening of multiple files from a text file source.

 

Example text file:

 

~/Desktop/files.log

 

~/Desktop/theFolder/theFile001.jpg
~/Desktop/theFolder/theFile002.jpg

 

There could be 1, 2 or more items to loop over in the log file.

 

 The following code opens the first file:

 

var logFile = File("~/Desktop/files.log");
logFile.open('r');
var filePath = logFile.readln();
logFile.close();
alert(filePath);
open(File(filePath));

 

However, I am having problems making it loop over each separate line in the log file to open all files (I have tried many different things without success). Can somebody please help me to open each line in the log file as a separate file? I would have thought that this would be a "common enough" request, however, I couldn't find anything that I could use.

 

TOPICS
Actions and scripting
423
Translate
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

correct answers 1 Correct answer

Community Expert , Oct 11, 2023 Oct 11, 2023

Does this help? 

var theResult = readPref ("~/Desktop/aaa.log");
for (var m = 0; m < theResult.length; m++) {
    alert (theResult[m])
};
////// read prefs file //////
function readPref (thePath) {
if (File(thePath).exists == true) {
var file = File(thePath);
file.open("r");
file.encoding= 'BINARY';
var theText = new String;
for (var m = 0; m < file.length; m ++) {
theText = theText.concat(file.readch());
};
file.close();
return theText.split("\n")
//return String(theText)
}
};
Translate
Adobe
Community Expert ,
Oct 11, 2023 Oct 11, 2023

Can you provide the log-file? 

Translate
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
Community Expert ,
Oct 11, 2023 Oct 11, 2023

Does this help? 

var theResult = readPref ("~/Desktop/aaa.log");
for (var m = 0; m < theResult.length; m++) {
    alert (theResult[m])
};
////// read prefs file //////
function readPref (thePath) {
if (File(thePath).exists == true) {
var file = File(thePath);
file.open("r");
file.encoding= 'BINARY';
var theText = new String;
for (var m = 0; m < file.length; m ++) {
theText = theText.concat(file.readch());
};
file.close();
return theText.split("\n")
//return String(theText)
}
};
Translate
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
Community Expert ,
Oct 11, 2023 Oct 11, 2023

@c.pfaffenbichler 

 

Thanks, I have attached the log file, it is just a text file with UTF-8 encoding with Unix line feeds. The script that creates the log file would conditionally write Windows LF if that was the platform running the script. 

 

Your script returns each file path correctly via the alert:

 

alert (theResult[m])

 

It was then just a simple case of swapping the alert for the open:

 

open(File(theResult[m]));

 

Thank you!

Translate
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
Community Expert ,
Nov 10, 2023 Nov 10, 2023

@c.pfaffenbichler 

 

Sorry to bother you with this again... I can't figure out how to reverse the order of the opened files, can you please help?

 

I tried to reverse the loop:

 

for (var m = theResult.length - 1; m >= 0; m--) {

 

And also tried:

 

theString.split('\n').reverse().join('\n')

 

The other alternative is to write to the log file in reverse order, but I am having different issues with that!

 

EDIT: 

OK, I worked out how to write to the log file in the required order, so no pressing need to read in reverse order... but it would be good to know for the sake of completeness.

 

Translate
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
Community Expert ,
Nov 22, 2023 Nov 22, 2023

The line you posted seems to work here. 

var theResult = readPref ("~/Desktop/example-files.log");
for (var m = theResult.length-1; m >= 0; m--) {
        alert (theResult[m])
};
////// read prefs file //////
function readPref (thePath) {
if (File(thePath).exists == true) {
var file = File(thePath);
file.open("r");
file.encoding= 'BINARY';
var theText = new String;
for (var m = 0; m < file.length; m ++) {
theText = theText.concat(file.readch());
};
file.close();
return theText.split("\n")
//return String(theText)
}
};

 

Translate
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
LEGEND ,
Nov 22, 2023 Nov 22, 2023
LATEST

I would write the log file into an array, then close the file and loop through the array. That would let you go from either end or an arbitrary line, search for a specific file, close and then re-open files, etc.

Translate
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