Copy link to clipboard
Copied
Hi Sunil Yadav.
I'm sorry.
I missed you and didn't reply in time.
The script provided by you can insert the path in the text containing @d:\pic\a.jpg@ directly into the image.
But when the picture is too large, the text box will be too wide to display.
How to apply an object style to an imported graph directly? For example, "mypic" object style.
So I can use mypic to control the position and size of the image.
Thank you very much.
---------------------------------------------------------------
Here's the script you provided
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = app.documents[0].findGrep();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/[\s]+/g,'').replace(/\\/g,'/'));
if(filePath.exists){
if(allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[-1].index+1].contents == "@"){
allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[-1].index+1].contents = "";
app.documents[0].recompose();
}
allFound[i].texts[0].contents = "";
allFound[i].texts[0].insertionPoints[0].place(filePath);
if(allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[0].index-1].contents == "@"){
allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[0].index-1].contents = "";
app.documents[0].recompose();
}
}
else{
i++;
}
app.documents[0].recompose();
var allFound = app.documents[0].findGrep();
}
Run this script :
//======================
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = getAll();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
var parentFrame = allFound[i].insertionPoints[0].parentTextFrames[0];
var oldGB
...
Try this one :
//======================
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = getAll();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
var parentFrame = allFound[i].insertionPoints[0].parentTextFrames[0];
var oldGB
...
Copy link to clipboard
Copied
Hi,
You just need to add one line while placing the object. Try this snippet:
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = app.documents[0].findGrep();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/[\s]+/g,'').replace(/\\/g,'/'));
if(filePath.exists){
if(allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[-1].index+1].contents == "@"){
allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[-1].index+1].contents = "";
app.documents[0].recompose();
}
allFound[i].texts[0].contents = "";
var object = allFound[i].texts[0].insertionPoints[0].place(filePath);
//=================apply Object Style
object[0].parent.appliedObjectStyle = app.documents[0].objectStyles.item("mypic");
//======================
if(allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[0].index-1].contents == "@"){
allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[0].index-1].contents = "";
app.documents[0].recompose();
}
}
else{
i++;
}
app.documents[0].recompose();
var allFound = app.documents[0].findGrep();
}
Best
Sunil
Copy link to clipboard
Copied
For example, the long path below is invalid for the script, but it can be used in Indesign
E:\G-工作up\S-新排版\G-乐高年鉴-2020\彩\好达年鉴 彩页\4领导调研\006-10月30日,国家民政部党组成员、副部长李文,国家民政部政策法规司司长刘行等一行深入区开展民政工作专题调研1参观社区党建工作2019.10.30 (1).tif
Sometimes,
The path behind @ was deleted, but the picture did not come out.
and prompts:
I can't guess why, but it feels good the first time, there will be errors afterwards, even if it is the same picture, it will go wrong after multiple executions.
Also, the script should only target the selection, not all.
Otherwise, there are too many changes and it may not be safe to control.
Copy link to clipboard
Copied
Can you share your IDML.
So that I can be able to test it.
Check Path of your folder whether it contains any special characters, if there is any remove it. And again you try it.
It will place.
Best
Sunil
Copy link to clipboard
Copied
Hi~ Sunil Yadav.
Sample files are not necessary.
Because my file is nothing special.
I just create a new ID file at will, and then input 2-3 lines of Chinese characters. Join anywhere@@
I think the key point is the poor support for long path, especially for Chinese character path.
You can find a picture and test it under the path I provide.
To test it again, use two @ @ @ in a paragraph, like this:
-------------------------------------------------------------------
这段文字是任意打的,这里有两个@your path1@这是第一张图,接着又有一张图@your path2@多试几种情况就有可能遇到问题。
-------------------------------------------------------------------
I hope this script works only for selection, for all possible insecurity.
My ID version: InDesign CC 2021 latest version.
Thank you very much
Copy link to clipboard
Copied
Do remember one thing, In Windows OS length of file path can be at max upto 259 characters including "/" as well.
If path is longer than that, it will not take. And moreover if there are any special characters which are not allowed to come in file path () special characters, you need to be aware of those too.
As far as script is concern, if you can provide me yor sample IDML file with those path in the file, or may be just those sample paths in a XML file or just those string, then I can be able to test it & try to resolve your concern.
Best
Sunil
Copy link to clipboard
Copied
Hello~
Please use Chinese path.
Is my script wrong?
Tonight, I came across 006.jpg on another computer. If I put it directly on disk D, the import will also prompt wrong.
The files used are here (valid for 21 days)
Best wishes
Copy link to clipboard
Copied
Try this updated snippet & let me know :
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = app.documents[0].findGrep();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
if(allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[-1].index+1].contents == "@"){
allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[-1].index+1].contents = "";
app.documents[0].recompose();
}
var object = allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[-1].index+1].insertionPoints[0].place(filePath);
//=================apply Object Style
if(app.documents[0].objectStyles.item("mypic").isValid){
object[0].parent.appliedObjectStyle = app.documents[0].objectStyles.item("mypic");
}
//======================
if(allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[0].index-1].contents == "@"){
var charToRemove = allFound[i].insertionPoints[0].parent.characters[allFound[i].texts[0].characters[0].index-1];
allFound[i].texts[0].contents = "";
charToRemove.contents = "";
app.documents[0].recompose();
}
}
else{
i++;
}
app.documents[0].recompose();
var allFound = app.documents[0].findGrep();
}
Best
Sunil
Copy link to clipboard
Copied
There seems to be no problem.
It's just a run, and the paths on all the pages have changed.
If you it can target at the current text frame, it is very safe and perfect.
Thank you very much
Copy link to clipboard
Copied
Here is the script for such. This script will find only inside the first selected Text Frame. If there is no frame is selected code will find it in entire document.
//======================
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = getAll();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
var object = allFound[i].insertionPoints[0].parent.characters.itemByRange(allFound[i].texts[0].characters[0].index-1, allFound[i].texts[0].characters[-1].index+1).place(filePath);
app.documents[0].recompose();
if(app.documents[0].objectStyles.item("mypic").isValid){
object[0].parent.appliedObjectStyle = app.documents[0].objectStyles.item("mypic");
}
}
else i++;
app.documents[0].recompose();
allFound = getAll();
}
//======================
function getAll(){
if(app.selection != null){
if(app.selection[0] instanceof TextFrame) return app.selection[0].findGrep();
else return app.documents[0].findGrep();
}
else return app.documents[0].findGrep();
}
//======================
Best
Sunil
Copy link to clipboard
Copied
The previous script is right, but this one seems to have some problems
Object mypic not recognized, object style not applied
There is an error prompt:
Copy link to clipboard
Copied
After placing image, that text frame is going overset. Because of that code is not able to get that object to apply object style. For this you add few lines of code.
Most importantly, without having full understanding of code never say this code is wrong or that code is right.
First get complete understanding, then proceed further.
Try this snippet:
//======================
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = getAll();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
var parentFrame = allFound[i].insertionPoints[0].parentTextFrame[0];
var object = allFound[i].insertionPoints[0].parent.characters.itemByRange(allFound[i].texts[0].characters[0].index-1, allFound[i].texts[0].characters[-1].index+1).place(filePath);
app.documents[0].recompose();
var oldGB = parentFrame.geometricBounds;
if(parentFrame.overflows){
parentFrame.fit(FitOptions.FRAME_TO_CONTENT);
}
if(app.documents[0].objectStyles.item("mypic").isValid){
object[0].parent.appliedObjectStyle = app.documents[0].objectStyles.item("mypic");
}
parentFrame.geometricBounds = oldGB;
}
else i++;
app.documents[0].recompose();
allFound = getAll();
}
//======================
function getAll(){
if(app.selection != null){
if(app.selection[0] instanceof TextFrame) return app.selection[0].findGrep();
else return app.documents[0].findGrep();
}
else return app.documents[0].findGrep();
}
//======================
Best
Sunil
Copy link to clipboard
Copied
Hello
Sorry, I still can't run the script, my example is general, please take a look for me again.
Copy link to clipboard
Copied
can you share that file again?
Copy link to clipboard
Copied
Try this below snippet. If still problem persists please share that file again?
//======================
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = getAll();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
var parentFrame = allFound[i].insertionPoints[0].parentTextFrames[0];
var object = allFound[i].insertionPoints[0].parent.characters.itemByRange(allFound[i].texts[0].characters[0].index-1, allFound[i].texts[0].characters[-1].index+1).place(filePath);
app.documents[0].recompose();
var oldGB = parentFrame.geometricBounds;
if(parentFrame.overflows){
parentFrame.fit(FitOptions.FRAME_TO_CONTENT);
}
if(app.documents[0].objectStyles.item("mypic").isValid){
object[0].parent.appliedObjectStyle = app.documents[0].objectStyles.item("mypic");
}
parentFrame.geometricBounds = oldGB;
}
else i++;
app.documents[0].recompose();
allFound = getAll();
}
//======================
function getAll(){
if(app.selection != null){
if(app.selection[0] instanceof TextFrame) return app.selection[0].findGrep();
else return app.documents[0].findGrep();
}
else return app.documents[0].findGrep();
}
//======================
Best
Sunil
Copy link to clipboard
Copied
Thank you~
Copy link to clipboard
Copied
Please download this file
Copy link to clipboard
Copied
Run this script :
//======================
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = getAll();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
var parentFrame = allFound[i].insertionPoints[0].parentTextFrames[0];
var oldGB = parentFrame.geometricBounds;
var startIndex = allFound[i].texts[0].characters[0].index-1;
var parentStory = allFound[i].insertionPoints[0].parent;
var object = allFound[i].insertionPoints[0].parent.characters.itemByRange(allFound[i].texts[0].characters[0].index-1, allFound[i].texts[0].characters[-1].index+1).place(filePath);
app.documents[0].recompose();
if(parentFrame.overflows){
parentFrame.fit(FitOptions.FRAME_TO_CONTENT);
}
var myObj = parentStory.characters[startIndex].rectangles[0];
try{
if(app.documents[0].objectStyles.item("mypic").isValid){
myObj.appliedObjectStyle = app.documents[0].objectStyles.item("mypic");
}
}
catch(e){}
parentFrame.geometricBounds = oldGB;
}
else i++;
app.documents[0].recompose();
allFound = getAll();
}
//======================
function getAll(){
if(app.selection != null){
if(app.selection[0] instanceof TextFrame) return app.selection[0].findGrep();
else return app.documents[0].findGrep();
}
else return app.documents[0].findGrep();
}
//======================
Best
Sunil
Copy link to clipboard
Copied
It works.
If there are two text boxes. (you can copy two of the same)
① You select a single text box and execute
Only the pictures in this text box are imported.
The results are as follows
② If you select nothing, all the pictures in the text box will be imported.
③ If you select the text in a text box, all the pictures in the text box will be imported
Copy link to clipboard
Copied
Try this code :
//======================
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = getAll();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
var parentFrame = allFound[i].insertionPoints[0].parentTextFrames[0];
var oldGB = parentFrame.geometricBounds;
var startIndex = allFound[i].texts[0].characters[0].index-1;
var parentStory = allFound[i].insertionPoints[0].parent;
var object = allFound[i].insertionPoints[0].parent.characters.itemByRange(allFound[i].texts[0].characters[0].index-1, allFound[i].texts[0].characters[-1].index+1).place(filePath);
app.documents[0].recompose();
if(parentFrame.overflows){
parentFrame.fit(FitOptions.FRAME_TO_CONTENT);
}
var myObj = parentStory.characters[startIndex].rectangles[0];
try{
if(app.documents[0].objectStyles.item("mypic").isValid){
myObj.appliedObjectStyle = app.documents[0].objectStyles.item("mypic");
}
}
catch(e){}
parentFrame.geometricBounds = oldGB;
}
else i++;
app.documents[0].recompose();
allFound = getAll();
}
//======================
function getAll(){
if(app.selection != null){
for(var s = 0; s < app.selection.length; s++){
if(app.selection[s] instanceof TextFrame){
if(app.selection[s].findGrep().length > 0){
return app.selection[s].findGrep();
}
}
else if(app.selection[s] instanceof TextStyleRange){
return app.selection[s].findGrep();
}
else return app.documents[0].findGrep();
}
return [];
}
else return app.documents[0].findGrep();
}
//======================
Best
Sunil
Copy link to clipboard
Copied
The report is the same as above. It's just that the source is different.
You can test it by copying two text boxes.
When I select content, I should only execute the selected content.
Even if there are two paths in a text box, only the selected path should be executed
Copy link to clipboard
Copied
Try this :
//======================
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = getAll();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
var parentFrame = allFound[i].insertionPoints[0].parentTextFrames[0];
var oldGB = parentFrame.geometricBounds;
var startIndex = allFound[i].texts[0].characters[0].index-1;
var parentStory = allFound[i].insertionPoints[0].parent;
var object = allFound[i].insertionPoints[0].parent.characters.itemByRange(allFound[i].texts[0].characters[0].index-1, allFound[i].texts[0].characters[-1].index+1).place(filePath);
app.documents[0].recompose();
if(parentFrame.overflows){
parentFrame.fit(FitOptions.FRAME_TO_CONTENT);
}
var myObj = parentStory.characters[startIndex].rectangles[0];
try{
if(app.documents[0].objectStyles.item("mypic").isValid){
myObj.appliedObjectStyle = app.documents[0].objectStyles.item("mypic");
}
}
catch(e){}
}
else i++;
app.documents[0].recompose();
allFound = getAll();
}
//======================
function getAll(){
try{
if(app.selection != null){
for(var s = 0; s < app.selection.length; s++){
if(app.selection[s] instanceof TextFrame){
if(app.selection[s].findGrep().length > 0){
return app.selection[s].findGrep();
}
}
else if(app.selection[s] instanceof TextStyleRange || app.selection[s] instanceof Text){
return app.selection[s].findGrep();
}
else return app.documents[0].findGrep();
}
return [];
}
else return app.documents[0].findGrep();
}
catch(e){
return [];
}
}
//======================
Best
Sunil
Copy link to clipboard
Copied
It's already very good, but I don't understand:
There will be only one selection in this box.
If you select only one of the other text boxes, all will be converted.
It should convert only the selected.
Copy link to clipboard
Copied
Try this one :
//======================
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = getAll();
for(var i = 0; i < allFound.length; i){
var allContent = allFound[i].texts[0].contents;
var filePath = File(allContent.toString().replace(/\\/g,'/'));
if(filePath.exists){
var parentFrame = allFound[i].insertionPoints[0].parentTextFrames[0];
var oldGB = parentFrame.geometricBounds;
var startIndex = allFound[i].texts[0].characters[0].index-1;
var parentStory = allFound[i].insertionPoints[0].parent;
var object = allFound[i].insertionPoints[0].parent.characters.itemByRange(allFound[i].texts[0].characters[0].index-1, allFound[i].texts[0].characters[-1].index+1).place(filePath);
app.documents[0].recompose();
if(parentFrame.overflows) parentFrame.fit(FitOptions.FRAME_TO_CONTENT);
var myObj = parentStory.characters[startIndex].rectangles[0];
try{
myObj.appliedObjectStyle = app.documents[0].objectStyles.item("mypic");
}
catch(e){}
}
else i++;
app.documents[0].recompose();
allFound = getAll();
}
//======================
function getAll(){
try{
if(app.selection != null){
for(var s = 0; s < app.selection.length; s++){
if(app.selection[s] instanceof TextFrame){
if(app.selection[s].findGrep().length > 0) return app.selection[s].findGrep();
}
else if(app.selection[s].hasOwnProperty("characters")) return app.selection[s].findGrep();
else return app.documents[0].findGrep();
}
return [];
}
else return app.documents[0].findGrep();
}
catch(e){return [];}
}
//======================
Best
Sunil
Copy link to clipboard
Copied
Sunil Yadav~Best wishes
Yes, that's it.
You are so great
Thank you very much~~