Skip to main content
Legend
November 3, 2023
Answered

How to use getDocumentInfoAsText()

  • November 3, 2023
  • 1 reply
  • 1482 views

While looking at Document properties, I discovered that there is a method called getDocumentInfoAsText. The name might suggest that it is a function that allows us to write out information from the Document Info panel as text. If this is available, it would be very easy to get a list of fonts used in a document from a script.

 

After some trial and error, I found that the first argument is an array and the second argument is a file. If the array is empty, then an empty text file will be created at the specified destination.

 

I assume that the first array is probably keys or something for the information to be written out, but what should I specify to succeed? I couldn't find that information in omv.xml or in the AppleScript dictionary.

 

app.documents[0].getDocumentInfoAsText([], new File('~/Desktop/document_info.txt')) ;

 

This topic has been closed for replies.
Correct answer m1b

Hi @sttk3, I worked out the parameters I think. Have a look at this:

/**
 * Exploration of Document.getDocumentInfoAsText.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/how-to-use-getdocumentinfoastext/m-p/14206023/thread-id/386366
 */
(function () {

    /** Whether to return info on the document's selection. */
    var SELECTED_ITEMS_ONLY = false;

    /**
     * Info types for Document.
     * @enum {Number}
     */
    var DocumentInfoType = {
        DOCUMENT: 2,
        OBJECTS: 3,
        GRAPHIC_STYLES: 4,
        BRUSHES: 5,
        SPOT_COLOR_OBJECTS: 6,
        PATTERN_OBJECTS: 7,
        GRADIENT_OBJECTS: 8,
        FONTS: 9,
        LINKED_IMAGES: 10,
        EMBEDDED_IMAGES: 11,
        FONT_DETAILS: 12,
        REPEAT_OBJECTS: 13,
    };

    // example:
    var outputFile; // outputFile = File('~/Desktop/document_info.txt');
    var result = getDocumentInfoAsText(app.activeDocument, [DocumentInfoType.FONTS, DocumentInfoType.GRAPHIC_STYLES], SELECTED_ITEMS_ONLY, outputFile);

    if (result)
        alert('Result:\n' + result);

})();


/**
 * Wrapper for `Document.getDocumentInfoAsText`
 * method just for experimenting.
 * Returns the contents of the output file.
 * @param {Documennt} [doc] - an Illustrator Document (default: active document).
 * @param {Array<DocumentInfoType>} [infoTypes] - array of DocumentInfoTypes (default: all types).
 * @param {Boolean} [selectionOnly] - whether to report on just the document's selection (default: false).
 * @param {File} [outputFile] - the file to write result to (default: temporary file, deleted after use).
 * @returns {String}
 */
function getDocumentInfoAsText(doc, infoTypes, selectionOnly, outputFile) {

    // for convenience:
    var ALL_DOCUMENT_INFO_TYPES = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

    var doc = doc || app.activeDocument,
        infoTypes = infoTypes || ALL_DOCUMENT_INFO_TYPES,
        selectionOnly = selectionOnly === true,
        f = outputFile || File(Folder.temp.fsName + '/getDocumentInfoAsText.txt'),
        success;

    if (!f.parent.exists)
        f.parent.create();

    try {
        success = doc.getDocumentInfoAsText(infoTypes, f, selectionOnly);
    } catch (error) {
        alert(error);
    }

    if (!success)
        return;

    f.open('r');
    var result = f.read();

    if (outputFile == undefined)
        f.remove();

    return result;

};

 - Mark

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
November 4, 2023

Hi @sttk3, I worked out the parameters I think. Have a look at this:

/**
 * Exploration of Document.getDocumentInfoAsText.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/how-to-use-getdocumentinfoastext/m-p/14206023/thread-id/386366
 */
(function () {

    /** Whether to return info on the document's selection. */
    var SELECTED_ITEMS_ONLY = false;

    /**
     * Info types for Document.
     * @enum {Number}
     */
    var DocumentInfoType = {
        DOCUMENT: 2,
        OBJECTS: 3,
        GRAPHIC_STYLES: 4,
        BRUSHES: 5,
        SPOT_COLOR_OBJECTS: 6,
        PATTERN_OBJECTS: 7,
        GRADIENT_OBJECTS: 8,
        FONTS: 9,
        LINKED_IMAGES: 10,
        EMBEDDED_IMAGES: 11,
        FONT_DETAILS: 12,
        REPEAT_OBJECTS: 13,
    };

    // example:
    var outputFile; // outputFile = File('~/Desktop/document_info.txt');
    var result = getDocumentInfoAsText(app.activeDocument, [DocumentInfoType.FONTS, DocumentInfoType.GRAPHIC_STYLES], SELECTED_ITEMS_ONLY, outputFile);

    if (result)
        alert('Result:\n' + result);

})();


/**
 * Wrapper for `Document.getDocumentInfoAsText`
 * method just for experimenting.
 * Returns the contents of the output file.
 * @param {Documennt} [doc] - an Illustrator Document (default: active document).
 * @param {Array<DocumentInfoType>} [infoTypes] - array of DocumentInfoTypes (default: all types).
 * @param {Boolean} [selectionOnly] - whether to report on just the document's selection (default: false).
 * @param {File} [outputFile] - the file to write result to (default: temporary file, deleted after use).
 * @returns {String}
 */
function getDocumentInfoAsText(doc, infoTypes, selectionOnly, outputFile) {

    // for convenience:
    var ALL_DOCUMENT_INFO_TYPES = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

    var doc = doc || app.activeDocument,
        infoTypes = infoTypes || ALL_DOCUMENT_INFO_TYPES,
        selectionOnly = selectionOnly === true,
        f = outputFile || File(Folder.temp.fsName + '/getDocumentInfoAsText.txt'),
        success;

    if (!f.parent.exists)
        f.parent.create();

    try {
        success = doc.getDocumentInfoAsText(infoTypes, f, selectionOnly);
    } catch (error) {
        alert(error);
    }

    if (!success)
        return;

    f.open('r');
    var result = f.read();

    if (outputFile == undefined)
        f.remove();

    return result;

};

 - Mark

sttk3Author
Legend
November 4, 2023

@m1b Great job! Works fine.

jduncan
Community Expert
Community Expert
November 4, 2023

Nice work @sttk3 @and @m1b. I've resorted to some hacky tricks to get this information before, so I'm excited to look at this closer. Thanks!