I have a very basic question but I couldn't find an adequate answer for me yet.
I just started trying to write a jsx script to select the first graphic in the document which lies within an anchored frame. I want to get the File name of this graphic and replace the whole graphic+frame with just the file name of the referenced graphic.
That's my plan so far... But I don't really understand how to even select the graphic.
I read some documents and found stuff like "NextGraphicInDoc" etc.
So I tried:
var doc = app.ActiveDoc;
var graphic = doc.NextGraphicInDoc;
I tried several other things like:
But I always get undefined as output. So I clearly don't understand the DOM on how to approach the elements of the doument properly and use them.
Could someone maybe enlighten me? I think once I get the gist of how to handle this properly I can get further myself but I am stuck at the beginning.
Thanks in advance. I also appreciate good beginner friendly sources if available. The 1000+ Page from Adobe for FM Scripts don't really help that much 😕
I am really not an expert in ExtendScript. What I notice: doc.NextGraphicInDoc does not refer to any graphic. Therefore you have to select a graphic first.
Resources which I used are many comments here in this forum! When you have issues, just post your questions here. The more context you give, the better.
And this web-site explained a lot:
Start with the oldest blog post.
You are correct to note that there is a lack of beginner-friendly documentation resources. I believe that is due to the fact that there are very few beginners. The vast majority of the FM user base does not take advantage of scripting, even though it is one of the most important features. So, it's a very small ecosystem of developers who got started through trial and error, and occasional help from others with more experience.
Unfortunately, you are not correct to say that your question is "basic." 🙂 You might think it should be basic, but as soon as you remove the UI and the ability to point and click, things get more complicated. So, you need some way to navigate a document without eyes and a mouse. You are correct that you can get the active document with:
var doc = app.ActiveDoc;
...but after that, it gets tricky. You can step through all anchored frames in the document with something like this:
var count = 0;
var graphic = doc.FirstGraphicInDoc;
if(graphic.constructor.name == "AFrame") count++;
graphic = graphic.NextGraphicInDoc;
alert(count + " frame(s) found.");
At each frame, you could get the first graphic object in the frame with:
So, here is the first trick... that from an API perspective, an anchored frame and the graphics within it are all considered graphic objects. You can cycle through a doc to find all AFrames graphic objects, then you can cycle through all graphics in the frame. When you finally get the graphic you want, you can make change it to a referenced graphic with:
graphic.InsetFile = "C:\\some\\path\\to\\file";
But then comes the next trick. How do you know which anchored frame or graphic you are at? I can't really answer that question. If you had an existing InsetFile path that you could key off of, that would work.
There are other ways to find anchored frames and graphics:
- Manually select the graphic, then get the object with doc.FirstSelectedGraphicInDoc.
- Go through the paragraphs in the flow and get the text items with GetText() and the flag Constants.FTI_FrameAnchor. This is quite complicated, although it might be what you need to do.
So, I hope this helps a little. It's too complicated to teach you everything with just a post here. You may just need to prepare for some extra time to get familiar with how it all works. I have some samples here, some dealing with graphics... perhaps they might help too:
Best of luck,
I would encourage you to download the free FrameMaker FDK (Frame Developers Kit). You can use the documentation to get familiar with the FrameMaker object model. While the examples are in the C language, the basic model of the properties and functions (methods) and how they relate to each other are the same in ExtendScript. The language is different, but I used the FDK documentation as a "theory guide" to get me up to speed with scripting in the late 1990s. I still consult it from time to time in my scripting work.