Welcome, Guest. Please Login
Rorohiko Workflow Resources
  Welcome the Message Board of Rorohiko Workflow Resources. To register, please e-mail [email protected] - automatic registration has been turned off because of relentless spambots and spammers. This Message Board is not actively monitored. If you have an urgent question, please e-mail [email protected]. If you post it here, it will probably be many weeks before we notice.
  HomeHelpSearchLogin  
 
Page Index Toggle Pages: 1
Send Topic Print
APID: surpassing the 32K limit on attached scripts (Read 3239 times)
Kris Coppieters
YaBB Administrator
*****
Offline



Posts: 181
New Zealand
Gender: male
APID: surpassing the 32K limit on attached scripts
05/05/06 at 12:14:06
 
The Active Page Item Developer palette allows you to edit scripts attached to any page item. However, the little editor only allows editing scripts up to 32K in size - anything above that gets chopped off. In addition, the editor is not really good enough to edit larger scripts.

Instead, it is better to develop your scripts 'outside' of InDesign, and only embed them into a document when compiling the script in order to release it.

I'll explain here one possible setup to create a scripted plugin larger than 32K in size, with the added bonus that you can edit it in a proper text editor.

Suppose you are building a scripted plugin 'MyPlugin'.

Instead of storing the source code of the plug-in inside of a page item, you instead create a page item with the following properties:

Script Label: MyPlugin
List of Subjects: - whatever you need -
EventFilter: - whatever you need -
JavaScript:

theController = theItem.labeledPageItems("MyPluginController")[0];
theController.handleScriptEvent("executeScript");


In addition to the MyPlugin page item you also create a second page item, MyPluginController:

Script Label: MyPluginController
List of Subjects: MyPlugin
EventFilter: selected, executeScript
JavaScript:

if (theItem.eventCode == "executeScript")
{
  var theSourceDoc = app.activeDocument;
  var theSourceFile = File(theSourceDoc.filePath + "/MyPlugin.jsx");
  if (theSourceFile.exists && theSourceFile instanceof File)
  {
    theSourceFile.open("r");
    var theSourceCode = theSourceFile.read();
    theSourceFile.close();
    if (theSourceCode != "")
    {
     app.doScript(theSourceCode);
    }
  }
}
else if (confirm("Are you sure you want to compile the MyPlugin plug-in?",true,"Compiling controller"))
{
  if (theItem.subjects.length != 1)
  {
    var theErrorMessage = "Cannot compile\n";
    theErrorMessage    += "There should be exactly one page item with a label ";
    theErrorMessage    += "matching " + theItem.subjectScriptTagList + " in ";
    theErrorMessage    += "the document.";
    alert(theErrorMessage);
  }
  else
  {
    //
    // Our single subject is the MyPlugin page item
    //
    var thePluginSourceItem = theItem.subjects[0];
   
    //
    // Stash the current script away - we'll replace it with a
    // script that is possibly > 32K, then compile, and then restore
    // this script
    //
    var savedScript = thePluginSourceItem.javaScript;

    var theSourceDoc = app.activeDocument;
    var theSourceFile = File(theSourceDoc.filePath + "/MyPlugin.jsx");
    if (theSourceFile.exists && theSourceFile instanceof File)
    {
     theSourceFile.open("r");
     var theSourceCode = theSourceFile.read();
     theSourceFile.close();
     if (theSourceCode != "")
     {
       thePluginSourceItem.javaScript = theSourceCode;
     }
    }
   
    //
    // This componentID embeds the following attributes:
    //
    // name of plugin:      MyPlugin
    // password:            whatever
    // copyright:           (c) 2006 Me myself and I
    // licensing:           free
    // min. plugin version: 1.0.19
    // end-date:            -1,-1,-1 (no timeout)
    // num actual days:     0 (not set)
    // num demo days:       0 (not set)
    //
    var theComponentName = "MyPlugin;whatever;(c) 2006 Me myself and I;Free";
    var theComponentId = theComponentName + ",1.0.19,-1,-1,-1,0,0";
   
    //
    // "Lock" both controllers. Because this currently executing script
    // still needs to be able to access the other controller we need to
    // lock it too, with the same componentId.
    //
    // If we did not lock theItem too, it would lose access to
    // thePluginSourceItem as soon as we locked that. By setting the
    // componentId in the proper order we make sure theItem can continue
    // to access the Active Page Item Developer data of thePluginSourceItem.
    //
    theItem.componentId = theComponentId;             // Lock compilerController
    thePluginSourceItem.componentId = theComponentId; // Lock autoLinerController

    //
    // Save the locked autoLinerController as a plug-in
    //
    thePluginSourceItem.saveToPlugin("MyPlugin");
   
    //
    // Unlock both controllers. This script is allowed to do that because
    // it is currently locked with the same componentId. This must be done
    // in the reverse order of locking.
    //
    thePluginSourceItem.componentId = "";
    theItem.componentId = "";

    //
    // Restore the original 'delegator' script
    //
    thePluginSourceItem.javaScript = savedScript;

    alert("Done");
  }
}


In this setup, the MyPlugin page item is nothing but a proxy that delegates all work to the MyPluginController by sending a custom executeScript event (executeScript is not part of the standard APID event codes).

The controller then executes the request by reading the MyPlugin.jsx file off disk.

If the controller is selected (to initiate the compilation of the scripted plug-in) it also reads MyPlugin.jsx off the disk, temporarily attaches it to the MyPlugin page item and compiles it.

The MyPlugin.jsx script will need to be made such that it can accept either 'direct' event codes or 'delegated' event codes - i.e. if it is callled in compiled form, you use theItem.eventCode. If it is used during development in uncompiled form, you use theItem.eventSource.eventCode instead - beacuse we're one extra step removed from the originator of the event.
Back to top
 

Kris
WWW  
IP Logged
 
Page Index Toggle Pages: 1
Send Topic Print