{"id":376,"date":"2011-07-20T15:05:32","date_gmt":"2011-07-20T22:05:32","guid":{"rendered":"http:\/\/10.0.1.201\/?p=376"},"modified":"2011-07-20T15:05:32","modified_gmt":"2011-07-20T22:05:32","slug":"aplpescripted-file-contents-search-using-grep","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2011\/07\/20\/aplpescripted-file-contents-search-using-grep\/","title":{"rendered":"AppleScripted file contents search using grep"},"content":{"rendered":"<p>This is a workaround for the Finder insisting on starting a search of the entire HDD on every new search rather than the good ol&#8217; days of 10.3 which was to search in the directory you were staring at. I know you can click a button to switch it, but every time? No, I like the old way better.<br \/>\n<!--more--><br \/>\nNo per-instance recursive option, but it&#8217;s easy enough to enable it if that is your preferred method.<\/p>\n<p><code>property search_prompt : \"Enter search text.\"<br \/>\nproperty search_string : \"\"<br \/>\nproperty is_recursive : false<\/p>\n<p>tell application \"Finder\"<br \/>\n\ttry<br \/>\n\t\tset this_folder to folder of front window as alias<br \/>\n\t\tif is_recursive is false then<br \/>\n\t\t\tset the_items to name of every item of this_folder whose kind is not folder as text<br \/>\n\t\telse<br \/>\n\t\t\tset the_items to {\"\"}<br \/>\n\t\tend if<br \/>\n\tend try<br \/>\nend tell<\/p>\n<p>repeat<br \/>\n\tset the_search to text returned of (display dialog search_prompt default answer search_string) as text<br \/>\n\tset search_string to the_search<br \/>\n\tset the_results to \"'\" & the_search & \"' found in these files:\" & return & return<br \/>\n\tset is_found to false<br \/>\n\trepeat with i in the_items<br \/>\n\t\tset _grep to \"\"<br \/>\n\t\ttry<br \/>\n\t\t\tset _grep to do shell script \"grep -ilr '\" & the_search & \"' \" & \"'\" & POSIX path of this_folder & i & \"'\"<br \/>\n\t\tend try<br \/>\n\t\tif _grep is not \"\" then<br \/>\n\t\t\tset is_found to true<br \/>\n\t\t\tset the_results to the_results & _grep & return<br \/>\n\t\tend if<br \/>\n\tend repeat<br \/>\n\tif is_found is true then<br \/>\n\t\tset what_now to button returned of (display dialog the_results buttons {\"Retry\", \"OK\"})<br \/>\n\t\tif what_now is \"OK\" then<br \/>\n\t\t\texit repeat<br \/>\n\t\tend if<br \/>\n\telse<br \/>\n\t\tset search_prompt to \"No matches.\"<br \/>\n\tend if<br \/>\nend repeat<br \/>\n<\/code><br \/>\nIf saved as an application you can place it in the Finder&#8217;s toolbar for easy launching.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a workaround for the Finder insisting on starting a search of the entire HDD on every new search rather than the good ol&#8217; days of 10.3 which was to search in the directory you were staring at. I know you can click a button to switch it, but every time? No, I like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,15,28],"class_list":["post-376","post","type-post","status-publish","format-standard","hentry","category-code","tag-applescript-2","tag-fixit","tag-os-x"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=376"}],"version-history":[{"count":0,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/376\/revisions"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}