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

Script : Replacing text by image with GREP

Community Beginner ,
Oct 21, 2019 Oct 21, 2019

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:


indesign_replaceImage1.png

 

 

How I do so far

  • I use a GREP (^.*\.png$) and look for the next result
  • Once selected, I copy it
  • I open the import window
  • I paste the name
  • I look for the next image name


So, in the end, I do a Ctrl+F and, for 200 to 300 times by document, I do :

  • Enter
  • Ctrl+C
  • Ctrl+D
  • Ctrl+V
  • Enter

 

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:

  • I end up with the the filename replaced by [object Text] and do not know how to really reach it
  • It's more of a bonus, but when launching the script, is there a way to have some kind Textfield prompt allowing me to write the folderpath? Or to indicate the folder relatively to the .indd file ? (it's alway a folder /images in the same folder).

 

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.

TOPICS
Import and export , Scripting

Views

2.4K

Translate

Translate

Report

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 22, 2019 Oct 22, 2019

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
...

Votes

Translate

Translate
Community Expert ,
Oct 22, 2019 Oct 22, 2019

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

Votes

Translate

Translate

Report

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 Beginner ,
Oct 22, 2019 Oct 22, 2019

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!

Votes

Translate

Translate

Report

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 22, 2019 Oct 22, 2019

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.

Votes

Translate

Translate

Report

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 Beginner ,
Oct 22, 2019 Oct 22, 2019

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 😕

Votes

Translate

Translate

Report

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
Explorer ,
Mar 28, 2020 Mar 28, 2020

Copy link to clipboard

Copied

LATEST

Hi guys, thanks for you post. I have a problem, the script replace everything except the first image, just that.

 

 

Carpe diem

Votes

Translate

Translate

Report

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