2

I'm creating a script to provide choices of programs to launch when the user double-clicks upon a file in Windows Explorer. I associate the selected file extension with the script that has been wrapped into an .exe.

But inside the Launcher script, I need to use the filename the user double-clicked in order to create a command line string to launch the selected program.

How can I get that filename inside the script? e.g. "%1"

Note: a Windows shortcut menu is not appropriate as I will also use this script in a web page.

Thanks!

JohnM
  • 31
  • 1
  • 3
  • 1
    Hi, are you planning to use PowerShell or an executable in a webpage ? It sounds weird. – sodawillow Nov 18 '15 at 20:31
  • 3
    It might just be my poor understand, but this seems discordant: "I need to use the filename the user double-clicked" and then "I will also use this script in a web page" – Mathias R. Jessen Nov 18 '15 at 20:50
  • I am creating a launcher to allow the user a choice of actions when a file is double-clicked. Normally a double-click on a filename provides a single action, determined by the file association. This will be used in a custom dashboard that also uses the file association to determine the action when the filename's hyperlink is clicked. – JohnM Nov 18 '15 at 21:33

2 Answers2

1

The $args variable holds the arguments passed to a PowerShell script.

If you put, for example :

Write-Host $args[0]

in your script, you will see the first argument passed in the script call.

PS > .\myscript.ps1 "test string"

would output

test string
sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • True, but I'm not starting the script with a command line. Instead, Windows is invoking the execution because of the file association. I tried using the $args[0] in my launcher, but it appears to be empty. – JohnM Nov 18 '15 at 22:01
  • I think you can modify that call, at least in the registry, see this : https://msdn.microsoft.com/en-us/library/cc144158%28VS.85%29.aspx from this question : http://stackoverflow.com/questions/1387769/create-registry-entry-to-associate-file-extension-with-application-in-c – sodawillow Nov 18 '15 at 22:04
  • @JohnM I think the MSDN link sodawillow provides gives the clue you need. The "%1" is the filename of the file that was clicked. It needs to be the first token after the name of your script in the "command" key (in the registry) So the missing piece is to add a statement like `param( $fileNameSelected)` at the beginning of your script file. $fileNameSelected will be whatever was specified as first argument on the command line to script. – Χpẘ Nov 18 '15 at 22:34
1

The answer is a combination of @sodawillow and @user2460798 answers. I'm grateful that you each gave me your input. Thankyou!

  1. The script must define a parameter to receive the path & filename from the double-click (note that Param declarations must be prior to any other code) "Param([String]$FileNameSelected)".
  2. The script must use the param $FileNameSelected as an argument when launching a program.
  3. Associate your filename extension with your script (wrapped to an .exe - I used PS2EXE).

Here's a sample script:

Param([String]$FileNameSelected)

$title = "Launch Menu"
$message = "Do you want to launch program A, B, or C?"
$pA = New-Object System.Management.Automation.Host.ChoiceDescription "&Notepad", "Launches Notepad."
$pB = New-Object System.Management.Automation.Host.ChoiceDescription "&B", "Launches program B."
$pC = New-Object System.Management.Automation.Host.ChoiceDescription "&C", "Launches program C."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($pA, $pB, $pC)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)
    {
        0 {
           $wshell = New-Object -ComObject Wscript.Shell
           $executable = 'notepad.exe'
           $argument = '"' + $FileNameSelected + '"'                                
           Start-Process $executable $argument -workingdirectory "c:\windows\system32"
          }
        1 {
           $wshell = New-Object -ComObject Wscript.Shell
           $executable = 'ProgramB.exe'
           $argument = '"' + $FileNameSelected + '"'                                
           Start-Process $executable $argument -workingdirectory "C:\Program Files (x86)\Test"
          }
        2 {
           $wshell = New-Object -ComObject Wscript.Shell
           $executable = 'ProgramC.exe'
           $argument = '"' + $FileNameSelected + '"'                                
           Start-Process $executable $argument -workingdirectory "C:\Program Files\Test"
          }
    }
JohnM
  • 31
  • 1
  • 3