ImageJ base code to get access to all the classes and their methods to test new Plugins. https://imagejdocu.tudor.lu/howto/plugins/the_imagej_eclipse_howto
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.1 KiB

3 years ago
  1. // "Search"
  2. // This macro searches for text in files contained in a directory.
  3. // TF, 2011.02 Added support for scripts; Recordable.
  4. str = "";
  5. contents = true;
  6. ignore = false;
  7. search = "Macros";
  8. firstLine = true;
  9. arg = getArgument;
  10. if (arg!="") {
  11. args = split(arg, "|");
  12. if (args.length==4) {
  13. str = args[0];
  14. contents = parseInt(args[1]);
  15. ignore = parseInt(args[2]);
  16. search = args[3];
  17. }
  18. }
  19. extensions = newArray(".java", ".txt", ".ijm", ".js", ".py", ".rb", ".clj", ".bsh", ".html");
  20. IJdir = getDirectory("imagej");
  21. Dialog.create("Search");
  22. Dialog.addString("_", str, 20);
  23. items = newArray("Macros", "Scripts", "Java", "ImageJ folder", "Choose...");
  24. Dialog.setInsets(2,20,0);
  25. Dialog.addRadioButtonGroup("Search:", items, 5, 1, search);
  26. Dialog.setInsets(0, 20, 0);
  27. Dialog.addCheckbox("Search_contents", contents);
  28. Dialog.addCheckbox("Ignore case", ignore);
  29. Dialog.setInsets(10, 0, 0);
  30. Dialog.addMessage("In the Log window, to open a file,\ndouble-click on its file path.");
  31. Dialog.show();
  32. str = Dialog.getString();
  33. contents = Dialog.getCheckbox();
  34. ignore = Dialog.getCheckbox();
  35. search = Dialog.getRadioButton();
  36. if (str=="")
  37. exit("Search string is empty");
  38. sourceExists = File.exists(IJdir+"source");
  39. searchNames = false;
  40. dir1=""; dir2=""; dir3="";
  41. if (search=="Scripts") {
  42. dir1 = getDirectory("macros");
  43. dir2 = getDirectory("plugins");
  44. dir3 = IJdir+"scripts/";
  45. extensions = newArray(".js", ".py", ".rb", ".clj", ".bsh");
  46. } else if (search=="Java") {
  47. dir1 = getDirectory("plugins");
  48. if (sourceExists)
  49. dir2 = IJdir+"source"+"/";
  50. extensions = newArray(".java");
  51. } else if (search=="ImageJ folder") {
  52. dir1 = getDirectory("imagej");
  53. searchNames = true;
  54. } else if (search=="Choose...") {
  55. dir1 = getDirectory("Choose a Directory");
  56. searchNames = true;
  57. } else {
  58. dir1 = getDirectory("macros");
  59. dir2 = getDirectory("plugins");
  60. extensions = newArray(".txt", ".ijm");
  61. }
  62. if (ignore)
  63. str = toLowerCase(str);
  64. count = 0;
  65. if (dir1!="") find(dir1);
  66. if (dir2!="") find(dir2);
  67. if (dir3!="") find(dir3);
  68. if (indexOf(str, "|")==-1)
  69. return ""+str+"|"+contents+"|"+ignore+"|"+search;
  70. exit;
  71. function find(dir) {
  72. list = getFileList(dir);
  73. for (i=0; i<list.length; i++) {
  74. showProgress(i, list.length);
  75. if (endsWith(list[i], "/"))
  76. find(""+dir+list[i]);
  77. else if (contents && valid(list[i])) {
  78. s = File.openAsString(dir+list[i]);
  79. s2 = s;
  80. if (ignore)
  81. s2 = toLowerCase(s);
  82. if (indexOf(s2,str)!=-1) {
  83. count++;
  84. if (firstLine)
  85. showMessageInHeader();
  86. print("");
  87. print(dir+list[i]);
  88. lines = split(s, "\n");
  89. n = 0;
  90. for (j=0; j<lines.length; j++) {
  91. line = lines[j];
  92. line2 = line;
  93. if (ignore) line2 = toLowerCase(line);
  94. if (indexOf(line2,str)!=-1 && n<8) {
  95. print((j+1)+": "+line);
  96. n++;
  97. }
  98. } // for
  99. } else
  100. searchName(list[i]);
  101. } else if (searchNames || valid(list[i]))
  102. searchName(list[i]);
  103. }
  104. if (count==1)
  105. showStatus("1 match");
  106. else
  107. showStatus(count+" matches");
  108. }
  109. function searchName(name) {
  110. name2 = name;
  111. if (ignore)
  112. name2 = toLowerCase(name2);
  113. if (indexOf(name2,str)!=-1) {
  114. if (firstLine)
  115. showMessageInHeader();
  116. print("");
  117. print(dir+name);
  118. count++;
  119. }
  120. }
  121. function valid(name) {
  122. for (i=0; i<extensions.length; i++) {
  123. if (endsWith(name, extensions[i]))
  124. return true;
  125. }
  126. return false;
  127. }
  128. function showMessageInHeader() {
  129. print("\\Heading: Double-click on a file name to open it");
  130. firstLine = false;
  131. }