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

How do I get a script to read lines from Notepad as style names?

Guide ,
Jun 13, 2025 Jun 13, 2025

Hello, everyone.
I want to put a notepad "name.txt" in the same directory as the scripts.
The style names used in all my scripts will read the ones set in "name.txt".
This way I can change them uniformly at any time.
Is there a way to do this?

 

For example, the content of my notepad looks like this, one style name per line.
In parentheses ( ) are the style names:


paraStname(mypar)
charStname(title1)
tabStname(mytab)
footerStname(footer)
objStaname(mypic)

 

Thank you very much.

TOPICS
Bug , Feature request , How to , Performance , Scripting
1.1K
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 3 Correct answers

Community Expert , Jun 13, 2025 Jun 13, 2025

You can read a text file like this:

 

//Text file to opem. Here a file named test.txt on the desktop
var p = File(Folder.desktop + "/test.txt");
var t = readFile(p)
//an array of lines from the text file
var ta = t.split("\n")

//the returned array
alert(ta);
//array’s 2nd item
alert(ta[1])


/**
* Read a text file 
* @ param path to the file 
* @ return the text 
*/
function readFile(p) {
	var f = new File(p);
	f.open("r");  
	var x = f.read();  
	f.close();
	return x; 
}

 

 

The alerts:

 

Screen Shot 13.png

 

Screen Shot 14.png

...
Translate
Community Expert , Jun 13, 2025 Jun 13, 2025

You can check out a sample code at the following link

https://community.adobe.com/t5/premiere-pro-discussions/how-to-read-json-file-in-premiere-cep-extension/m-p/10647827#M229987

Change you input file to a JSON like the following

{
    "paraStname":"mypar",
    "charStname":"title1",
    "tabStname":"mytab",
    "footerStname":"footer",
    "objStaname":"mypic"
}

After executing the script you can access myPic with the code 

contentObj.objStaname

-Manan

Translate
Guide , Jun 14, 2025 Jun 14, 2025

@Manan Joshi @rob day 

Suddenly it worked ......

Thank you good people great good people.

 

I would like to ask:
alert(objStn); can output "mypic"
alert(contentObj); but not the whole content?

The result is: [object object]


Why?

 

This is the kind of thing that will unify the settings of all scripts now.

I wonder if it would be a waste of resources and time to call json.jsx and mystyle.json all the first time.

 

//@include "json.jsx";

var script_folder = script_dir();
var jsonFile = File(script_folder + "/m
...
Translate
Community Expert ,
Jun 13, 2025 Jun 13, 2025

I did not understand your ask here. Is it just about reading the contents of the texfFile? If so it is pretty easy

  • Create a file object
  • Open the file in read mode
  • Read the file line by line using readln method
  • Close the file when done

If there is something else you want please explain better with examples.

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#File.html

-Manan

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
Guide ,
Jun 13, 2025 Jun 13, 2025

I'm using these style names in multiple scripts.
I want to get them all from this name.txt.

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 ,
Jun 13, 2025 Jun 13, 2025

Manan has given you a starting point to write the code. We are not here to write code for you. An AI tool is your next step to get starting code if you can't or won't write it yourself. We can provide guidance if you get stuck.

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
Guide ,
Jun 13, 2025 Jun 13, 2025

HI brian_p_dts

Thank you for looking out for me.
Do you have any other good ideas?

You must have a great idea and I truly hope you share it.

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 ,
Jun 13, 2025 Jun 13, 2025

You can read a text file like this:

 

//Text file to opem. Here a file named test.txt on the desktop
var p = File(Folder.desktop + "/test.txt");
var t = readFile(p)
//an array of lines from the text file
var ta = t.split("\n")

//the returned array
alert(ta);
//array’s 2nd item
alert(ta[1])


/**
* Read a text file 
* @ param path to the file 
* @ return the text 
*/
function readFile(p) {
	var f = new File(p);
	f.open("r");  
	var x = f.read();  
	f.close();
	return x; 
}

 

 

The alerts:

 

Screen Shot 13.png

 

Screen Shot 14.png

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
Guide ,
Jun 13, 2025 Jun 13, 2025

Hi rob day

I tested it and it works.

Thank you very much.
It just seems like this must have a fixed order of 0 1 2 3……?
Can't it just mess up the order?
For example, let the tabStname in the script just look for tabStname(mytab) without worrying about what line tabStname(mytab) is on.
I found this from Peter Kahrel's script.


It seems to work.

var script_folder = script_dir();
var p = File(script_folder + "/test.txt");
function script_dir() {
    try {
        return File(app.activeScript).path;
    } catch (e) {
        return File(e.fileName).path;
    }
}

 

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 ,
Jun 13, 2025 Jun 13, 2025
var ta = t.split("\n")

Converts your plain text into an array in the order it is written.

 

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
Guide ,
Jun 13, 2025 Jun 13, 2025

I wanted this, but couldn't figure it out.

What a simple idea, what a complicated process.
Do I have to write a function for each name?

//Text file to opem. Here a file named test.txt on the desktop

var script_folder = script_dir();
var p = File(script_folder + "/test.txt");
var t = readFile(p)
//an array of lines from the text file
var ta = t.split("[\n")
for (i = 0;i < ta.length;i++){
    if (footerStn == ta[i].matchAll(/footerStname\(.+\)/g));
}
footerStname = ta[i];

//the returned array
//alert(ta);
//array’s 2nd item

alert(footerStn);

 

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 ,
Jun 13, 2025 Jun 13, 2025

I'm not a javascripter, but seems like processing the array could be handled with a switch structure.

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
Guide ,
Jun 13, 2025 Jun 13, 2025

Iteration feels too time-consuming

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 ,
Jun 13, 2025 Jun 13, 2025

Keep your file as a JSON. Parse the JSON usign json2.js(google it) and you are done. No iteration just a simple statement to get value out, like the following

jsonObj.footerStname will give you the value of footerStname from the file

-Manan

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
Guide ,
Jun 13, 2025 Jun 13, 2025

Hi Manan Joshi

More than I could have imagined.
There doesn't seem to be much information, which is what makes it even more unreadable.

Perhaps XML might be less fiddly?
Too bad I don't know how.

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 ,
Jun 13, 2025 Jun 13, 2025

You can check out a sample code at the following link

https://community.adobe.com/t5/premiere-pro-discussions/how-to-read-json-file-in-premiere-cep-extens...

Change you input file to a JSON like the following

{
    "paraStname":"mypar",
    "charStname":"title1",
    "tabStname":"mytab",
    "footerStname":"footer",
    "objStaname":"mypic"
}

After executing the script you can access myPic with the code 

contentObj.objStaname

-Manan

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
Guide ,
Jun 13, 2025 Jun 13, 2025

Can't find a clue.
It doesn't seem right.

Also looked at this one below, but it seems like it's still very different from the js in ID

https://www.nhooo.com/note/qa0rq3.html

 

 

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
Guide ,
Jun 14, 2025 Jun 14, 2025

Prompted me with a colon error.
Point me in the right direction.

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
Guide ,
Jun 14, 2025 Jun 14, 2025

@Manan Joshi @rob day 

Suddenly it worked ......

Thank you good people great good people.

 

I would like to ask:
alert(objStn); can output "mypic"
alert(contentObj); but not the whole content?

The result is: [object object]


Why?

 

This is the kind of thing that will unify the settings of all scripts now.

I wonder if it would be a waste of resources and time to call json.jsx and mystyle.json all the first time.

 

//@include "json.jsx";

var script_folder = script_dir();
var jsonFile = File(script_folder + "/mystyle.json");

jsonFile.open('r');
var content = jsonFile.read();
jsonFile.close();
contentObj = JSON.eval(content);
var objStn = contentObj.objStaname;
alert(objStn);

function script_dir() {
    try {
        return File(app.activeScript).path;
    } catch (e) {
        return File(e.fileName).path;
    }
}

 

dublove_0-1749902580906.jpegdublove_1-1749902595787.jpeg

 

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 ,
Jun 14, 2025 Jun 14, 2025
quote

alert(objStn); can output "mypic"
alert(contentObj); but not the whole content?

The result is: [object object]


By @dublove

contentObj is an object so you see it right when you alert it. This is how it is supposed to work. For the whole file content you already have that in the string you read from the file and parsed into an object using the JSON.eval function

-Manan

 

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
Guide ,
Jun 16, 2025 Jun 16, 2025

Hi @Manan Joshi 

Thank you very much.

My mystyle.json has

{
"poReg018":"\\s*@([^@]+)@\\s*"
}

My jsx wants to call

var poReg18 = contentObj.poReg018;

and then use:

findWhat:poReg018,

here It doesn't seem to recognize it, saying my poReg18 is undefined.

What's wrong?

Regular expressions here, what special handling is required?

 

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 ,
Jun 16, 2025 Jun 16, 2025

Please post the whole code and the json file for me to test.

-Manan

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
Guide ,
Jun 16, 2025 Jun 16, 2025

Hi @Manan Joshi 

down here,please.

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 ,
Jun 17, 2025 Jun 17, 2025

Your code in the current state is working with no error. I am guessing that you are getting error on line 39 which is as follows

findWhat: "poReg018", //查找的正则

Now first, if you want to use the value of the variable then it should not be in "". I am sure you know that. After removing the "" we get the error you mentioned and that is because you are using wrong variable name. You have the defined 

poReg18

but trying to use 

poReg018

The following code would work

-Manan

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
Guide ,
Jun 17, 2025 Jun 17, 2025

sorry.

findWhat:
I've tried multiple ways to do this, and I've always assumed it's here for special handling.
Turns out it is:

I saw it because lost the 0 in poReg018 and got too dizzy.

 

Looks like I'll have to get a whole error correction debugging plugin.
Does anyone have any recommendations?

 

Thank you very much.

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 ,
Jun 17, 2025 Jun 17, 2025

I don't understand your post. Did my recommendation solve your issue. In short you just had a typo nothing serious. Do you want recommendations for suggesting typo errors?

-Manan

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
Guide ,
Jun 17, 2025 Jun 17, 2025

Hi Manan Joshi

 That's the point.
You pointed out my mistake.
Thank you very much.

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