Author Topic: File Open Dialog Using PowerShell in Liquid UI  (Read 3353 times)

umang@guixt.com

  • Administrator
  • Newbie
  • *****
  • Posts: 34
    • View Profile
File Open Dialog Using PowerShell in Liquid UI
« on: October 01, 2024, 03:53:04 PM »
Below code demonstrates the use of powershell to execute a file open dialog for selecting an excel spreadsheet


del("X[IMAGE_CONTAINER]");
//pushbutton([23,42], "Select File - UserAccounts  ",{ "process":selectFileUserAccounts, "size":[2,25]});
//pushbutton([7,42], "Select File - Execute PS1 ",{ "process":selectFilePS1, "size":[2,25]});
//pushbutton([11,42], "Select Folder - Shell.Application",{ "process":selectFolder, "size":[2,25]});
//pushbutton([15,42], "Select File - CommonDialog",{ "process":selectFileCommonDialog, "size":[2,25]});

pushbutton([19,42], "Select File - PowerShell  ", '?', { "process":selectFilePowerShell, "size":[2,25]});

function selectFilePowerShell() {
   onscreen '*'
   println('\n\nExecuting ... selectFilePowerShell() .....\n\n');
   variableX = 'undefined';
   
   var tempOutputFile = "C:\\LiquidUI\\Scripts\\output.txt";           // Temporary file to store the output from the batch script

   executeOpenFileDilaogCommand(tempOutputFile, false);
   
   do {
      println('\n\n.... Inside do while .....\n\n');
      sleep(500);
      openfile(tempOutputFile, {"delimiter":"\n"});
      readfile(tempOutputFile, {"variableX":true});
      closefile(tempOutputFile);
      println("===============");
      println("\nvariableX:"+ variableX + ":\n");
      println("===============");
   } while (variableX == 'undefined');
   


   if(variableX != 'undefined') {
      println('\n\nBefore Removing File .... variableX ... : ' + variableX + '....selectFilePowerShell() .....\n\n');
      removefile(tempOutputFile);
   }
   
   enter('?');

   //Continue with rest of the function.....
}


// Import the necessary Liquid UI functions
function getPowerShellFormsExecutionString(fileAbsolutePathWithName) {
   println('\n\nExecuting ... getPowerShellFormsExecutionString() .....\n\n');
   //return "@echo off\nsetlocal\n\nrem Run the PowerShell command and store the result in a variable\nfor /f \"tokens=* delims=\" %%i in ('powershell -Command \"Add-Type -AssemblyName System.Windows.Forms; $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $openFileDialog.InitialDirectory = [Environment]::GetFolderPath('Desktop'); $openFileDialog.Filter = 'All files (*.*)|*.*'; $openFileDialog.Multiselect = $true; if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { $openFileDialog.FileNames -join '|' }\"') do set \"SelectedFiles=%%i\"Display the selected files\nif defined SelectedFiles (\nrem echo Selected files: %SelectedFiles%\necho %SelectedFiles% > " + fileAbsolutePathWithName + " \n) else (\nrem echo No file selected\necho DISMISSEDDIALOG > " + fileAbsolutePathWithName + " \n)\n";
   return "@echo off\nsetlocal\n\nrem Run the PowerShell command and store the result in a variable\nfor /f \"tokens=* delims=\" %%i in ('powershell -Command \"Add-Type -AssemblyName System.Windows.Forms; $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $openFileDialog.InitialDirectory = [Environment]::GetFolderPath('Desktop'); $openFileDialog.Filter = 'Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx'; $openFileDialog.Multiselect = $false; if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { $openFileDialog.FileNames -join '|' }\"') do set \"SelectedFiles=%%i\"Display the selected files\nif defined SelectedFiles (\nrem echo Selected files: %SelectedFiles%\necho %SelectedFiles% > " + fileAbsolutePathWithName + " \n) else (\nrem echo No file selected\necho DISMISSEDDIALOG > " + fileAbsolutePathWithName + " \n)\n";

}


function executeOpenFileDilaogCommand(fileName, remove) {
   println('\n\nExecuting ... executeOpenFileDilaogCommand() .....\n\n');
    //var fileName = "C:\\LiquidUI\\tempCommand.txt";
    var batcName = fileName + "_term.bat";
    // Open the file in write mode, which creates it if it does not exist
    openfile(batcName, {"output":true});

    // Write the command string to the file
    name1 = getPowerShellFormsExecutionString(fileName);
    appendfile(batcName, {"name1":true});

    // Close the file
    closefile(batcName);
    system.ShellExecute(batcName, "", "open", 0);
   
    // Optionally read back the content (uncomment if needed)
    // readfile(fileName, {"output":"????"});

    // Remove the file after it has served its purpose
    if(remove) {sleep(2000); removefile(batcName);}
}