Allow user to select file name (Open file name picker dialog)

The XRUNandWAIT command allows you to start any external script, and it can help here.

In the demo below I start a small Powershell script that opens the FileDialog. The result is stored in a text file. Back inside the macro we read this text file with csvRead, and then have the file name with full path in ${!COL1}.

Powershell script “getfilename.ps1”:

Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "CSV Files (*.csv)|*.txt|All Files (*.*)|*.*"
$f.ShowHelp = $true
$f.Multiselect = $false
[void]$f.ShowDialog()

Write $f.FileName
Set-Content -Path 'c:\test\text.csv' -Value $f.FileName
[Environment]::Exit(1)       

Macro:

{
  "Name": "OpenFileDialog",
  "CreationDate": "2020-6-22",
  "Commands": [
    {
      "Command": "echo",
      "Target": "Demo script for file name picker",
      "Value": "green"
    },
    {
      "Command": "XRunAndWait",
      "Target": "Powershell.exe",
      "Value": "-executionpolicy bypass c:\\test\\getfilename.ps1"
    },
    {
      "Command": "echo",
      "Target": "Powershell return code ${!xrun_exitcode}",
      "Value": "blue"
    },
    {
      "Command": "comment",
      "Target": "Powershell script stores file name in text file => read it as CSV file",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "false",
      "Value": "!StringEscape"
    },
    {
      "Command": "csvRead",
      "Target": "c:\\test\\text.csv",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "Selected file name = ${!COL1}",
      "Value": "blue"
    }
  ]
}
1 Like