Grep code works inside indesign but not in script
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Grep code works inside indesign but not in script
- This topic has 5 replies, 3 voices, and was last updated 7 years, 9 months ago by Ari Singer.
-
AuthorPosts
-
-
May 31, 2016 at 6:51 am #85308brad wilsonMember
Hi all,
I’ve been using the following GREP in Indesign’s Find/Change and it works fine, but when I try to use it via a Script it doesn’t do anything.
Find what:
(\s)(\d+\.\d+)(,\s)(\d+\.\d+)(,\s)(\d+\.\d+)(,\s)(\d+\.\d+)(\s)
Change to:
\t$2\t$4\t$6\t$8\tThis is what I’m trying to run as part of the script:
//grep {findWhat:”(\s)(\d+\.\d+)(,\s)(\d+\.\d+)(,\s)(\d+\.\d+)(,\s)(\d+\.\d+)(\s)”} {changeTo:”\t$2\t$4\t$6\t$8\t”} {}
Basically i’m just trying to get rid of the commas and get tabs in the spaces between scores that look like this: teamname 1.2, 2.1, 3.3, 4.5 (98)
I’ve also tried more basic versions just to see if I could change one of the commas to a tab and none of them have worked.
What am I doing wrong?
Cheers,
Brad -
May 31, 2016 at 6:59 am #85309Matt IsaacParticipant
I don’t know much about scripts but I do know that the double forward slash creates a comment. so anything following double forward slashes will not be read by the script. Remove those and try again.
-
May 31, 2016 at 7:03 am #85310brad wilsonMember
Sorry, the comment // wasn’t there when I was trying it out. I can’t edit them out of the original post.
-
May 31, 2016 at 7:12 am #85311Ari SingerMember
Whenever you include a backlash within quotes in a script, the script will get confused. For example, if you write:
findWhat: "\r"
JavaScript will think that you want to insert an actual return in that string (because that’s the way it’s done in JavaScript). So whenever you include a GREP query in a script, any backslashes have to be escaped again. So instead of\r
it has to be\\r
.In other words, for every backslash: one more. (except when you already have two backslashes, such as when you’re searching for an actual backslash, then you have to write three backslashes, not four.
So in your case, that would be like this:
{findWhat:”(\\s)(\\d+\\.\\d+)(,\\s)(\\d+\\.\\d+)(,\\s)(\\d+\\.\\d+)(,\\s)(\\d+\\.\\d+)(\\s)”} {changeTo:”\\t$2\\t$4\\t$6\\t$8\\t”} {}
-
May 31, 2016 at 11:09 am #85313brad wilsonMember
Thanks Ari, that helped a lot.
-
-
June 2, 2016 at 5:09 pm #85416Ari SingerMember
I think I’ve made a mistake when implying that when using an escaped backslash in a script you need three backslashes not four. As it turns out in my recent experience you actually do need four! And three results in an error.
-
-
AuthorPosts
- You must be logged in to reply to this topic.