Copy link to clipboard
Copied
Hello eveyone!
I'm trying to write a script to help me to replace some text (wich are images names, like "logo.png" or "example1.png") by the matching image. I've looked in multiple scripts and forums to try to achieve it, but the closest I found were too specific (like replacing a word with a matching icon, etc.) and didn't answers my needs.
However, I'm not familiar with scripting and I did'nt go really far before needing to ask for help ^^
The context
I have to import markdown files to InDesign. I clean it, and the images are "isolated" in one line. Because they all are in the same folder, I keept just the name and the extension. It can look like this basically:
How I do so far
So, in the end, I do a Ctrl+F and, for 200 to 300 times by document, I do :
This is humanly manageable, not too much time consuming (you can deal with 2 images/second like that), but the risk of mistake and just the dull redundancy make me want to try a script for that.
The script : what I've got so far
Not too much. I kind of made working the GREP but I do not know how to handle the result:
And there is my current script:
main();
function main()
{
var doc = app.activeDocument;
if(app.documents.length > 0)
{
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
replaceImage("^.*\.png$");
}
else { alert("Aucun document ouvert."); }
}
// IMAGE REPLACEMENT
function replaceImage(query)
{
// Clean the pre-existing GREP and add our query
app.findGrepPreferences.findWhat = query;
var result = app.findGrep();
// Look for each result of the GREP query and replace it by the image.
for (var i=0; i<result.length;i++)
{
var folder = "C:/Users/myUsername/Desktop/test InDesign/";
var imageName = result[i];
var imagePath = folder+imageName;
app.place(imagePath);
// For Test only : Currently write "C:/Users/myUsername/Desktop/test InDesign/images.[object Text]". I need "C:/Users/myUsername/Desktop/test InDesign/images.myImageName.png"
// alert(imagePath);
}
}
And voilà . Thank you in advance for your help and for your time! 🙂
EDIT : Code simplied, comments and variables translated in English. Tested and still giving the same result.
First of all, you need to process the found names back to front because you're inserting images and removing text. By processing back to front you the indexes of the unprocessed items aren't disturbed.
result[i] in your code is a text object, and to get the image name you should get the object's content. Here's a working version of your function:
function replaceImage(query)
{
// Clean the pre-existing GREP and add our query
app.findGrepPreferences.findWhat = query;
var result = app.findGrep
...
Copy link to clipboard
Copied
First of all, you need to process the found names back to front because you're inserting images and removing text. By processing back to front you the indexes of the unprocessed items aren't disturbed.
result[i] in your code is a text object, and to get the image name you should get the object's content. Here's a working version of your function:
function replaceImage(query)
{
// Clean the pre-existing GREP and add our query
app.findGrepPreferences.findWhat = query;
var result = app.findGrep();
var folder = "C:/Users/myUsername/Desktop/test InDesign/";
// Look for each result of the GREP query and replace it by the image.
for (var i = result.length-1; i > 0; i--)
{
var imageName = result[i].contents;
var imagePath = folder+imageName;
// Place the image in the found text
result[i].insertionPoints[0].place(imagePath);
// Delete the text
result[i].remove();
}
}
Peter
Copy link to clipboard
Copied
Hi Peter,
I've tested and studied your code: I understand now what I got wrong and everything works fine (even better and faster than I expected!).
Thank you a lot four your help!
Copy link to clipboard
Copied
I'm going for that bonus.
1. ask for a folder:
var folder = Folder.selectDialog("Select images folder");
if (!folder) /* someone pressed "Cancel" */
exit();
folder = folder.fullName;
2. use the folder of the current document:
folder = app.activeDocument.filePath+'/images/';
3. Let's have fun and do both:
var folder = Folder(app.activeDocument.filePath+'/images').selectDlg("Select image folder");
if (!folder) /* someone pressed "Cancel" */
exit();
folder = folder.fullName;
... With this, you will find that the default selected folder is the "images" subfolder inside your current document folder and so you can press Enter right away, but you can also browse for another one to use.
Copy link to clipboard
Copied
Thanks Jongware!
I've almost forgot about that bonus, I managed to do it with basic a prompt. Your solution is however way more elegant and safe (and fun to learn and play with)!
I've added it to my script, wich now works perfectly fine and is even better that I could have hoped.
Thanks to both of you! 🙂
EDIT : I do not find how to mark the answer as correct or the topic as solved. I saw that the topic has to be marked as a question for allowing that, but do not find how either. Sorry for the inconvenience 😕
Copy link to clipboard
Copied
Hi guys, thanks for you post. I have a problem, the script replace everything except the first image, just that.