跳转到内容

与Flash应用程序通信

与Flash应用程序通信

ScriptUI支持Flash Player,它可以在Adobe应用程序的窗口中运行Flash应用程序。Flash应用程序运行ActionScript,这是与Adobe应用程序运行的ExtendScript版本的JavaScript不同的JavaScript实现。

要打开Flash Player,请在您的ScriptUI窗口中添加一个类型为FlashPlayer的控件。这种类型的控件对象包含允许您的脚本加载SWF文件并控制电影播放的功能。它还包含允许您的Adobe应用程序脚本与Flash应用程序的ActionScript环境通信的功能。请参阅FlashPlayer控件函数

可以在两个脚本环境之间传递有限的数据类型:

  • 数字
  • 字符串
  • 布尔值
  • null
  • undefined
  • 对象
  • 数组

ActionScript的classdate对象不支持作为参数值。

在Flash应用程序的ActionScript脚本中,您必须通过提供对External API的访问来准备双向通信。为此,请将ExternalInterface类导入到您的Flash应用程序中:

import flash.external.ExternalInterface;

从ActionScript调用ExtendScript函数

ActionScript的ExternalInterface类允许您调用在Adobe应用程序脚本中定义的FlashPlayer元素中的ExtendScript函数,并在ActionScript环境中运行它。您必须在FlashPlayer元素中定义一个具有匹配函数名称的方法。

例如,为了让SWF代码调用名为myExtendScriptFunction的ExtendScript函数,请将名为myExtendScriptFunction的函数定义为FlashPlayer控件对象的方法。函数名称没有特殊要求,但函数必须仅接受和返回支持类型的数据。

您不需要在ActionScript环境中注册ExtendScript函数。您的ActionScript脚本可以直接使用ExternalInterface.call()方法调用外部函数:

var res = ExternalInterface.call( "myJavaScriptFunction" );

当Flash Player执行ExternalInterface调用时,ScriptUI会查找与FlashPlayer元素方法同名的函数,并使用指定的参数调用它。在函数的上下文中,JavaScript的this对象指的是FlashPlayer对象。


从ScriptUI脚本调用ActionScript函数

在ExtendScript端,使用FlashPlayer方法invokePlayerFunction()来调用在Flash应用程序中定义的ActionScript方法:

var result = flashElement.invokePlayerFunction( "ActionScript_function_name", [ arg1, ..., argN ] );

您可以使用可选参数将数据(支持的类型)传递给ActionScript方法。

在从Adobe应用程序脚本调用任何ActionScript函数之前,您的Flash应用程序必须将该函数注册到ExternalInterface对象,作为Flash容器的回调。要注册函数,请使用ExternalInterface.addCallback()方法:

public static addCallback(methodName:String, instance:Object, method:Function);

这将注册一个在Adobe应用程序脚本中定义的名为getActionScriptArray()的函数:

ExternalInterface.addCallback( "getActionScriptArray", this, getActionScriptArray );

Flash示例

Adobe ExtendScript SDK中的这些示例演示了如何使用Flash Player:

示例描述
UsingFlashPlayer.jsx展示如何创建Flash® Player,并使用它加载并播放SWF文件中定义的电影。
ActionScriptDemo.jsx展示如何在Adobe应用程序脚本环境和Flash Player的ActionScript™脚本环境之间进行通信。