用as3打开文件位置

时间:2016-01-25 18:49:01

标签: file actionscript-3 air

我有一个<s:List />,其中包含一堆files。右键单击我在鼠标的(x,y)位置打开一个菜单,让用户“打开文件位置”。我的斗争是打开文件位置并选择(不打开)文件,就像Window的浏览器一样。我最近打开父文件夹并使用file.openWithDefaultApplication();打开文件所在的文件夹而不向用户显示实际文件。

MXML

        <s:List
            id="fileDownList"
            height="100%"
            width="100%"
            dataProvider="{files}"
            labelField="name"
            allowMultipleSelection="false"
            rightClick="rightMouseDown(event)"
            itemRollOver="currentItem = event.itemRenderer.data"
            />

AS3

    private function rightMouseDown(event:MouseEvent):void {
        createMenu(currentItem, event.stageX, event.stageY);
    }

        private function createMenu(item:Object, xPos:int, yPos:int):void {
        if (menu != null) {
            menu.hide();
        }
        var menuItems:Array = [];

        menuItems.push({label:"Open File Location"),
            func: function run():void{
                //runs on doMenuAction listener, need to open location here

            }

        });

        if (menuItems.length > 0) {
            menu = Menu.createMenu(tree, menuItems);
            //noinspection JSValidateTypes
            menu.addEventListener(MenuEvent.ITEM_CLICK, doMenuAction);
        }

        if (menu != null) {
            menu.show(xPos, yPos);
        }

    }

示例

enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

试一试......使用NativeProcess和一些Explorer.exe参数,结果可能

这是一个基本的AS3示例。请测试然后在代码中应用逻辑:

//# String holds required file path
//# example ::: myfileURL = "C:\\myFolder\\mySubFolder\\myImage.jpg";
public var myfileURL : String = "";

myfileURL = "C:\\VC1\\Tests\\CoolSong.mp3"; //update this before running function
openWindows_FileSelected(); //run the function

private function openWindows_FileSelected ():void  
{  
    var explorer:File = new File("C:\\Windows\\explorer.exe");

    if (explorer.exists)  
    {  
        var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();  
        nativeProcessStartupInfo.executable = explorer;  

        var args:Vector.<String> = new Vector.<String>();  

        args.push("/select,"); 
        args.push( myfileURL ); //file to auto-highlight

        nativeProcessStartupInfo.arguments = args;  
        process = new NativeProcess();  
        process.start(nativeProcessStartupInfo);  
    }  

} 


PS:

我能想到的唯一问题......因为您正在使用File,您必须通过File .nativePath命令获取其路径的字符串它给出了一个字符串:
"C:/myFolder/mySubFolder/myImage.jpg"

但是要使上面的代码工作,你必须进行替换(尝试String方法Split/Join)并使其看起来像:
"C:\\myFolder\\mySubFolder\\myImage.jpg"
如果你不用双反斜杠替换所有单个正斜杠,那么Explorer.exe不会喜欢它,你总会得到一个错误......

答案 1 :(得分:1)

我最终做的是创建一个.cmd文件(只是一个重命名的.bat文件),打开一个文件上带有/select参数的目录。

<强> AS3

private function run():void{
                        var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
                        var file:File = File.applicationDirectory.resolvePath("C:\\Users\\Me\\Desktop\\launcher.cmd");
                        nativeProcessStartupInfo.executable = file;

                        var processArgs:Vector.<String> = new Vector.<String>();
                        processArgs[0] = item.url;
                        nativeProcessStartupInfo.arguments = processArgs;

                        process = new NativeProcess();
                        process.start(nativeProcessStartupInfo);

                    }

<强> launcher.cmd

@ECHO OFF
SET /a LOCATION=%1
explorer /select, %1