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

[Extendscript] Trying to insert text, how do I find the Doc that a particular object is in?

Community Beginner ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

I have a cell object and I would like to add text to it. I'm trying to call `doc.AddText(textLoc, 'cell text');`, but I don't have the `doc` object, I only have the `cell` object.

 

function setCellText(cell, text) {
    // Create a text location at the beginning of the cell
    var textLoc = new TextLoc();
    textLoc.obj = cell;
    textLoc.offset = 0;

    // Delete all the paragraphs after the first (new) paragraph
    while (cell.FirstPgf.id != cell.LastPgf.id) {
        cell.LastPgf.Delete();
    }
    cell.LastPgf.Delete();

    // Write the specified cell value to the text location
    doc.AddText(textLoc, text);    // <== !! How do I get the doc for the current cell?
}


Rather than adding a `doc` parameter to every function in the call statck, I'd like to be able to determine the `doc` from the cell itself. I have some code that will look up the page an object is on, but I couldn't figure out how to get to the parent doc anywhere along the way.


At a higher level, is there an alternate/better way to add text to a cell than `doc.AddText`? I suspect not, but just in case.

 

For reference, here is the function to get the Page:

fucntion getDoc(obj) {
    // ???
}

function getPage(obj) {

    if (obj.PageFramePage != undefined && obj.PageFramePage.ObjectValid())
        return obj.PageFramePage;

    if (obj.FrameParent != undefined && obj.FrameParent.ObjectValid())
        return obj.FrameParent.getPage();

    if (obj.InTextFrame != undefined && obj.InTextFrame.ObjectValid())
        return obj.InTextFrame.getPage();

    if (obj.TextLoc != undefined && obj.TextLoc != null)
        return obj.TextLoc.obj.getPage();

    return null;
}

 

 Cross posted on SO: https://stackoverflow.com/q/68230503/892536

TOPICS
Scripting

Views

443

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
Enthusiast ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

I wonder how you found the cell? 😉

If you have an active document, you get the doc object with "var goDoc = app.ActiveDoc";

I can recommend you a wonderful blog by Debra Herman:

 

http://extendingframemaker.blogspot.com/2011/12/making-selection-in-table.html

http://extendingframemaker.blogspot.com/2011/12/getting-table-cells-column-by-column.html

 

In this blog are many excellent examples of scripts.

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 ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

Thanks for the reply and the blog link - I'll check it out.

 

This is happening in a sub-function. Them main function takes a table and does some reporting. Occassionally I need to write to the table, but that happens a couple of function calls into the main function. Mostly for the sake of clenliness, I would like to be able to insert text into a given cell without having to pull along the doc object though functions that, for the majority of the time, don't need it.

 

I don't think ActiveDoc would work because this code is going though a book file and looking at each file. The files are opened hidden - I'd have to test it out, but I don't think that ActiveDoc is always going to be the doc that I'm working in.

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 ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

There may be a way to navigate up to the document object from the cell object; FrameScript used to have ways to do this. However, it is probably more efficient to pass the doc object along from the top. This is basically how I do it:

main ();

function main () {

    var doc;

    doc = app.ActiveDoc;
    if (doc.ObjectValid () === 1) {
        processDoc (doc);
    }
}

function processDoc (doc) {

    // Pass the doc object to other functions.
}

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 ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

Yes, that's what I'm trying to accomplish - somehow being able to navigate up to the document object from any element inside of it. However, I'm not familiar enough with FrameMaker's object model to figure this out myself (I tried). I was previously able to find some code that could do that for the Page an object is on, but I'd like to be able to do that for the Doc as well.

 

Generically, this kind of function would be very useful in lots of scripts, just like the getPage() function - I've been in the middle of a script and needed the page. Rather than go rework the script, it's very helpful to be able to just get the page right there and continue coding.

 

I think you're right about not being very efficient. I probably wouldn't want to populate an entire table looking up the doc object for each cell. I'm thinking I need to updat the function to take a doc object (and encourage the calling function to pass it in), but if it is not passed in I could have this function look up the doc and it would still work.

 

I suppose, worst case, I could try to look through all the open documents and try to get the object by unique id, then check to make sure it matched the cell.

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 ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

LATEST

After I posted, I looked through the ExtendScript object model and the FDK documentation and couldn't find a way to navigate up to the document object. Common convention, though, is to pass the document object along to each function because there are ExtendScript methods that require a document object.

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