Search code examples
fileactionscript-3air

open file location with as3


I have a <s:List /> that contain a bunch of files. On right click I open a menu at the (x, y) position of the mouse that lets the user "Open File Location". My struggle is opening the file location and selecting (without opening) the file much like Window's explorer. The closest I have come in opening the parent folder and using file.openWithDefaultApplication();, which opens the folder that the file is in without showing the user the actual file.

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);
        }

    }

Example

enter image description here enter image description here


Solution

  • What I ended up doing was creating a .cmd file (just a renamed .bat file) that opens up a directory with the /select argument on the file.

    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