【问题标题】:open file location with as3用 as3 打开文件位置
【发布时间】:2016-01-25 18:49:00
【问题描述】:

我有一个<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);
        }

    }

示例

【问题讨论】:

  • 不清楚你在问什么。另外——基于浏览器?空气?
  • @Sleeper 哪一部分不清楚,所以我可以澄清一下?这是一个 AIR 桌面应用程序。
  • @Sleeper,你用过 Windows 文件搜索吗?当它为您提供文件名列表时,您可以右键单击一个并选择“打开文件位置”,这将打开一个新的资源管理器窗口并自动向下滚动到自动为您突出显示/选择的所述文件...
  • @Jordan.J.D,我不知道这是否可能。也许您可以执行一个 Windows CMD 终端命令(作为 NativeProcess?)以突出显示预期的文件。您必须四处搜索才能看到
  • @Sleeper,如果你还需要什么,请告诉我。

标签: file actionscript-3 air


【解决方案1】:

试一试...结果是有可能使用 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 将不会喜欢它,并且您总是会收到错误...

【讨论】:

  • 文件有一个url 属性,您可以使用它来代替拆分和连接。
  • 啊!!早该想到的对基于 AIR 的代码仍然很陌生。你的问题是我第三次尝试。只需要将 AS3 代码重新编译到 Android。
【解决方案2】:

我最终做的是创建一个 .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

【讨论】:

    猜你喜欢
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多