Indexing and Search

Is there a way to have a script index all files on your hardrive of a certain type (*.mp3) and save that index in a specified location???

Then have a script just search the index for a particular file???

Julz

do shell script "find / -name \"*.mp3\" >> PATHTOLOG"

Will create a file with entries like this in it

/Users/julz/Documents/Music/audioTrack.mp3

Once you have that text file how you want to search it is an other story, this would search for a user sepcified string and display the matching track’s path

set MP3file to (choose file) as Unicode text
display dialog "Search MP3 List for:" default answer ""
set searchString to text returned of result
try
	set MP3path to do shell script "grep " & quoted form of searchString & " " & quoted form of POSIX path of MP3file
	display dialog "Track found at: " & MP3file
on error
	display dialog "No matching track found."
end try

Hi,

the fastest way is using spotlight, it creates a file myMP3.txt on desktop including all (POSIX) paths

do shell script "mdfind -onlyin / 'kMDItemFSName  = \"*.mp3\"' >> ~/Desktop/myMP3.txt"

And if you have Play Sound installed, you can listen to a random selection from your Music folder this way:

[applescriptset Random_MP3 to (POSIX file (some item of paragraphs of (do shell script “mdfind -onlyin ~/music ‘kMDItemFSName = "*.mp3"’”)) as alias)
tell application “Play Sound” to play Random_MP3

Faster yes, but prone to a few problems (unless I’m mistaken)

Spotlight will only search within your home folder, the top level of other users folders (but not say in their music folder) and these locations:

/Library/PreferencePanes/
/System/Library/PreferencePanes/
/Applications

Additionally spotlight will not search/index(by default) network-attached or shared volumes as well as files that are:

Hidden those whose names begin with a period (.).
Invisible files.
Files within hidden or invisible folders.
System-related files or folders.

Or of course pre 10.4

Kind of fun even with those limitations, though, James:

If you have Play Sound installed (it’s free) then:


set Random_Pick to (POSIX file (some item of paragraphs of (do shell script "mdfind -onlyin ~/Music 'kMDItemContentType  = \"public.mp3\"'")) as alias)
tell application "Play Sound" to play Random_Pick

Edited for different attribute so it isn’t dependent on the name extension

Oh I couldn’t agree more! Spotlight is way faster than a brute ‘find’!

Just want the OP to know in case it wouldn’t work for his scenario :smiley:

Thanks guys for all your help.

However how do I get it to post the result in filename other than just the path.

Do I have to get the indexing script to store data in a different way??? :confused:

Oh and Whos OP??? :expressionless:

Julz

Well OP is Original Poster :smiley:

As for removing the path you could either do that when writing the index file or when returning the results of search. Which would you prefer? I would think you would want to keep the path in the index so you know where all your music is actually residing… Unless of course all your songs are in a music folder =)

OP… I like that.

Well when I search trhough the index… I would like the results to post the file name or the filename and the path in two columns.

Is that possible???

Give this a try whilst I go hunt down coffee… hmm coffee, the brain is sludge without it ! lol

set MP3file to (choose file) as Unicode text
display dialog "Search MP3 List for:" default answer ""
set searchString to text returned of result
set AppleScript's text item delimiters to {""}

try
	set MP3path to do shell script "grep " & quoted form of searchString & " " & quoted form of POSIX path of MP3file
	set AppleScript's text item delimiters to {"/"}
	set TrackName to last text item of MP3path
	set AppleScript's text item delimiters to {""}
	set TrackPath to text 1 thru -((count of text items of TrackName) + 2) of MP3path
	display dialog TrackName & return & return & TrackPath
on error
	display dialog "No matching track found."
end try

Okay I’m timing you guys now (Adam, Stefan, Kel, etc…). How long will it take one of you to post an optimized/different faster version :smiley:

The only comment I’d make, James, is that you can hasten searches for files by specifying their types so the chooser won’t be offered options that won’t work:

set MP3file to (choose file of type "public.mp3") as Unicode text

And, BTW; you can discover types like zo…

set tFile to choose file without invisibles
set tType to (do shell script "mdls -name kMDItemContentType " & quoted form of POSIX path of tFile) as text
set O to offset of "=" in tType
set tType to text (O + 1) thru -1 of tType

thanks guys for the help.

set MP3file to (choose file) as Unicode text

I would like to use a specific path instead of (choose file)… With the current setting, I have to scroll and choose the index manually.

Suppose I combine both script together. Have the script index music and then start up the search as soon as I open it.

I’ll place an update once I have it figured out…:stuck_out_tongue:

Julz

to set it static do something like this

set MP3file to "/Users/julz/Desktop/thefile.txt"

Thanks James,

I guess I’ve yet to learn how to set static paths in applescript. :stuck_out_tongue:

Julz