# $language = "VBScript" # $interface = "1.0" ' SaveScreenTextToFile.vbs ' Last Modified: 28 Jan, 2014 ' - Check for selected text on the screen and save/open that instead ' of the entire screen. ' - Check for command line arg "/Mode:Screen". If present, open the ' entire screen for editing (this is the default behavior, but the ' presence of /Screen arg will override text being selected on the ' screen. ' - Check for command line arg "/Mode:Scrollback". If present, both ' the screen and the scrollback contents are saved/opened. This also ' overrides any text being selected on the screen at the time the script ' is launched. ' - Check for command line arg "/Mode:Selection". If present, preferred ' mode would be to always operate on selection. If no selection is ' present, screen mode would be used. ' - Check for command line arg "/Prompt:". If value is off|no|0, ' individuals are not prompted for filename before saving/opening. ' ' Description: ' Saves text on the screen (or in the scrollback buffer, depending on args ' provided to the script) to a unique file located in user's documents ' folder (user can change the name of the file if desired, as they are ' prompted to confirm the name -- unless the /Prompt:No arg is present). ' ' What gets saved to the screen if no args are present depends on whether ' or not there is anything on the screen currently selected. ' - If there is any non-whitespace text selected on the screen, that ' text that is selected is what gets "peeled off" into a separate file. ' - If there is nothing selected on the screen, the entire screen gets ' "peeled off" into a separate file. ' ' If the /mode:scrollback argument is specified when running the script ' (for example, if you configure a button on SecureCRT's button bar to ' run the script with argument of /mode:scrollback, then the entire screen ' and all the contents of the scrollback buffer are "peeled off" into a ' separate file. ' ' The separate file that is saved is saved by default with a .txt file ' extension and the default application assigned to open such files is ' launched automatically for viewing/editing. Note that you can change ' the extension of the file to something different (when prompted) as a ' quick way of changing which app gets launched to view the data. For ' example, if you have primarily comma-separated values selected or on the ' screen, rename the file to .csv in order to automatically have Excel (or ' whichever app is assigned to handle .csv files) launched to view the data. ' ' Demonstrates: ' - How to use the crt.Dialog.FileOpenDialog API (SecureCRT 6.7 and later) ' - How to use the Screen.Get2 property to get data from the screen where ' individual lines are marked with CRLF line ending characters. ' - How to use the Scripting.FileSystemObject to write data to a ' text file. ' - One way of handling arguments passed to a script. ' Const ForWriting = 2 ' Be "tab safe" by getting a reference to the tab for which this script ' has been launched: Set objTab = crt.GetScriptTab Set g_shell = CreateObject("WScript.Shell") Set g_fso = CreateObject("Scripting.FileSystemObject") g_strMode = "" g_bPromptForFilename = True ' First order of work to do is handle any arguments that may have been ' supplied to the script. We'll do this in a subroutine to keep things ' more clean. ProcessCommandLineArgs SaveScreenTextToFile '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub SaveScreenTextToFile() Select Case g_strMode Case "screen" ' Transfer the data from the SecureCRT screen into a variable. Using ' Screen.Get2() instead of Screen.Get() allows us to separate each ' line with \r and \n characters so that lines appear in the file ' the same way they appears in the SecureCRT window. strScreenData = _ objTab.Screen.Get2(1, 1, crt.screen.Rows, crt.Screen.Columns) Case "scrollback" crt.Screen.SendSpecial("MENU_SELECT_ALL") strScreenData = crt.Screen.Selection Case "selection" strSelection = crt.Screen.Selection If IsBlockEmpty(strSelection) Then ' Fall back to "screen" mode if there's nothing selected on the ' screen. g_strMode = "screen" SaveScreenTextToFile Exit Sub End If strScreenData = strSelection End Select If IsBlockEmpty(strScreenData) Then crt.Dialog.MessageBox "Nothing available to save!" Exit Sub End If Do If strFilename = "" Then ' Create a unique filename for starters (user will be prompted to ' change the name if the /Prompt:No arg is present as argument to ' this script). Do strFilename = g_shell.SpecialFolders("MyDocuments") & _ "\SCRT_" & g_strMode & "_" & GetUniqueFileName Loop Until Not g_fso.FileExists(strFilename) End If strFilenameBefore = strFilename If g_bPromptForFilename Then ' Now prompt the user for the filename to use, defaulting to the ' name determined above strFilename = _ crt.Dialog.FileOpenDialog("Specify Filename to Save", "Save", _ strFilename, "Text Files (*.txt)|*.txt||") If strFilename = "" Then Exit Sub End If If g_fso.FileExists(strFilename) Then If crt.Dialog.MessageBox("Filename already exists!" & vbcrlf & _ vbcrlf & vbtab & strFilename & vbcrlf & vbcrlf & _ "Do you wish to overwrite this file?", _ "Overwrite File?", _ vbYesNo) = vbYes Then Exit Do Else Exit Do End If Loop Set objFile = g_fso.OpenTextFile(strFilename, ForWriting, True) objFile.Write strScreenData & vbcrlf objFile.Close ' Now open the text file in the default .txt file application handler... g_shell.Run chr(34) & strFilename & chr(34) End Sub '----------------------------------------------------------------------------- Function IsBlockEmpty(strBlockText) strTempValue = strBlockText strTempValue = Replace(strTempValue, vbcrlf, "") strTempValue = Replace(strTempValue, vbcr, "") strTempValue = Replace(strTempValue, vblf, "") strTempValue = Replace(strTempValue, vbtab, "") strTempValue = Replace(strTempValue, vbcrlf, "") strTempValue = Replace(strTempValue, vbcrlf, "") strTempValue = Trim(strTempValue) If strTempValue = "" Then IsBlockEmpty = True End Function '----------------------------------------------------------------------------- Function ProcessCommandLineArgs() If crt.Arguments.Count > 0 Then For nIndex = 0 To crt.Arguments.Count - 1 strOrigArg = crt.Arguments(nIndex) strArg = LCase(strOrigArg) If InStr(strArg, "/mode:") > 0 Then nPos = Instr(strArg, ":") strArg = Mid(strArg, nPos + 1) strArg = LCase(strArg) strArg = Trim(strArg) Select Case strArg Case "screen" g_strMode = "screen" Case "scrollback" g_strMode = "scrollback" Case "selection" g_strMode = "selection" Case Else crt.Dialog.MessageBox _ "Unknown /mode: passed in as arg: " & _ strOrigArg & vbcrlf & vbcrlf & _ "Allowed values are: screen | scrollback | selection" End Select ElseIf InStr(strArg, "/prompt:") > 0 Then nPos = Instr(strArg, ":") strArg = Mid(strArg, nPos + 1) strArg = LCase(strArg) strArg = Trim(strArg) Select Case strArg Case "off", "no", "0" g_bPromptForFilename = False Case "on", "yes", "1" g_bPromptForFilename = True Case Else crt.Dialog.MessageBox _ "Unknown /prompt: passed in as arg: " & _ strOrigArg & vbcrlf & vbcrlf & _ "Allowed values are: on | off | yes | no | 1 | 0 " End Select End If Next End If If g_strMode = "" Then ' If no mode args were passed, try to heuristically determine what to do ' (screen or selection?) ' If there's selected text on the screen, let's set mode to selection. ' Otherwise, just use screen mode. If Trim(crt.Screen.Selection) <> "" Then g_strMode = "selection" Else g_strMode = "screen" End If End If End Function '----------------------------------------------------------------------------- Function GetUniqueFileName() Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem") For Each objItem In colItems strLocalDateTime = objItem.LocalDateTime Exit For Next ' strLocalDateTime has the following pattern: ' 20111013093717.418000-360 [ That is, YYYYMMDDHHMMSS.MILLIS(zone) ] ' Take the left-most 18 digits... strFilename = Left(strLocalDateTime, 18) bReallyParanoid = False If bReallyParanoid Then ' ...add a random string to it, to reduce likelihood of collisions within the ' same millisecond... strFilename = strFilename & "_" & _ CreateObject("Scripting.FileSystemObject").GetTempName & ".txt" ' ...Remove the ".tmp" portion and the "rad" prefix... strFilename = Replace(strFilename, ".tmp", "") strFilename = Replace(strFilename, "rad", "") Else strFilename = strFilename & ".txt" End If ' ... return the unique filename as value of the function: GetUniqueFileName = strFilename End Function