1

I'm very new to Javascript and trying to create a script that will run an action if it finds a particular set of characters in the open document's name. Right now I've got this which doesn't do anything, but I think it might be on the right track?

var name = activeDocument.name

var mRP = "mrp"
var hits = []

for(var i = 0 ; i < name.length ; i++){
    if(name[i] === mRP[0]){
         for(var j = i ; j < i + mRP.length ; j++){
             if(name.substring(j, j + mRP.length)===mRP)

             hits.push(name.substring(j, j + mRP.length));

        }
    }
if(hits[0] === "mrp"){
app.doAction ('Make Crease', 'MR P')
}

}

I'm looking for the action to run if it finds mrp in the document's title.. usually structured like BC342D_mrp_cu

1 Answer 1

1

You can search through the activeDocument.name string using regular expression and just do your action every time it finds "mrp" substring.

var name = activeDocument.name

var regex = /mrp/gi;

while ( regex.exec(name) )
    app.doAction ('Make Crease', 'MRP');

Here is fiddle: http://jsfiddle.net/g3hnnf3o/

7
  • Hi Zavg, thanks for your reply. Edit: Is this supposed to replace the entire code or go somewhere in it? I've tried running it just on its own and it doesn't work.
    – Constantin
    May 21, 2015 at 18:38
  • Sorry, didn't realise hitting enter submits the comment. The code doesn't seem to work?
    – Constantin
    May 21, 2015 at 18:41
  • Check out the fiddle! I have just added the link
    – zavg
    May 21, 2015 at 18:42
  • Sorry, still a little confused. I've put your code into Adobe's ExtendScript Toolkit to test it and whenever I run it it just comes back with Result: Undefined
    – Constantin
    May 21, 2015 at 18:45
  • I don't know what your app.doAction does, but it seems that the error is within this command.
    – zavg
    May 21, 2015 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.