Search code examples
actionscript-3apache-flexairfilesystemsadobe

Is there a way generate a shortcut file with adobe air?


Good afternoon,
I would like create a application that can can create folders and short cuts to folders in the file system. The user will click a button and it will put a folder on there desktop that has short cuts to files like //server/folder1/folder2 Can you create a desktop shortcut with code in adobe air? How would you do that? How do you create a folder? I keep thinking this should be easy but i keep missing it.
Thank you for your help sorry for the trouble,
Justin


Solution

  • If your deployment profile is Extended Desktop, you may be able to use NativeProcess and some simple scripts that you could package with your app. This approach would entail handling the functionality on a per OS basis, which would take some work and extensive testing. However, I wanted to at least share a scenario that I verified does work. Below is a test case that I threw together:

    Test Case: Windows 7

    Even though the Adobe documentation says that it prevents execution of .bat files, apparently it doesn't prevent one from executing the Windows Scripting Host: wscript.exe. This means you can execute any JScript or VBScript files. And this is what you would use to write a command to create a shortcut in Windows (since Windows doesn't have a commandline command to create shortcuts otherwise).

    Here's a simple script to create a shortcut command, which I found on giannistsakiris.com, (converted to JScript):

    // File: mkshortcut.js
    
    var WshShell = new ActiveXObject("WScript.Shell");
    var oShellLink = WshShell.CreateShortcut(WScript.Arguments.Named("shortcut") + ".lnk");
    oShellLink.TargetPath = WScript.Arguments.Named("target");
    oShellLink.WindowStyle = 1;
    oShellLink.Save();
    

    If you package this in your application in a folder named utils, you could write a function to create a shortcut like so:

    public function createShortcut(target:File, shortcut:File):void {
      if (NativeProcess.isSupported) { // Note: this is only true under extendedDesktop profile
        var shortcutInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
    
        // Location of the Windows Scripting Host executable
        shortcutInfo.executable = new File("C:/Windows/System32/wscript.exe");
    
        // Argument 1: script to execute
        shortcutInfo.arguments.push( File.applicationDirectory.resolvePath("utils/mkshortcut.js").nativePath);
    
        // Argument 2: target
        shortcutInfo.arguments.push("/target:" + target.nativePath);
    
        // Argument 3: shortcut
        shortcutInfo.arguments.push("/shortcut:" + shortcut.nativePath);
    
        var mkShortcutProcess = new NativeProcess();
        mkShortcutProcess.start(shortcutInfo);
    
      }
    }
    

    If one wanted to create a shortcut to the Application Storage Directory on the Desktop, the following would suffice:

    var targetLocation:File = File.applicationStorageDirectory;
    var shortcutLocation:File = File.desktopDirectory.resolvePath("Shortcut to My AIR App Storage");
    
    createShortcut(targetLocation, shortcutLocation);
    

    Obviously there's a lot of work to be done to handle different OS environments, but this is at least a step.