ACF Library

ACFJsRunExample

Back to List

Description: Example of Indesignpackage that calls Indesign Scripts - Both async scripts and regular scripts.

Category: INDESIGN

Function source:

package ACFJsRunExample "Example of calling an InDesign UXP function from ACF";

// Menu setup - automatically called when function loads. 
function ACFJsRunExample_menu ()
    return JSON(
      "title", "ACF JavaScript Example",
      "items", JSONarray(
        JSON(
          "function", "ACFJsRunExample_test",
          "title", "Run Async JavaScript function"
        ),
        JSON(
          "function", "ACFJsRunExample_testSync",
          "title", "Run Sync JavaScript function"
        )
      )
    );
end

// Called when menu items is selected from above def. 
function ACFJsRunExample_test ()
    JSON result = jsRun(
      "ACF_jsRun_library.idjs",
      "ACFJsRunDemo",
      "Hello from ACF",
      3,
      JSON("source", "ACF jsRun")
    );

    return JSON(
      "type", "indesign.message",
      "version", 1,
      "severity", "information",
      "message", string(result)
    );
end



function ACFJsRunExample_testSync ()
    JSON result = jsRun(
      "ACF_jsRun_library.idjs",
      "ACFJsRunDemoSync",
      "Hello from ACF",
      3,
      JSON("source", "ACF jsRun")
    );

    return JSON(
      "type", "indesign.message",
      "version", 1,
      "severity", "information",
      "message", string(result)
    );
end

Calling Indesign Scripts from ACF functions Two different scripts, one async and one regular.

Here is the scripts, simple returns the number of open documents, and relaying back a message sent in the parameter from ACF.

ACF_jsRun_library.idjs

/*
  Copy this file to InDesign's User Scripts Panel folder before running
  ACF_jsRun_example.acf with a relative filename.
*/

// Async script
globalThis.ACFJsRunDemo = async function (message, count, options) {
  const { app } = require("indesign");

  await new Promise((resolve) => setTimeout(resolve, 100));

  return {
    success: true,
    message,
    count,
    source: options && options.source ? options.source : "",
    openDocuments: app.documents.length
  };
};

// Sync script
globalThis.ACFJsRunDemoSync = function (message, count, options) {
  const { app } = require("indesign");

  return {
    success: true,
    message,
    count,
    source: options && options.source ? options.source : "",
    openDocuments: app.documents.length
  };
};
Back to List