Running a script from an InDesign plug-in

Perhaps you’ve come to native plug-in development from a more script-based background, or perhaps you have some existing script code you want to reuse in a new plug-in project. Whatever your background, it’s really handy to be able to run a script from a native plug-in, and it’s also surprisingly easy.

The code below works out of the box so you can copy and paste as much as you please.

#include "IScriptManager.h"
#include "IScriptEngine.h"
#include "IScriptRunner.h"
#include "IScriptUtils.h"
#include "JavaScriptID.h"

InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
if (!scriptManager)
{
   return;
}

InterfacePtr<IScriptEngine> scriptEngine(scriptManager->QueryDefaultEngine());
if (!scriptEngine)
{
   return;
}

InterfacePtr<IScriptRunner> scriptRunner(scriptEngine, UseDefaultIID());
if (!scriptRunner)
{
   return;
}

RunScriptParams params(scriptRunner);
scriptRunner->RunScript("alert('hello, world!');", params);

The above code uses JavaScript but you may use any script manager you like, including kAppleScriptMgrBoss for AppleScript and kOLEAutomationMgrBoss for Visual Basic. For a complete list of available scripting managers see ‘Script managers’ in chapter 10 ‘Scriptable Plug-in Fundamentals’ of the Programming Guide Volume 1 included in the plug-in SDK (available at http://www.adobe.com/devnet/indesign/sdk.html).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>