Search within structure

pho's picture

Hi,

I'm using the Directory Scanner patch and now I need to filter the results of the structure by the filename.

For instance, if I have a folder with two files named "apple.jpg" and "apple tiger.jpg" and I search for tiger, I'll get a structure containing only the "apple tiger.jpg" file.

Any suggestions.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

mattgolsen's picture
Re: Search within structure

Your best bet would be to use the Javascript patch IMO. Do you have any experience with it? If not I think I have a composition that could help. Swiftlikeninja and I have actually discussed releasing a "power pack" of commonly needed/used Javascript stuff.

pho's picture
Re: Search within structure

Well, unfortunately I don't have any experience with Javascript. I know someone who can help me with it, but if you could show me that composition I'd appreciate it :-)

usefuldesign.au's picture
Re: Search within structure

Hey I have a bunch of things in JS I wrote as demo for proof of concept things for my own use. I do sort of manual unit testing on functions/blocks of code in a new Composition to keep things clean and (somewhat) understandable.

We could either do it as part of wiki here or someone could make the website I've been planning for QC && JS code snippets for last 2 years ;-)

usefuldesign.au's picture
Re: Search within structure

Here is a JS patch to find the path of a specific file name in the list and another to search for a token and produce a list of files including the token in filename. It searches directory the comp is in so remember to save your new comp when using this patch or there's no reference directory for the Scanner patch.

Uncomment the Log statements to learn better how it works. I often add comments to debug, sometimes it's something outside the patch causing grief though, or inside the patch like assigning a structure to a string output (that wont recast automagically ;-) ) You need console app or QC Console visible (hidden prefs) to see Log statement output.

JS can go weird at times, I believe, and a restart of QC is prob best solution. While I was writing this search JS patch for you as mod of the "Find that file (JS)" patch, I was trying to pass value of variable called index to the output Count and it stubbornly refused to pass. Log told me index = 2 but it wouldn't output. I change variable name and type (index/number) several times but nothing. So then I made a new variable result._index and incremented it directly. At the moment I got that working Count started outputting truly. Go figure!

gtoledo3's picture
Re: Search within structure

This may work for your scenario.

You'll see a folder with a few images.

Enter your relative or absolute path into Directory Location.

Enter your search file name into "Search For This File Name".

If your directory changes in the middle of runtime, you'll want to Scan Signal again, so the new struct gets loaded.

PreviewAttachmentSize
MEDIA.zip43.74 KB

pho's picture
Re: Search within structure

Thank you! This is exactly what I was looking for!

I spent a long time trying to find a plug-in that could do this. I actually found the Spotlight Search plug-in, which permits search with UTI string, but doesn't allow to filter by directory.

I'm only afraid of the possibility of system hangs with larger file structures now.

I'll be trying it out :-)

gtoledo3's picture
Re: Search within structure

pho wrote:
Thank you! This is exactly what I was looking for!

I spent a long time trying to find a plug-in that could do this. I actually found the Spotlight Search plug-in, which permits search with UTI string, but doesn't allow to filter by directory.

I'm only afraid of the possibility of system hangs with larger file structures now.

I'll be trying it out :-)

I was thinking about this, and it wouldn't take that long to make a plugin that did this, and it would be easier (for me at least) than using JS. I actually thought about doing that quickly last night, when I read this, but I was already sorta tired.

There's something weird to me about searching through a struct with JS and then matching for type (like, is it a string, and does that string equal what's written in an input port), but it's very easy in a plugin (imo). This is probably more, for me, because I avoid javascript, and I'm not 100% sure how to go "if val is a string, let only that pass, AND if it = ___" in JS.

(@ JS-y types - Can we interrogate for value types in QC JS, when they're already in a struct (like, only let virtuals through, or only images?) (sorry@ AL if the stuff he posted does that... I'll check).)

When I've done stuff like this with JS, I wind up using an approach where I present the JS with the entire incoming structure, but run through it only ONE index at a time, within the JS logic - valid output then gets looped back around, or maybe gathered in a queue or something else. Once the whole structure has been combed through, (might take a few seconds), your "passing" structure will have been built, and it will have happened in a very non-taxing way.

I'd suggest to find the Apple developer example qtz called "Image TV" qtz., and check out that approach. It's very similar to what I'm describing, and while it will have a kind of "load" time to the approach, it will also tend to give no kind of system hangs.

What I uploaded could also be adjusted to not unleash the whole structure to be analyzed at once, but to go through one index at a time, and gather result. It would only require mild tweaking (haven't looked/thought about it too hard yet though... coffee time!)

usefuldesign.au's picture
Re: Search within structure

I don't see why variable type comes into it. The Directory Scanner patch outputs a structure of strings, no need to check on that.

To answer your question, QC types in JS are difficult to test for. I've posted comps on other threads showing ways and means, but it's not foolproof like inside hard-typed languages.

The triple equals operator "===" tests for equality and equivalence of type. Javascript type that is. The auto-casting (of type) in JS means types are "soft" so a line like  if ("5" == 5) returns a 'truthie' result for the if statement.  if ("5" === 5 ) returns a false to the if statement. JS can pass virtual and images from one structure to another but as far as I'm aware it can not detect them as types since JS has no knowledge of them.

Randomly selected blog post from google: http://geekswithblogs.net/brians/archive/2010/07/03/quality-equality-wit...

Douglas Crockford is the man for all you JS questions, he has posted 3 lectures and a few talks on YouTube. His website is javascript.crockford.com

gtoledo3's picture
Re: Search within structure

Variable type comes into play, because one could, in QC, attach any structure to any structure port, on purpose, or by accident.

If there's a structure that has different data types present, eliminating non-possibilites when doing a match, may result in better performance. There's no real need to match a string against an index that has anything besides a string value. It seems like if the structure is processed as a list, and not an array, with all non-possibilities thrown out as a first step, it would be faster. It may be faster, even as an array, to throw out all non-possibilites as a first step, without going through each index for the match, especially depending on how long the search terms/file paths are.

I'd rather not make hypothesis, and just actually test. Since you asked though, that was my thought.

usefuldesign.au's picture
Re: Search within structure

In more generalised cases there's the typeof statement but again applies exclusively to JS types, not QC types. So one has to be clever the way one makes the structure in first place, I guess, putting a hint in the key name (image_43: ) or an extra element/item in array/object providing types.

52 Member(s):
   0: (0) =  <Structure>
      0: "name" = something to search through
      1: "image" = <image>
      2: "date" = 17/11/11
      3: "type" = jpeg_image
   1: (1) = <Structure>
      0: "name" = something different to search through
      1: "image" = <image>
      2: "date" = 16/11/11
      3: "type" = pdf_image
<\code>
<p \>
 
For that matter how does one test for QC types in QC itself, short of writing a plugin?
 
This was a question I asked a few years ago and cwright pointed me to teh [geshifilter-code]typeof

statement and also another way to see if a variable/object has items/elements as opposed to a number or string. I can't recall having a QC way to get type at the time, except for literal tests. ie. if a *1 != 0 then 'a' is probably a number; unless a == '' or zero since QC also has soft typing so that messes you around a bit. <br><br><small> Man there's something screwed with the markdown going on here. The more I try to bring the lines after the [/geshifilter-code] tag the more lines get shown inside the code block. Must be all the derogatory comments I've passed John Gruber's way for being a teachers-pet-wannabe over the years.

pho's picture
Re: Search within structure

I tried to use your compositions but I found some problems.

George, I didn't get your composition to work in any folder besides the one you included.

Usefuldesign, I've used your JS patch for partial token, but it always returns results, even if the input parameter is emply. Also, it is case sensitive, but that's not very important.

gtoledo3's picture
Re: Search within structure

Just change the folder file path that the directory scanner is searching.

pho's picture
Re: Search within structure

Surely enough, I did that. I also checked the Scan signal box. No path got to the image loader.

usefuldesign.au's picture
Re: Search within structure

What problems?

Null token will pass all the files because all the filesnames have "" inside them. That's not a bug so much as an edge condition you need to decide about if it effects you.

You could change a single line of the JS patch testing for this condition and skipping out of the true condition:

ie if (filename.indexOf(Search_Token) != -1) becomes

if (Search_Token !="" && filename.indexOf(Search_Token) != -1) if it effects you. Or you could just have the JS patch not do anything if Search_Token == "". Or you could just not use "" as a search token ;-)

Yes .indexof is case sensitive. If that's an issue for you you may wish to read through the String methods on Cybero's QC Javascript documentation for one that is not case sensitive. From memory, the Search and the Match methods use regular expressions which are powerful but dammed tricky to use for novice coder (very hard to read them and anticipate what they will do even for those who use them all the time).

gtoledo3's picture
Re: Search within structure

I made a rookie mistake. I forgot that Directory Scanner adds all of this %20 blah to stuff, when you're pulling from different locales.

Does this work? I'm loading stuff from the Desktop picture library, using SL, and it seems fine.

PreviewAttachmentSize
Search 2.qtz7 KB

pho's picture
Re: Search within structure

Sorry I had to bother you again. It would have been really easy to figure that out if I had taken the time.

I added partial token functionality to the composition.

Thanks

PreviewAttachmentSize
Search 3_0.qtz8.55 KB

gtoledo3's picture
Re: Search within structure

No problem... And come to think of it, that parsing out the %20 stuff w/ those string replaces is something I originally picked up from an early alpha of openEmu (and that I wound up slapping in a "file cycler" macro that I made ages ago, back in leopard. One couldn't do the rest this way, bc you couldnt publish output of an iterator. So shout out to whatever that demo was. The little match thing in the iterator took a couple of minutes at best, no biggie :-)

I'm not blaming any of the rest of the programming on them (eg. OpenEmu... I guess vade or dan winckler ) though, in case I did something regrettable. I haven't tested it much, so definitely give it a work over before serious use :-)