有没有办法获取放在 Mac 和 Windows 上的应用程序图标上的文件?
Is there a way to get a file that's dropped onto the application icon on Mac and Windows?
在 Mac 上,可以将文件拖放到应用程序停靠栏中的应用程序图标上。是否有可能获得该信息,是否有可能在 Windows 上做同样的事情?
我一直在阅读 InvokeEvent 但我没有看到它说可以将文件拖放到它的图标上。它也没有说明 Windows 是否支持该功能。
奖励积分:
由于未安装 Flash Builder 应用程序(通过调试启动),如何对此进行测试。
我已经在 Mac 上运行并且应该在 Windows 上运行。
当您注册不同的文件类型时,您可以将文件拖放到图标上,应用程序将打开,并在为其添加事件侦听器后调度调用事件(问题 1 已解决)。注册文件类型还允许您在 Mac 和 Windows 上使用 "Open with..."(问题 2 已解决)。
如果您使用 Flash Builder 或其他 IDE 来测试您的调用函数是否有效,您可以在 Run/Debug 启动参数中添加文件路径(加分)。在它周围加上引号并用 space 分隔以添加其他参数。
您必须向应用程序添加一个事件侦听器,然后所有调用都将被调度。在那之前,他们只是排队。
当您将多个文件拖放到 Mac 上的应用程序图标上时,将出现一个带有多个参数的调用事件。在 Windows 和 Linux 上,此事件将使用单个参数分派多次。每个参数都是文件的完整路径。
如果您正常打开应用程序并侦听调用事件,即使应用程序上没有放置任何文件,也会分派该事件。这是一个标准的调用类型,不包含任何参数。
下面的示例侦听调用事件并处理不同的情况。
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
invoke="windowedapplication1_invokeHandler(event)"
>
<fx:Script>
<![CDATA[
protected function windowedapplication1_invokeHandler(event:InvokeEvent):void {
var invokeArguments:Array = event.arguments;
var filePath:String;
var stream:FileStream;
var file:File;
var testing:Boolean = false;
// application icon had a file dropped on it or an associated file was double clicked while app was open
if (event.reason == InvokeEventReason.STANDARD && invokeArguments.length) {
invokeWithFile(event.currentDirectory, invokeArguments);
}
// application opened normally
if (event.reason == InvokeEventReason.STANDARD &&
(invokeArguments.length == 0 || testing)) {
if (testing) {
invokeArguments = ["/Users/me/Desktop/test.jpg"];
}
invokeWithFile(event.currentDirectory, invokeArguments);
return;
}
// application opened at login
if (event.reason == InvokeEventReason.LOGIN) {
return;
}
// application opened from URL
if (event.reason == InvokeEventReason.OPEN_URL) {
return;
}
// application opened from notification such as iOS APN
if (event.reason == InvokeEventReason.NOTIFICATION) {
return;
}
}
public var invokedFile:File;
/**
* Invoked file.
* */
public function invokeWithFile(currentDirectory:File, invokedArguments:Array):void {
var filePath:String = invokedArguments && invokedArguments.length ? invokedArguments[0] : null;
var fileData:String;
var fileStream:FileStream;
var file:File;
if (filePath) {
try {
file = new File(filePath);
}
catch (errorEvent:*) {
trace("Error: " + errorEvent.toString());
return;
}
if (file &&
file.exists &&
file.isDirectory==false &&
file.extension &&
file.extension.toLowerCase()=="mxml") {
fileStream = new FileStream();
try {
fileStream.open(file, FileMode.READ);
if (fileStream.bytesAvailable) {
fileData = fileStream.readUTFBytes(fileStream.bytesAvailable);
}
}
catch (error:*) {
}
}
}
}
]]>
</fx:Script>
</s:WindowedApplication>
这是应用程序描述符文件。文件类型是严格的。我在内嵌了注释:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<application xmlns="http://ns.adobe.com/air/application/20.0">
<fileTypes>
<fileType>
<!-- name cannot contain a space -->
<name>MXML.File</name>
<!-- extension cannot be * -->
<extension>mxml</extension>
<description>MXML file</description>
<!-- content type is required -->
<contentType>text/plain</contentType>
</fileType>
<fileType>
<name>Photoshop.Image</name>
<extension>psd</extension>
<description>Adobe Photoshop Image</description>
<contentType>application/octet-stream</contentType>
</fileType>
<fileType>
<name>JPEG.Image</name>
<extension>jpg</extension>
<description>JPEG Image</description>
<contentType>image/jpeg</contentType>
</fileType>
<fileType>
<name>JPEG.Image</name>
<extension>jpeg</extension>
<description>JPEG Image</description>
<contentType>image/jpeg</contentType>
</fileType>
<fileType>
<name>PNG.Image</name>
<extension>png</extension>
<description>PNG Image</description>
<contentType>image/png</contentType>
</fileType>
<fileType>
<name>GIF.Image</name>
<extension>gif</extension>
<description>GIF Image</description>
<contentType>image/gif</contentType>
</fileType>
</fileTypes>
</application>
文件类型错误:
错误 104:需要 application.fileTypes.fileType.contentType。
解决方案:您必须包括内容类型。网上其他地方提到它是可选的,但现在可能是必需的。
错误 104:需要 application.fileTypes.fileType.extension。
解决方案:您必须包括扩展名。
错误 105:application.fileTypes.fileType.extension 包含无效值。
解决方法:扩展名不能为空或*.
错误 105:application.fileTypes.fileType.name 包含无效值。
解决方法:名称中不能包含space字符。
在 Mac 上,可以将文件拖放到应用程序停靠栏中的应用程序图标上。是否有可能获得该信息,是否有可能在 Windows 上做同样的事情?
我一直在阅读 InvokeEvent 但我没有看到它说可以将文件拖放到它的图标上。它也没有说明 Windows 是否支持该功能。
奖励积分:
由于未安装 Flash Builder 应用程序(通过调试启动),如何对此进行测试。
我已经在 Mac 上运行并且应该在 Windows 上运行。
当您注册不同的文件类型时,您可以将文件拖放到图标上,应用程序将打开,并在为其添加事件侦听器后调度调用事件(问题 1 已解决)。注册文件类型还允许您在 Mac 和 Windows 上使用 "Open with..."(问题 2 已解决)。
如果您使用 Flash Builder 或其他 IDE 来测试您的调用函数是否有效,您可以在 Run/Debug 启动参数中添加文件路径(加分)。在它周围加上引号并用 space 分隔以添加其他参数。
您必须向应用程序添加一个事件侦听器,然后所有调用都将被调度。在那之前,他们只是排队。
当您将多个文件拖放到 Mac 上的应用程序图标上时,将出现一个带有多个参数的调用事件。在 Windows 和 Linux 上,此事件将使用单个参数分派多次。每个参数都是文件的完整路径。
如果您正常打开应用程序并侦听调用事件,即使应用程序上没有放置任何文件,也会分派该事件。这是一个标准的调用类型,不包含任何参数。
下面的示例侦听调用事件并处理不同的情况。
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
invoke="windowedapplication1_invokeHandler(event)"
>
<fx:Script>
<![CDATA[
protected function windowedapplication1_invokeHandler(event:InvokeEvent):void {
var invokeArguments:Array = event.arguments;
var filePath:String;
var stream:FileStream;
var file:File;
var testing:Boolean = false;
// application icon had a file dropped on it or an associated file was double clicked while app was open
if (event.reason == InvokeEventReason.STANDARD && invokeArguments.length) {
invokeWithFile(event.currentDirectory, invokeArguments);
}
// application opened normally
if (event.reason == InvokeEventReason.STANDARD &&
(invokeArguments.length == 0 || testing)) {
if (testing) {
invokeArguments = ["/Users/me/Desktop/test.jpg"];
}
invokeWithFile(event.currentDirectory, invokeArguments);
return;
}
// application opened at login
if (event.reason == InvokeEventReason.LOGIN) {
return;
}
// application opened from URL
if (event.reason == InvokeEventReason.OPEN_URL) {
return;
}
// application opened from notification such as iOS APN
if (event.reason == InvokeEventReason.NOTIFICATION) {
return;
}
}
public var invokedFile:File;
/**
* Invoked file.
* */
public function invokeWithFile(currentDirectory:File, invokedArguments:Array):void {
var filePath:String = invokedArguments && invokedArguments.length ? invokedArguments[0] : null;
var fileData:String;
var fileStream:FileStream;
var file:File;
if (filePath) {
try {
file = new File(filePath);
}
catch (errorEvent:*) {
trace("Error: " + errorEvent.toString());
return;
}
if (file &&
file.exists &&
file.isDirectory==false &&
file.extension &&
file.extension.toLowerCase()=="mxml") {
fileStream = new FileStream();
try {
fileStream.open(file, FileMode.READ);
if (fileStream.bytesAvailable) {
fileData = fileStream.readUTFBytes(fileStream.bytesAvailable);
}
}
catch (error:*) {
}
}
}
}
]]>
</fx:Script>
</s:WindowedApplication>
这是应用程序描述符文件。文件类型是严格的。我在内嵌了注释:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<application xmlns="http://ns.adobe.com/air/application/20.0">
<fileTypes>
<fileType>
<!-- name cannot contain a space -->
<name>MXML.File</name>
<!-- extension cannot be * -->
<extension>mxml</extension>
<description>MXML file</description>
<!-- content type is required -->
<contentType>text/plain</contentType>
</fileType>
<fileType>
<name>Photoshop.Image</name>
<extension>psd</extension>
<description>Adobe Photoshop Image</description>
<contentType>application/octet-stream</contentType>
</fileType>
<fileType>
<name>JPEG.Image</name>
<extension>jpg</extension>
<description>JPEG Image</description>
<contentType>image/jpeg</contentType>
</fileType>
<fileType>
<name>JPEG.Image</name>
<extension>jpeg</extension>
<description>JPEG Image</description>
<contentType>image/jpeg</contentType>
</fileType>
<fileType>
<name>PNG.Image</name>
<extension>png</extension>
<description>PNG Image</description>
<contentType>image/png</contentType>
</fileType>
<fileType>
<name>GIF.Image</name>
<extension>gif</extension>
<description>GIF Image</description>
<contentType>image/gif</contentType>
</fileType>
</fileTypes>
</application>
文件类型错误:
错误 104:需要 application.fileTypes.fileType.contentType。
解决方案:您必须包括内容类型。网上其他地方提到它是可选的,但现在可能是必需的。
错误 104:需要 application.fileTypes.fileType.extension。
解决方案:您必须包括扩展名。
错误 105:application.fileTypes.fileType.extension 包含无效值。
解决方法:扩展名不能为空或*.
错误 105:application.fileTypes.fileType.name 包含无效值。
解决方法:名称中不能包含space字符。