using LoginPI.Engine.ScriptBase; using System.IO; public class fileSaveTimer : ScriptBase { private void Execute() { /* -Disclaimer: This workload is provided as-is and might need further configuration and customization to work successfully in each unique environment. For further Professional Services-based customization please consult with the Login VSI Support and Services team. Please refer to the Help Center section "Application Customization" for further self-help information regarding workload crafting and implementation. -Run the workload against the "target" using Script Editor to ensure it will work before uploading it and testing with it -Workload version and changelist: --V1.0 | original -Leave Application running compatibility: true Workload developed with: -Login PI ScriptingToolSet ver. 3.5.9 -Windows 10 x64 Pro winver 1903 18362.356 -Microsoft Office Excel 365 (Desktop version; reskinned 2019) ver. 1907 This script will: -Save a file to a specified location. For example: opening a large .xls file into Excel, then Save As the file to a defined path, then start a timer after file saving started, then stop the timer after the application's window title indicates the file's been saved, then close the application -Create file path and delete existing target file path, if necessary -Open defined application and the defined file path -Find the window title, which should be present once the initial file has loaded -Open the Save As dialog box with the defined keyboard shortcut -Wait for Save As dialog box to appear and find the File Name field -Type to the File Name field the defined file destination path; submit the Save As dialog -Start custom timer -Wait for a defined window title to be present, which should only be present once the destination file has finished saving -Stop the custom timer -Close the target application Please update the application launch path to include the file needing to be opened, such as: excel.exe c:\temp\mysheetinput.xlsx Please update the following variables: */ // Locate the ControlAfterApplicationInitialLoad and FileNameFieldControl defined variables in this document. Update these lines appropriately for the target application. // Use the Application XRay tool to determine this. Read up more about the Application XRay tool's usage, if need be, in Login PI's documentation // For ControlAfterApplicationInitialLoad variable, add the information for a control that should only be present once the initial loading of the application is done and the application should be interactive; example: // var ControlAfterApplicationInitialLoad = MainWindow.FindControl(className : "DataGrid", title : "Grid",timeout:GlobalTimeoutInSeconds); // Which is visible in the Excel 2019 main UI when a spreadsheet has loaded // For FileNameFieldControl variable, add the information for the File Name input field in the Save As dialog box; example: // SaveAsWindow.FindControl(className : "Edit:Edit", title : "File name:",timeout:GlobalTimeoutInSeconds); // Which is visible in Excel 2019 when the File Name field in the Save As dialog box is present string OutputFileFolder = @"c:\temp\"; // This is for creating the path in which the file will be saved, if it doesn't exist. This is so the target application won't produce an error message such as "The specified directory doesn't exist" when trying to save the file; example "c:\temp\" string ProcessNameTaskkill = "excel*"; // To ensure the target application isn't running before the START(); metafunction, put the process name of the target application here; example: "excel*" int GlobalTimeoutInSeconds = 60; // Define how long it should take maximum for operations to complete, such as opening an Excel document, in seconds. This script will fail, as designed, if this value is surpassed; example: 60 double WaitSeconds = 0.5; // Define here how many seconds to wait in between functions; example: 5. This will allow for the target application's UI to catch up int CharactersPerMinuteToType = 180; // Define how many characters per minute the typing function will type; example; 180 string OutputFilePath = @"c:\temp\mysheetoutput.xlsx"; // Put the path to save the file at; example: "c:\temp\mysheetoutput.xlsx". Please note this will also delete this file path first in this script, if it exists. A UNC/network path would also work here string InitialWindowTitleName = "*mysheetinput.xlsx*"; // Put the window title name that will be present once the source file has been opened within it; example: "*mysheetinput.xlsx*" string WhatToTypeToOpenSaveAs = "{F12}"; // Put the keyboard shortcut that should open up the application's Save As dialog; example: "{F12}" (which will open the Save As dialog in Excel) string SaveAsDialogWindowTitleName = "*Save As*"; // Put the name of the target application's Window title here; example: "*Save As*" string KeystrokesToClearOutFilenameField = "{CTRL+a}{BACK}"; // Put the keystrokes combination to select all text and clear out a field; example "{CTRL+a}{BACK}" -- which will do ctrl+a -> backspace int FileSaveTimeout = 60; // Add the maximum amount of time the file save operation should take in seconds; example: 60 // Note the script will fail, by design, if this value threshold is surpassed string WindowTitleAfterSave = "*mysheetoutput.xlsx*"; // Put the string that should be present in the application's title once the file has been save; example: "*mysheetoutput.xlsx - Saved*" // Ensuring the target application isn't already running ShellExecute("cmd /c taskkill /f /im " + ProcessNameTaskkill,waitForProcessEnd:true,timeout:GlobalTimeoutInSeconds); // These are file system prepare tasks if(File.Exists(OutputFilePath))// This will delete OutputFilePath variable path, if it exists { File.Delete(OutputFilePath); } System.IO.Directory.CreateDirectory(OutputFileFolder); // This will create the path in the OutputFileFolder variable, if it doesn't exist START(mainWindowTitle:InitialWindowTitleName,timeout:GlobalTimeoutInSeconds); // This will start the application, looking for the defined filename in the title Wait(seconds:WaitSeconds); var ControlAfterApplicationInitialLoad = MainWindow.FindControl(className : "DataGrid", title : "Grid",timeout:GlobalTimeoutInSeconds); // Add the information for a control that should only be present once the initial loading of the application is done and the application should be interactive; example: // var ControlAfterApplicationInitialLoad = MainWindow.FindControl(className : "DataGrid", title : "Grid",timeout:GlobalTimeoutInSeconds); // Which is visible in the Excel 2019 main UI when a spreadsheet has loaded Wait(seconds:WaitSeconds); Type(WhatToTypeToOpenSaveAs,cpm:CharactersPerMinuteToType,hideInLogging:false); // This will type the keyboard shortcut to open the Save As dialog var SaveAsWindow = FindWindow(title:SaveAsDialogWindowTitleName,timeout:GlobalTimeoutInSeconds); // This will look for the window and bring it into focus Wait(seconds:WaitSeconds); SaveAsWindow.Focus(); var FileNameFieldControl = SaveAsWindow.FindControl(className : "Edit:Edit", title : "File name:",timeout:GlobalTimeoutInSeconds); // Add the information for the File Name input field in the Save As dialog box; example: // SaveAsWindow.FindControl(className : "Edit:Edit", title : "File name:",timeout:GlobalTimeoutInSeconds); // Which is visible in Excel 2019 when the File Name field in the Save As dialog box is present Wait(seconds:WaitSeconds); FileNameFieldControl.Click(); // Will click the File Name field control Wait(seconds:WaitSeconds); Type(KeystrokesToClearOutFilenameField,cpm:CharactersPerMinuteToType); // Will type out the keystrokes to clear out the File Name field // The following will type in the OutputFilePath and type enter, to submit the Save As page/commit saving the file. Then a custom timer is started. Type(OutputFilePath +"{enter}",cpm:CharactersPerMinuteToType); StartTimer(name:"FileSavingTime"); FindWindow(title : WindowTitleAfterSave,timeout:FileSaveTimeout); // Will wait for the window title to be present, which should only be present once the file has saved StopTimer(name:"FileSavingTime"); // Stopping the custom timer Wait(seconds:WaitSeconds); STOP(); // This will close out of the application Wait(seconds:WaitSeconds); } }