I am trying to use the new text search function to help me look through a directory of files for string that I chose. Essentially I've got a large list of notebooks and don't want to manually look through all of them. By typing in a key word I want it to give me a short list of file names containing that key word and then I will select one and the file will open. It almost works.

I cannot get the b[[i]] to cycle through all the i's the table function. It has something to do with the hold rest attribute for rule delayed symbol in the last line of code. Can someone help me?

You can try it by putting in your own file directory where you see mine.

 d = InputString[];

si = CreateSearchIndex[
   "/Users/michaelmccain/Library/Mobile \
Documents/com~apple~CloudDocs/Ventura College/Mathematica/How To/"];

doc = TextSearch[si, d];

b = Table[doc[[i]]["Location"], {i, 1, Length[doc]}];
a = Table[doc[[i]]["FileName"], {i, 1, Length[doc]}];

ActionMenu["Search Results", 
 Table[a[[i]] :> SystemOpen[b[[1]]], {i, 1, Length[doc]}]]

You have correctly identified the problem, namely that the second argument of RuleDelayed (:>) has the Hold attribute so that if i is used for the second argument, it will not be replaced by the value dictated by the iterator. There is a trick that can be used to insert values into held expressions, that looks like this (assume i has been defined outside of this expression):

With[{i = i}, heldExpr[i]]

In your code, this will do:

 "Search Results",
  With[{i = i}, a[[i]] :> SystemOpen[b[[i]]]],
  {i, 1, Length[doc]}

However, to iterate through lists using Table and Part ([[ ]]) is not good practice. A more typical way of writing this code is

ActionMenu["Search results", MapThread[# :> SystemOpen[#2] &, {a, b}]]
  • 1
    $\begingroup$ You're awesome. Thank you. Still learning... : ) $\endgroup$ – Michael McCain Sep 26 '15 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.