5 Replies Latest reply: Jun 28, 2012 10:11 AM by BenCloutier RSS

    Place images with regex references

    BenCloutier Community Member



      lets say i have several images that has the same 8 first digits but differnt endings.








      I tryed something like...


      tmp = "/15889801.*\.[a-z]{3}/g";

      myRectangle.place(File("/Volumes/Photos/" +tmp));


      but obviously it does not work. Is it doable or should i make a loop that check for all the possible endings?





        • 1. Re: Place images with regex references
          [Jongware] MVP

          You can't supply a RegExp to a file open command. But you *can* throw it into a getFiles command, and tou get a nice array of qualifying files in return. Then loop over that to place the files one at a time.

          • 2. Re: Place images with regex references
            BenCloutier Community Member

            Thanks Jongware. I looked at your post about the getFile. Will it work with images (eps, psd, etc)? I will definately look into it.

            • 3. Re: Place images with regex references
              [Jongware] MVP

              It's a bit simpler than I outlined in that post. I must have learned a few new tricks since then, I guess


              You can feed .getFiles a regular wildcard expression, but you can also feed it a function. The function gets called for each object (which may be a file or a folder) with the file object as it argument, and it must return 'true' or 'false', where 'true' will include it into the result. (Underlining because 'the file object' returns by default the entire path, and you obviously only want to test the 'file name' part of it.)


              Your list of sample files only shows PSDs but I understand you also want to grab other file types. Theoretically, you could use the regular wildcard expression "15889801*.*", but with a function that uses GREP you have more control:


              result = Folder("/Volumes/Photos").getFiles ( function (anyItem) {
                                  return anyItem instanceof File && anyItem.name.match(/^15889801.*\.[a-z]{3}$/i);
              alert (result.length+' files:\r'+result.join('\r'));


              Note that I added '^' (start of string) at the start and used 'i' in the RegExp flags. I don't think you need 'g' (Global) here -- first, it gets defeated by my '^', but second, this would match any occurrence of the digits string anywhere inside any file name. Then it would also match files where this digit string does not occur at the very start of the file name. And the 'i' is so it matches not just ".psd" but also ".PSD".


              The '$' at the very end assures only and exactly 3-letter extensions are returned. Your version would return not only "15889801.abc" but also "15889801.abcde" and even "15880901.psd.do_not_use.old_backup.indd".


              If you find the function returns too many files, amongst which a couple of extensions you do not want, you can always make an exhaustive list of the extensions you do want:


              .. anyItem.name.match(/^15889801.*\.(psd|eps|etc)$/i);
              • 4. Re: Place images with regex references
                [Jongware] MVP

                By the way. Assuming the above works for you:


                benc_academie wrote:

                tmp = "/15889801.*\.[a-z]{3}/g";

                myRectangle.place(File("/Volumes/Photos/" +tmp));


                .. be warned that the return result of ".place" is an array. Even though it usually only contains a single element, you still have to address it as "tmp[0]" if you are going to move it around right after your .place command.

                • 5. Re: Place images with regex references
                  BenCloutier Community Member

                  Thanks, it work well.


                  All i need to do now is to compare "result" with each element in myList, an array created with the results of a grep search. I guess my "match regex" could be a variable?


                  i would put my function but i don't know how to put codes without it getting parsed.