Proper Search Syntax for Applescript

Is there a proper search syntax that i can use in a new script application that doesn’t use Sherlock or Finder to search and give me results???

I’m looking for a search syntax to use in my MP3 search engine. I’m trying to make a search engine that searches my Mp3s that are on my computer and on a network.

Would be greatful for the help…

regards

Julz

the *ix command find

A script for this would look something like this:

set searchDirectory to quoted form of posix path of (choose folder)
set searchString to quoted form of "track.mp3"
set foundFiles to paragraphs of (do shell script "find " & searchDirectory & " -name " & searchString)

(not tested though - but you should get the idea :rolleyes: )

thanks guys

But I think I forgot to mention that I’m kinda new to applescript. And the truth is the project I’m working on is actually something to help me learn the language while reading a book by “Ethan Wilde” entitled “Applescrfipt for applications”.

So while your advice is rather excellent, I’m kinda confused on the how the syntax actually works… Nevertheless I’m sure I’ll get what you are trying to say, once I’ve combined your help with the definitions in the book. :wink:

Thanks again,

Julz

Well the find command is a insane beast, but very cool and powerful… you won’t find any reference to it though in an Applescript book, I don’t think so anyways, because it’s a Unix command. It’s syntax as I said can be daunting, but it’s worth it to learn it.

So that said did you have a specifc portion of the script above you needed explaining?

Truthfully yeah…

All of it…:smiley:

Your remark just said something about the Find command

However Vincent gave an example of what the script would look like.

Could you explain your Find syntax using that script as an example???

Sorry if I seem a bit brainless when it comes to this

Actually I’m new to programming on a whole… the last bit of programs I tried to make were small javascripts… no that successful though… :stuck_out_tongue:

Sorry, I’ll try to explain it a bit:

(*You have two types of file paths:
1. Alias (Applescript's Standard Paths)
2. POSIX (Unix' Standard Paths)

Alias looks like "Macintosh HD:Users:vincent:anyfolder:subfolder:index.html"
While the same as POSIX would be "/Users/vincent/anyfolder/subfolder/index.html"

This line will bring up a "Choose folder" dialog and paste the chosen folder into variable searchDirectory
As we are using the Unix command line "file" we need the folder's path as POSIX. Hence we use "POSIX path of (choose folder)" to convert it.
The next problem with command line is that misplaced spaces and single quotes can cause errors.
To prevent these errors we use: "quoted form of ..." to escape all single quotes and spaces. (While single quotes will be attached at both ends of the string)*)
set searchDirectory to quoted form of POSIX path of (choose folder)
--Next we put the search string into searchString
set searchString to quoted form of "index.html"
--And last but not least we combine everything together:
--In this case the resulting command string would look like "find '/Users/vincent/anyfolder/subfolder/' -name 'index.html'"
set theCommand to ("find " & searchDirectory & " -name " & searchString) as text
--Now as we have the command we just have to run it using "(do shell script theCommand)"
--The command will return a text will all found files seperated by a newline character (\n)
set foundFiles to (do shell script theCommand)
--For Applescript it would be better to have the files listed in a list (array).
--As every found item in the returned text has its very own paragraph (because they are delimited by newlines) we can convert them to a list like so:
set foundFiles to paragraphs of foundFiles
--Will return: {"/Users/vincent/anyfolder/subfolder/index.html"}

For a newbie combining Applescript with Unix Shell could be a little heavy if you aren’t yet familiar with the basics.
But Applescript doesn’t have its own search methods. So for a fast & clean solution you’d need Unix anyway. My 2 cents.

Good luck!

Thanks Vincent.

You are right. I’m a newbie to programming on a whole. Apart from the few failed attempt with Javascripting for web design stuff, I guess applescript is my first real programming language, if you can call it that.

Once again thanks…

Julz

ty Vincent! I was trying to write something to explain it, but frankly my explanations usually suck :smiley:

Hey Vincent,

I’m a little stuck on the “theCommand” line…

why do we set it up like "set theCommand (“find” & searchdirectory & “-name” & searchstring) as text "

Kinda need a better understanding of Unix commands I guess… Can you recommend a site that teaches it???

Julz

Hi Vincent.
Just tried your script.
There is a “:” at the end of the alias path, this in turn gets converted into a “/” for the unix path.
the find command will return each result with an extra “/”
Example:
find ‘/Users/UserName/Sites/’ -name ‘index.html’
returns
]/Users/UserName/Sites//index.html
if I do
find ‘/Users/UserName/Sites’ -name 'index.html’l
I get
/Users/UserName/Sites/index.html

so I have added a sed command piped to the end. to look for and replace the // with /

set theCommand to ("find " & searchDirectory & " -name " & searchString & " | sed 's/\\/\\//\\//g'") as text

there maybe a better way of doing this.

**edit **also this works to remove the trailing / before the find command is run. so the sed command is not needed

set searchDirectory to do shell script "dirname " & (quoted form of POSIX path of (choose folder)) & ","

Another variation on your last method mark would be to use this

set searchDirectory to text 1 thru -3 of (quoted form of POSIX path of (choose folder)) & "'"

This gets around an unessecary shell call.

Always good to know more than one way to skin a cat. :slight_smile: Thanks

Needless to say apart from the explanations needed above this all can be done on one line.
I wonder if it can be cut down?

set searchDirectory to paragraphs of (do shell script " find " & (text 1 thru -3 of (quoted form of POSIX path of (choose folder)) & "'") & " -name " & quoted form of "index.html")

I like it Mark. Couln’t resist turning it into a library function for myself:


to searchDirectory(forWhat, inWhere)
	return paragraphs of (do shell script " find " & (text 1 thru -3 of (quoted form of POSIX path of inWhere) & "'") & " -name " & quoted form of forWhat)
end searchDirectory

set tFiles to searchDirectory(text returned of (display dialog "Enter a Search Term" default answer ""), choose folder with prompt "Choose the folder to search")

I like that also.

Still can keep it to one line though, :smiley:

set searchDirectory to paragraphs of (do shell script " find " & (text 1 thru -3 of (quoted form of POSIX path of (choose folder with prompt "Choose the folder to search")) & "'") & " -iname " & quoted form of (text returned of (display dialog "Enter a Search Term" default answer "")))

Just changed it to -iname

Like -name, but the match is case insensitive

Also for anyone new to find (like me)
If you are looking for a file and you only know part of it use * as wild-card searching
example
I want to find all file that are avi file in a folder

*.avi

or I am looking for files that start with the word tom in the name

tom*

with the word tom somewhere in the name
tom

Hey guys,

Thanks for all the input… With all the remarks floating around, I’m sure to get the project going better.

Julz

Hey guys,

probably remember me on the topic of “proper search syntax…”

Anyways… I’m trying to take my script in a new direction…

Instead of searching a single directory. I’m planning to creat an index of all the mp3’s over a network and then use a script to search the index and play the song from there. :stuck_out_tongue:

Any ideas how to do that???