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

Script for replacing placeholder/URL with inline image

New Here ,
Feb 20, 2020 Feb 20, 2020

Copy link to clipboard

Copied

Hi, I am looking for a script, that automatically replaces a placeholder paragraph with the appropriate inline image in the same postion. The image should be identified by the paragraph text (could be URL, ID, Name, etc.) I would use a paragraph style that only serves that purpose. There are multiple positions in the body copy that would need this replacement. All images will have the same width but potentially different heights. All instances are in one linked textframe spread across multiple pages. Using a placeholder/url relative to the path of the .indd file would be my preferred way to use this.

 

1. Scan selected textframe for first instance (solved)

2. Disect the URL from selected text (solved)

3. Delete selected text and place inline image from created URL instead (solved)

4. Scale image to set width

5. Repeat for all instances (solved)

 

Thanks for any help.

 

EDIT:

I did come across a solution for a part of my problem now. I don't know why I couldn't find it before. 

credits to "Script : Replacing text by image with GREP" by a_r54776508
I'm still missing the solution to scaling the image right after (or when) placing it.
 
Current code:

 

main();

function main()
{
	var doc = app.activeDocument;	

	if(app.documents.length > 0)
		{
			app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
			replaceImage("^.*\.png");
		}
	else { alert("No document opened!"); }
}

function replaceImage(query)
{
	// Clean the pre-existing GREP and add our query
	app.findGrepPreferences.findWhat = query;	
	var result = app.findGrep();
        var folder = 'C:/*****/images/';
	// 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();
	}
}

 

 

TOPICS
Scripting

Views

845

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 ,
Feb 20, 2020 Feb 20, 2020

Copy link to clipboard

Copied

Set a variable at your .place function: var placedImg = results[i].place(imagePath);

 

Then, you can access the image and use the resize method with: placedImg[0].resize(args);

 

For more on resize: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Image.html#d1e236446__d1e239706

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
Engaged ,
Feb 21, 2020 Feb 21, 2020

Copy link to clipboard

Copied

LATEST

This script is placing also inline images.

But the placeholder needs to be marked with a hashtag (e.g ##imageName.pd##)

 

http://www.indesignscript.de/skript-detail/detail/News/snippetplacer/

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