Monday, November 28, 2011

Determining an Object's Type

If you find an object by navigating a list such that the lists of paragraphs, markers, pages, and the like, you know in advance what object type you will find. But what if you get an object because it represents, for example, the user selection? In such a case, there is uncertainty as to what you have found but to do anything with that found object, you need to definitively determine its object's type.

The FDK provides the function F_ApiGetObjectType() which returns the FO_ type of an object but there is no analog in ExtendScript. Your best option is to use the object’s constructor property name. (Thanks to Ian Proudfoot for this tip.)


The following script updates the find selection type script to determine the object type of the currently selected object, if any.

function GetSelectedObjectType(doc) {
    var tRange, obj, type;
 
    tRange = doc.TextSelection;
    obj = tRange.beg.obj;
    if (!obj.ObjectValid()) {
        obj = doc.SelectedTbl;
        if (!obj.ObjectValid()) {
            obj = doc.FirstSelectedGraphicInDoc;
        }
    }

    if (obj.ObjectValid()) {
        type = obj.constructor.name;
    }
    else {
        type = "None";
    }

    return type;
}

var doc = app.ActiveDoc;
Alert(GetSelectedObjectType(doc), Constants.FF_ALERT_CONTINUE_NOTE);






3 comments:

  1. I was just pawing through Adobe's ChangeStyle script and I discovered in ChangeStyleUtils.jsx the statement:

    var parentType = parentFrame.type;

    I can't find the type (lowercase) property documented anywhere in the ExtendScript docs, but it seems to do the trick.

    ReplyDelete
    Replies
    1. Revisiting this topic with the folder.getFiles() method, which returns an array of File and Folder objects, the only way there to distinguish which object is which is with ".constructor.name". Oh well.

      Delete