Skip navigation
Currently Being Moderated

Place images with regex references

Jun 27, 2012 11:28 AM

Tags: #javascript #images #grep #extendscript #automation

Hi,

 

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

 

15889801BC.psd

15889801BG.psd

15889801BD.psd

15889801_1.psd

15889801BC1.psd

 

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?

 

Thanks

 

Ben

 
Replies
  • Currently Being Moderated
    Jun 27, 2012 12:01 PM   in reply to BenCloutier

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 27, 2012 1:41 PM   in reply to BenCloutier

    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);
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 27, 2012 1:49 PM   in reply to BenCloutier

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points