Prerequisites
- Products: Liquid UI WS, Liquid UI Server or Local DLL, Client Software
- Commands: del(), pushbutton(), message(), openfile()
Purpose
The article demonstrates how to use PowerShell and Liquid UI functions to create batch files that open the file picker dialog. This allows users to interactively select Excel spreadsheets and capture the selected file paths. The code is based on the SAP Easy Access screen and manages temporary files for storing the output.
- Delete the image container on the SAP Easy Access screen.
- Add a push button to execute the process.
- Add three functions to let the user select a file, run a PowerShell command, and generate and execute a batch script.
User Interface
//Create the file SAPLSMTR_NAVIGATION.E0100.sjs inside your scripts folder for customizing the SAP Easy Access screen
//Now, add the Liquid UI script to the above file and save it.
Customization
- Delete the image container on the SAP Easy Access screen using del().
// Deletes the image container on the SAP screen del("X[IMAGE_CONTAINER]");
- Create a push button labeled Select File- PowerShell to execute the selectFilePowerShell process on click.
// Creates a push button to execute the process pushbutton([19,42], "Select File - PowerShell",'?', { "process":selectFilePowerShell, "size":[2,25]});
- Add the selectFilePowerShell() to handle the file selection via PowerShell.
// Function to handle the file selection process via PowerShell 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 //Function to execute the batch command for opening the file selection dialog 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 a valid file has been selected, remove the temporary output file if(variableX != 'undefined') { println('\n\nBefore Removing File .... variableX ... : ' + variableX + '....selectFilePowerShell() .....\n\n'); removefile(tempOutputFile); } enter('?'); }
- Add the getPowerShellFormsExecutionString function, which constructs a batch script to invoke a PowerShell command for opening a file dialog and capturing the selected file(s).
// Function to execute the batch file command that invokes the file selection dialog 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"; }
- Add the executeOpenFileDilaogCommand function to create and execute the batch file that runs the Powershell script for file selection.
//Function to create and execute the batch file 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 batch file after writing the content 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);} }
SAP Process
- Refresh the SAP screen and click Select File - Powershell push button to open the file selection dialog.