Copy link to clipboard
Copied
How can i find and replace automaticaly an anchored image placed in a text, by a character ?
Thanks
Copy link to clipboard
Copied
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '~a'; //finding image
app.changeGrepPreferences.changeTo = 'image1'; //replacing with text
app.activeDocument.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
Vandy
Copy link to clipboard
Copied
Thanks Vandy for your reply,
but your script replace all anchored images, even if they are different images,
i would like to change each image by a different character,
and the find and replace ou grep search (รข or ~a) find all anchored images,
i can't find the one i want...
JCL
Copy link to clipboard
Copied
Only you know which one you want. How do you intend to communicate that to the script?
Or perhaps I should ask: which character goes with which image? You can use a search to find them all and then cycle through them with a for-loop to replace each individually.
How much help do you need? Have you written scripts yourself or are you looking for someone to write one for you?
Dave
Copy link to clipboard
Copied
No, I don't have written the script
I just don't know how to find a particular image
If i select and copy a text with a anchored image inside, when i paster it the image disappear.
I found that ^a or ~a is the code for anchored image, so if i search ^a it finds all anchored image
but it doesn't make difference between 2 images.
Do you i any idea how to find a specific anchored image ?
an ythen i think i can make the script
JCL
Copy link to clipboard
Copied
You should look for all of them and then cycle through them with a for loop. Here's one way of tackling it that works because anchored pageItems are returned as the pageItems collection of text, so assuming that the anchors you care about are within the selection, this will work:
//DESCRIPTION: Replace anchored items in selected text with characters
(function(){
if (app.documents.length > 0
&& app.selection.length == 1
&& app.selection[0].hasOwnProperty("baseline")) {
replaceAnchors(app.selection[0]);
}
function replaceAnchors(someText) {
var pageItems = someText.pageItems;
for (var j = pageItems.length - 1; j >= 0; j--) {
var myChar = pageItems
.parent; myChar.contents = String.fromCharCode(65 + j);
}
}
}())
Dave
Copy link to clipboard
Copied
I have tried your script but it doesn't work
My anchored image name is "picto+.eps" and i would like to replace it by "ยง"
Do i have to change any think in your script to make it work ?
Thanks
JCL
Copy link to clipboard
Copied
The script most certainly does work. It just doesn't do what you wanted it to because until now you haven't said what you wanted.
And even now I'm thoroughly confused because you said earlier that you had a number of images you wanted to replace, but now you have just one.
Where is it? Do you know exactly where in the document? Or do you have to search for it?
Does the image appear just once? Or is it in the document many times?
Is it linked? Because if so, that's the best way to find it, via the document's links collection.
Dave
Copy link to clipboard
Copied
Yes, I have a 600 pages document, with hundreds of anchored images.
The person who made this file anchored images to illustrate the text.
For exemple : "Grand Hotel de Paris (image of a swiming pool), Hotel de Londres (image of a parking),..."
Because i need to work on this file, i would like to replace these anchored images by a type character,
with a type i made with those images.
and the texte would look like : "Grand Hotel de Paris #, Hotel de Londres ยง,..."
# is an image of swimmingpool, and ยง is an image of a parking
The images are linked actualy, and each image appear dozen of time in the file.
JCL
Copy link to clipboard
Copied
This script does the job. You'll need to update the anchArray to fit your actual needs. You'll see that I added a test case so I could test the script.
//DESCRIPTION: Replace Anchored Images with Text
(function(){
if (app.documents.length > 0) {
var anchArray = [
// edit names; add new pairs as needed
{'name' : "parking.eps", 'char' : "ยง"},
{'name' : "swimmingpool.eps", 'char' : "#"},
{'name' : "o-fc1_setup.pdf", 'char' : "โ"} // my test case
]
replaceAnchors(app.documents[0], anchArray);
}
function replaceAnchors(aDoc, anchArray) {
var links = aDoc.links.everyItem().getElements();
for (var j = links.length - 1; j >= 0; j--) {
var anchor = getAnchor(links
); if (anchor == null) continue; // link not anchored
var aChar = getCharacter(links
.name, anchArray); if (aChar != null) {
anchor.contents = aChar;
}
}
}
function getCharacter(linkName, anchArray) {
for (var j = anchArray.length - 1; j >= 0; j--) {
if (linkName == anchArray
.name) { return anchArray
['char']; }
}
return null;
}
function getAnchor(link) {
var theChar = link.parent.parent.parent;
if (theChar instanceof Character) {
return theChar;
}
return null; // not anchored
}
}())
Dave
Copy link to clipboard
Copied
Great,
That works perfectly, and changes all my anchored images to characters.
Last request, is it possible to give this character a new font and size to look like exactly as the anchored image.
Thanks
JCL
Copy link to clipboard
Copied
Yes.
Which font? What size? Or do you want the script to get the size of the anchored frame and match that automatically?
The mechanics of this would be to set the font and size in the replace Anchors function immediately after setting the contents to the designated character.
Dave
Copy link to clipboard
Copied
No,
The font is unique "KYBpictoNORMAL", and the size is "28pt"
and the space bitween two line (or line spacing) is 24pt
That's all
JCL
Copy link to clipboard
Copied
While this could indeed be done by the script through local formatting (although I wouldn't be able to test it because I don't have the font), you might consider setting up a character style to apply the required parameters, then the script could apply that or you could even apply it using a GREP style (assuming you have no other instances of the characters in question).
Dave
Copy link to clipboard
Copied
Thanks anyway,
If you can put these character specifications in the script
using "Zapf Dingbats" or a style name like "StyleKYB",
it would be perfect,
otherwise i will search and replace the character using GREP.
Thanks for all your help
JCL
Copy link to clipboard
Copied
Here you go. You'll have to create the character style in your document, and I've assumed that it is not inside a CharacterStyleGroup.
//DESCRIPTION: Replace Anchored Images with Styled Text
(function(){
if (app.documents.length > 0) {
var anchArray = [
// edit names; add new pairs as needed
{'name' : "parking.eps", 'char' : "ยง"},
{'name' : "swimmingpool.eps", 'char' : "#"},
{'name' : "o-fc1_setup.pdf", 'char' : "โ"} // my test case
]
replaceAnchors(app.documents[0], anchArray);
}
function replaceAnchors(aDoc, anchArray) {
var links = aDoc.links.everyItem().getElements();
for (var j = links.length - 1; j >= 0; j--) {
var anchor = getAnchor(links
); if (anchor == null) continue; // link not anchored
var aChar = getCharacter(links
.name, anchArray); if (aChar != null) {
anchor.contents = aChar;
anchor.appliedCharacterStyle = "StyleKYB"
}
}
}
function getCharacter(linkName, anchArray) {
for (var j = anchArray.length - 1; j >= 0; j--) {
if (linkName == anchArray
.name) { return anchArray
['char']; }
}
return null;
}
function getAnchor(link) {
var theChar = link.parent.parent.parent;
if (theChar instanceof Character) {
return theChar;
}
return null; // not anchored
}
}())
I'd appreciate you indicating that I have correctly answered your question.
Thanks,
Dave
Copy link to clipboard
Copied
I have a script error on line : "anchor.appliedCharacterStyle = "StyleKYB".
unexpected value...
Can't we put directly the fonts name and size in the script instead of the style ?
JCL
Copy link to clipboard
Copied
Did you make a character style with that name? That is far and away the easiest and most reliable way to achieve the aim and it also has the benefit of being the most flexible.
Dave
Copy link to clipboard
Copied
Yes, I did.
But it doesn't work.
JCL
Copy link to clipboard
Copied
Could you select a paragraph that includes a character to which you have applied this character style you created, export that to tagged text and then post that tagged text here?
Thanks,
Dave
Copy link to clipboard
Copied
Thanks Dave Dave,
You realy helped me a lot.
JCL