Copy link to clipboard
Copied
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.
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:
You can check out a sample code at the following link
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
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
...
Copy link to clipboard
Copied
I did not understand your ask here. Is it just about reading the contents of the texfFile? If so it is pretty easy
If there is something else you want please explain better with examples.
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#File.html
-Manan
Copy link to clipboard
Copied
I'm using these style names in multiple scripts.
I want to get them all from this name.txt.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
var ta = t.split("\n")
Converts your plain text into an array in the order it is written.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
I'm not a javascripter, but seems like processing the array could be handled with a switch structure.
Copy link to clipboard
Copied
Iteration feels too time-consuming
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
You can check out a sample code at the following link
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Prompted me with a colon error.
Point me in the right direction.
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Please post the whole code and the json file for me to test.
-Manan
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Manan Joshi
That's the point.
You pointed out my mistake.
Thank you very much.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now