Here’s the problem. I have a line of code that I want to locate containing the words:
If you put all of your JSL code into a single file, then you can just use the script editor search (CTRL+F) to find specific elements of your code. But I like to split my code by organisation units, each in their own include file. Here is an example of what my script might look like before I start executing the Main block of code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Names Default To Here(1); /////////////////////////////////////////////////////////////////////////// // include files /////////////////////////////////////////////////////////////////////////// Include("Enumerated Constants.jsl"); Include("Filepath Definitions.jsl"); Include("Utility Functions.jsl"); Include("Ambr Connector Utilities.jsl"); Include("Version.jsl"); Include("Preferences.jsl"); Include("Ambr Connector Event Handlers.jsl"); Include("File Handling.jsl"); Include("File Import.jsl"); Include("Cell Viability Data Handlers.jsl"); Include("Var List Management.jsl"); /////////////////////////////////////////////////////////////////////////// // local function definitions /////////////////////////////////////////////////////////////////////////// Get Ambr Connector Namespace = Function({},{Default Local}, nsName = "nsAPP.ambr-connector250.pega-analytics.co.uk"; Assert( Namespace Exists(nsName), "Get Ambr Connector Namespace", "Ambr connector namespace does not exist" ); Namespace(nsName); ); /////////////////////////////////////////////////////////////////////////// // MAIN /////////////////////////////////////////////////////////////////////////// // namespace for file paths nsPATH = Get nsPATH Namespace(); nsAPP:ambrPath = Get Ambr Folder Location Preference(); |
In this example I have about a dozen individual JSL files. If I want to locate a segment of code then usually I know where to look because of the logical organisation of the files. But not always. There may be an error message that I want to look for, or a particular variable.
So I have written a script that will allow me to search all of my JSL files within a specific folder:
From here I just specify what I am looking for:
Now it’s easy. I can open the file (there is even an open button!) and search (CTRL+F) for the specific text.
Here’s the code to perform the script preview:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
dir = Get Default Directory(); files = Files In Directory(dir); lstScripts = {}; For (i=1,i<=NItems(files),i++, If (Contains(Lowercase(files[i]),".jsl"), InsertInto(lstScripts,files[i]) ) ); New Window("Script Preview", Border Box(Top(20),Bottom(20),Left(20),Right(20), V List Box( Panel Box("Directory", Text Box(dir,Set Width(600)) ), Spacer Box(Size(0,10)), V List Box( Text Box("Search:"), H List Box( V List Box( Spacer Box(Size(0,2)), teb = Text Edit Box("",Set Width(200)) ), Spacer Box(Size(6,0)), Button Box("search",DoSearch(dir,teb<<Get Text)) ) ), H List Box( V List Box( Spacer Box(Size(0,18)), Text Box("Scripts:"), lb = List Box(lstScripts,Width(600),NLines(16)), Button Box("open",DoOpenScript(dir)) ) ) ) ) ); Do Search = Function({dir,str},{Default Local}, str = LowerCase(str); // get list of jsl files files = Files In Directory(dir); lstScripts = {}; For (i=1,i<=NItems(files),i++, If (Contains(Lowercase(files[i]),".jsl"), InsertInto(lstScripts,files[i]) ) ); lstScriptNames = {}; // search file contents For (i=1,i<=NItems(lstScripts),i++, filePath = dir || lstScripts[i]; strFile = LowerCase( Load Text File(filePath) ); If (Contains(strFile,str), InsertInto(lstScriptNames,lstScripts[i]) ); ); lb << Remove All; lb << Set Items(lstScriptNames); ); Do Open Script = Function({dir},{Default Local}, sel = lb << Get Selected; scriptName = dir || sel[1]; dt = Open(scriptName); ); |
This is a handy tool, especially for anyone who is writing any sort of nontrivial scripts.
Another way to approach this problem that might be useful would be a tool that “expands” the includes — so when you run the tool, a new script window shows up that has the original script except that all the includes have been replaced by the included files. You’d just want to make sure that none of the script(s) get evaluated when you’re putting the overall script into a new script window.
Thanks for your comments Michael. I agree there are times where it would be useful to perform a one-click operation to consolidate all of the code into a single file.