search your scripts quickly by contents

in the meantime I have a hugh amount of scripts and snippets,
so I often want to search quickly the scripts by contents to find something specific.

Open your script folder in the Finder and define a new search pressing cmd-F with search criteria only by contents (view screenshot)
Save the search window (as a smart folder) and change the value of the property savedSearchName to this name.
I use FastScripts to trigger the script with option-cmd-F

property savedSearchName : "ScriptFolder"

set searchString to text returned of (display dialog "Enter search string" default answer "")
tell application "Finder"
	activate
	open ((path to library folder from user domain as Unicode text) & "Saved Searches:" & savedSearchName & ".savedSearch")
end tell
tell application "System Events"
	tell process "Finder"
		tell group 1 of group 2 of group 1 of splitter group 1 of window savedSearchName
			click button "Edit"
			repeat until exists group 1
				delay 0.5
			end repeat
			set value of text field 1 of group 1 to searchString
		end tell
	end tell
end tell

Edit: the script requires, that spotlight is able to find contents of compiled scripts.
For example Script Debugger includes a spotlight importer for script contents

Hi Stefan,
Thanks for the script…
But it did not work for me?, the click button part, and also the repeat function was incomplete?

Also searching contents will only work if you save your script as applescript text. i.e yourscript.applescript rather than yourscript.scpt

I normally use sportlight, but will see how this holds up.

Cheers

mark

property savedSearchName : "Scripts" & ".savedSearch"
set searchString to text returned of (display dialog "Enter search string" default answer "")
tell application "Finder"
	activate
	open ((path to library folder from user domain as Unicode text) & "Saved Searches:" & savedSearchName)
end tell
tell application "System Events"
	tell process "Finder"
		set The_groups to (group 1 of group 2 of group 1 of splitter group 1 of window savedSearchName)
		click button "Edit" of The_groups
		repeat until exists group 1 of The_groups
			delay 0.5
		end repeat
		set value of text field 1 of group 1 of The_groups to searchString
	end tell
end tell

**edit, if you want to know how to setup spotlight to search just let me know…

Hi Mark,

I also use spotlight normally, but I want search quickly in one specific folder only by typing in the search string
and have the results in a Finder window

I’d like to know that :slight_smile:

When I say I search using Spotlight, I mean I have added the applescript text type file to the spotlight importer so it will index the content.
Otherwise AFAIK it will not look at the files. I then use the normal Spotlight up in the Top right hand corner.

Is this what you already do?? Or are you only using it to search file names??

Thanks, I’ve read your article at macosxhints but I haven’t done it yet. :slight_smile:
My main intention having written the script above is to search only in one specified folder (including subfolders) and only by contents.
If I use spotlight normally I get serch results from everywhere, and I don’t want to make any additional clicks or type kind:xxx things

PS: I know now, why searching scripts by contents works flawlessly on my machine. I use Script Debugger,
which has included a spotlight importer for script contents

Ah the old ASLT files.
I do not use that anymore. I just put “com.apple.applescript.text” in the rich text importer. and Save the scripts as Applescript text.
I did not really want to pay for the script debugger just to get the .scpt importer :slight_smile:

How does this work for you.
It looks in one place and at content ( there is probably a way to not include filenames)
Then it opens the results folder to show alias files to the scripts it found. I did it this way so you only got shown the results.
It deletes the previous aliases on the next search

property SearchPath : "/Users/UserName/ScriptFolder/"
property ResultWindow : "Macintosh HD:Users:UserName:Script_Results"
set searchString to text returned of (display dialog "Enter search string" default answer "")
set biglist to {}
set searchresults to do shell script "mdfind -onlyin " & quoted form of SearchPath & space & searchString


if searchresults is not "" then
	tell application "Finder"
		delete items of folder ResultWindow
		open ResultWindow
	end tell
	repeat with i from 1 to number of paragraphs in searchresults
		set this_item to paragraph i of searchresults
		set this_item to POSIX file this_item
		
		tell application "Finder"
			activate
			set theAlias to make new alias file at ResultWindow to this_item
			
		end tell
	end repeat
	
end if

this is pretty clever, Mark :slight_smile:

cheers :smiley:

I have just been experimenting.

It seems if I added a * (astrix) to the end of the search it only searches content??

Change below edit also add to it so the dialog always pops up in the front app

property SearchPath : "/Users/UserName/ScriptFolder/"
property ResultWindow : "Macintosh HD:Users:UserName:Script_Results"
tell application "System Events"
	set target_app to item 1 of (get name of processes whose frontmost is true)
end tell
tell application target_app
	set searchString to text returned of (display dialog "Enter search string" default answer "")
end tell
set searchresults to do shell script "mdfind -onlyin " & quoted form of SearchPath & space & searchString & "*"

if searchresults is not "" then
	tell application "Finder"
		delete items of folder ResultWindow
		open ResultWindow
	end tell
	repeat with i from 1 to number of paragraphs in searchresults
		set this_item to paragraph i of searchresults
		set this_item to POSIX file this_item
		
		tell application "Finder"
			activate
			set theAlias to make new alias file at ResultWindow to this_item
			
		end tell
	end repeat
end if

I have both Script Debugger 4 and FastScripts, but I don’t understand this paragraph:

Open your script folder in the Finder and define a new search pressing cmd-F with search criteria only by contents
Save the search window (as a smart folder) and change the value of the property savedSearchName to this name.
I use FastScripts to trigger the script with option-cmd-F

because I don’t know how to save the resulting search window as a smart folder.

We say in german, an picture tells more than 1000 words

http://homepage.mac.com/stklieme/.Pictures/jpg/SmartSearch1.png
http://homepage.mac.com/stklieme/.Pictures/jpg/SmartSearch2.png
http://homepage.mac.com/stklieme/.Pictures/jpg/SmartSearch3.png
http://homepage.mac.com/stklieme/.Pictures/jpg/SmartSearch4.png
http://homepage.mac.com/stklieme/.Pictures/jpg/SmartSearch5.png

Thank you Stefan - the equivalent English expression is “a picture is worth a thousand words”. Googling around for that, I find “The first time a form of the phrase appears in print is in James Kirke Paulding’s New Mirror for Travellers, 1828: "A look, which said as plainly as a thousand words.

Having said that, the script runs down to ‘click button “Edit”’ and hangs with “System Events got an error: NSReceiverEvaluationScriptError: 4”. A delay doesn’t help. I’ll fiddle with it – I have UI Browser, so having opened the window, I should be able to find the damned button.

Very strange,
I’m running the system in english, and I use the script daily,
it works perfectly

Hi Adam, Stefan,

if you look at post #2 you will see I had the same problem with the script from post #1,

Which I showed a fix in post #2.

It is odd though that Stefan can get his script to work and we can not???

Yours works a treat, Mark. Thanks.

This seems to have stopped working in Leopard 10.5.1 even though I’ve gone through the process of setting it up again. Any clues? I think that the reason is that Leopard does not search the ~/Library (I know this has been a problem for other apps that kept data in Application Support). Anyone found a workaround?

Hi Adam,

I’ve rewritten my original script completely, because Apple has changed the whole structure of the smart folders in Leopard.

property savedSearchName : "ScriptFolder"

set searchString to text returned of (display dialog "Enter search string" default answer "")
activate application "Finder"
tell application "Finder" to (exists window savedSearchName)
if result is false then
	openScriptFolder()
	tell application "System Events"
		tell process "Finder"
			click menu button 1 of group 2 of tool bar 1 of window "ScriptFolder"
			delay 0.5
			key code {125, 125, 125, 49}
			delay 0.5
			repeat until exists text field 4 of group 1 of group 1 of splitter group 1 of window "ScriptFolder"
				delay 0.5
			end repeat
		end tell
	end tell
end if
tell application "System Events"
	tell process "Finder"
		tell text field 4 of group 1 of group 1 of splitter group 1 of window "ScriptFolder"
			set value of attribute "AXFocused" to true
		end tell
		keystroke searchString
	end tell
end tell

on openScriptFolder()
	tell application "Finder"
		open ((path to library folder from user domain as Unicode text) & "Saved Searches:" & savedSearchName & ".savedSearch")
		repeat until exists window "ScriptFolder"
			delay 0.5
		end repeat
		tell window "ScriptFolder"
			set current view to list view
			set bounds to {585, 198, 1370, 964}
		end tell
	end tell
end openScriptFolder

Ahh; Thank you Stefan – I’ll set up another folder and give it a go – I miss it.

PS: the script will probably not work, if you don’t have the same amount of search conditions

I use these:
Kind is Other script
Any of the following are true
File extension is scpt
File extension is app
Items matching text is

True, Stefan, it didn’t work.

Not going to, either, for two reasons: I don’t know how to specify “Any”, and I don’t know how to set “Items matching text is ” which is not a choice in “Other”.

PPS: this is (I hope) a universal version independent of the number of search criteria


property savedSearchName : "ScriptFolder"

set searchString to text returned of (display dialog "Enter search string" default answer "")
activate application "Finder"
tell application "Finder" to (exists window savedSearchName)
if result is false then
	openScriptFolder()
	tell application "System Events"
		tell process "Finder"
			click menu button 1 of group 2 of tool bar 1 of window savedSearchName
			delay 0.5
			key code {125, 125, 125, 49}
			delay 0.5
			repeat until exists group 1 of group 1 of splitter group 1 of window savedSearchName
				delay 0.5
			end repeat
		end tell
	end tell
end if
tell application "System Events"
	tell process "Finder"
		tell last text field of group 1 of group 1 of splitter group 1 of window savedSearchName
			set value of attribute "AXFocused" to true
		end tell
		keystroke searchString
	end tell
end tell

on openScriptFolder()
	tell application "Finder"
		open ((path to library folder from user domain as text) & "Saved Searches:" & savedSearchName & ".savedSearch")
		repeat until exists window savedSearchName
			delay 0.5
		end repeat
		tell window savedSearchName
			set current view to list view
			set bounds to {585, 198, 1370, 964}
		end tell
	end tell
end openScriptFolder