This script shows a dialog that allows for up to 10 search and replace fields to be done at one time.
To install this script, navigate to the Adobe InDesign Application Directory and paste the script file into the following folder ‘Scripts -> Scripts Panel’. The script can then be run from the Scripts panel; this is found in ‘Window -> Utilities -> Scripts’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | /* Multple Search and Replace Fields Script 65bit Software Ltd*/ var myEC = app.easycatalogObject; var myDS = chooseDataSource(); if(myDS != false) { var myDV = myDS.dataviews.add(); var pleaseSelectArray = ['Please Select']; var myRecord = myDV.records.item(1); var everyItemArray = myRecord.fields.everyItem().name; var myFieldArray = pleaseSelectArray.concat(everyItemArray); var NULL_SIZE = [0,0]; var MAX_SIZE = [639,44]; var NULL_SPACING = 0; var MAX_SPACING = 8; var NULL_MARGINS = [15,0,15,0]; var MAX_MARGINS = [15,10,15,10]; var optionCount = 0; var optionArray = []; var searchDropdownArray = []; var replaceDropdownArray = []; var searchArray = []; var replaceArray = []; var u, w = new Window("dialog", "Search and Replace Fields"); w.orientation = "column"; var option1 = w.add("group") { option1.orientation = "row"; option1.alignment = ['fill','']; option1.add("statictext", u, "Search:"); var searchList1 = option1.add("dropdownlist",[0,0,240,20], myFieldArray); searchList1.minimumSize.width = 220; searchList1.selection = 0; option1.add("statictext", u, "Replace:"); var replaceList1 = option1.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList1.minimumSize.width = 220; replaceList1.selection = 0; } option1.margins = MAX_MARGINS; option1.spacing = MAX_SPACING; searchDropdownArray.push(searchList1); replaceDropdownArray.push(replaceList1); optionArray.push(option1); w.spacing = NULL_SPACING; var option2 = w.add("group") { option2.orientation = "row"; option2.alignment = ['fill','']; option2.add("statictext", u, "Search:"); var searchList2 = option2.add("dropdownlist",[0,0,240,20], myFieldArray); searchList2.minimumSize.width = 220; searchList2.selection = 0; option2.add("statictext", u, "Replace:"); var replaceList2 = option2.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList2.minimumSize.width = 220; replaceList2.selection = 0; } option2.visible = false; option2.maximumSize = NULL_SIZE; option2.spacing = NULL_SPACING; option2.margins = NULL_MARGINS; searchDropdownArray.push(searchList2); replaceDropdownArray.push(replaceList2); optionArray.push(option2); var option3 = w.add("group") { option3.orientation = "row"; option3.alignment = ['fill','']; option3.add("statictext", u, "Search:"); var searchList3 = option3.add("dropdownlist",[0,0,240,20], myFieldArray); searchList3.minimumSize.width = 220; searchList3.selection = 0; option3.add("statictext", u, "Replace:"); var replaceList3 = option3.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList3.minimumSize.width = 220; replaceList3.selection = 0; } option3.visible = false; option3.maximumSize = NULL_SIZE; option3.spacing = NULL_SPACING; option3.margins = NULL_MARGINS; searchDropdownArray.push(searchList3); replaceDropdownArray.push(replaceList3); optionArray.push(option3); var option4 = w.add("group") { option4.orientation = "row"; option4.alignment = ['fill','']; option4.add("statictext", u, "Search:"); var searchList4 = option4.add("dropdownlist",[0,0,240,20], myFieldArray); searchList4.minimumSize.width = 220; searchList4.selection = 0; option4.add("statictext", u, "Replace:"); var replaceList4 = option4.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList4.minimumSize.width = 220; replaceList4.selection = 0; } option4.visible = false; option4.maximumSize = NULL_SIZE; option4.spacing = NULL_SPACING; option4.margins = NULL_MARGINS; searchDropdownArray.push(searchList4); replaceDropdownArray.push(replaceList4); optionArray.push(option4); var option5 = w.add("group") { option5.orientation = "row"; option5.alignment = ['fill','']; option5.add("statictext", u, "Search:"); var searchList5 = option5.add("dropdownlist",[0,0,240,20], myFieldArray); searchList5.minimumSize.width = 220; searchList5.selection = 0; option5.add("statictext", u, "Replace:"); var replaceList5 = option5.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList5.minimumSize.width = 220; replaceList5.selection = 0; } option5.visible = false; option5.maximumSize = NULL_SIZE; option5.spacing = NULL_SPACING; option5.margins = NULL_MARGINS; searchDropdownArray.push(searchList5); replaceDropdownArray.push(replaceList5); optionArray.push(option5); var option6 = w.add("group") { option6.orientation = "row"; option6.alignment = ['fill','']; option6.add("statictext", u, "Search:"); var searchList6 = option6.add("dropdownlist",[0,0,240,20], myFieldArray); searchList6.minimumSize.width = 220; searchList6.selection = 0; option6.add("statictext", u, "Replace:"); var replaceList6 = option6.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList6.minimumSize.width = 220; replaceList6.selection = 0; } option6.visible = false; option6.maximumSize = NULL_SIZE; option6.spacing = NULL_SPACING; option6.margins = NULL_MARGINS; searchDropdownArray.push(searchList6); replaceDropdownArray.push(replaceList6); optionArray.push(option6); var option7 = w.add("group") { option7.orientation = "row"; option7.alignment = ['fill','']; option7.add("statictext", u, "Search:"); var searchList7 = option7.add("dropdownlist",[0,0,240,20], myFieldArray); searchList7.minimumSize.width = 220; searchList7.selection = 0; option7.add("statictext", u, "Replace:"); var replaceList7 = option7.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList7.minimumSize.width = 220; replaceList7.selection = 0; } option7.visible = false; option7.maximumSize = NULL_SIZE; option7.spacing = NULL_SPACING; option7.margins = NULL_MARGINS; searchDropdownArray.push(searchList7); replaceDropdownArray.push(replaceList7); optionArray.push(option7); var option8 = w.add("group") { option8.orientation = "row"; option8.alignment = ['fill','']; option8.add("statictext", u, "Search:"); var searchList8 = option8.add("dropdownlist",[0,0,240,20], myFieldArray); searchList8.minimumSize.width = 220; searchList8.selection = 0; option8.add("statictext", u, "Replace:"); var replaceList8 = option8.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList8.minimumSize.width = 220; replaceList8.selection = 0; } option8.visible = false; option8.maximumSize = NULL_SIZE; option8.spacing = NULL_SPACING; option8.margins = NULL_MARGINS; searchDropdownArray.push(searchList8); replaceDropdownArray.push(replaceList8); optionArray.push(option8); var option9 = w.add("group") { option9.orientation = "row"; option9.alignment = ['fill','']; option9.add("statictext", u, "Search:"); var searchList9 = option9.add("dropdownlist",[0,0,240,20], myFieldArray); searchList9.minimumSize.width = 220; searchList9.selection = 0; option9.add("statictext", u, "Replace:"); var replaceList9 = option9.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList9.minimumSize.width = 220; replaceList9.selection = 0; } option9.visible = false; option9.maximumSize = NULL_SIZE; option9.spacing = NULL_SPACING; option9.margins = NULL_MARGINS; searchDropdownArray.push(searchList9); replaceDropdownArray.push(replaceList9); optionArray.push(option9); var option10 = w.add("group") { option10.orientation = "row"; option10.alignment = ['fill','']; option10.add("statictext", u, "Search:"); var searchList10 = option10.add("dropdownlist",[0,0,240,20], myFieldArray); searchList10.minimumSize.width = 220; searchList10.selection = 0; option10.add("statictext", u, "Replace:"); var replaceList10 = option10.add("dropdownlist",[0,0,240,20], myFieldArray); replaceList10.minimumSize.width = 220; replaceList10.selection = 0; } option10.visible = false; option10.maximumSize = NULL_SIZE; option10.spacing = NULL_SPACING; option10.margins = NULL_MARGINS; searchDropdownArray.push(searchList10); replaceDropdownArray.push(replaceList10); optionArray.push(option10); var newRow = w.add("group"); newRow.orientation = "row"; newRow.alignChildren = "right"; newRow.alignment = ['fill','bottom']; var addGroup = newRow.add("group") addGroup.alignment = "left"; addGroup.orientation = "row"; var addButton = addGroup.add("button",u, "Add"); var removeButton = addGroup.add("button",u, "Remove"); var okCancelGroup = w.add("group"); okCancelGroup.alignment = ['right','bottom']; okCancelGroup.alignChildren = "right"; okCancelGroup.orientation = "row"; var okButton = okCancelGroup.add("button",u, "OK"); var cancelButton = okCancelGroup.add("button",u, "Cancel"); addButton.onClick = function() { if(optionCount < 9) { optionCount = optionCount + 1; optionArray[optionCount].maximumSize = MAX_SIZE; optionArray[optionCount].spacing = MAX_SPACING; optionArray[optionCount].margins = MAX_MARGINS; w.spacing = w.spacing + 1; optionArray[optionCount].visible = 1; w.layout.layout(1); } else { alert("Maximum Number of Replacements Reached."); } }; removeButton.onClick = function() { if(optionCount != 0) { optionArray[optionCount].maximumSize = NULL_SIZE; optionArray[optionCount].spacing = NULL_SPACING; optionArray[optionCount].margins = NULL_MARGINS; w.spacing = w.spacing - 1; optionArray[optionCount].visible = 0; optionCount = optionCount - 1; w.layout.layout(1); } else { alert("You must have at least one Search and Replace"); } }; okButton.onClick = function() { for(var z = 0; z < optionCount+1; z++) { var searchOptionText = searchDropdownArray[z].selection.text; var replaceOptionText = replaceDropdownArray[z].selection.text; var h = z + 1; if(searchOptionText == "Please Select" || replaceOptionText == "Please Select") { alert("You have not chosen a field for Option" + " " + h); return; } } //STORE DROPDOWN RESULTS for(var i = 0; i < optionCount+1; i++) { var searchOptionText = searchDropdownArray[i].selection.text; var replaceOptionText = replaceDropdownArray[i].selection.text; if(searchOptionText != "Please Select" || replaceOptionText != "Please Select") { searchArray.push(searchOptionText); replaceArray.push(replaceOptionText); } } w.close(); }; w.show(); var searchArrayLength = searchArray.length; for(var y = 0; y < searchArrayLength; y++) { var searchText = searchArray[y]; var replaceText = replaceArray[y]; myDS.replaceFields(myDoc, searchText, replaceText); }; myDV.closeDataView(); }; function chooseDataSource() { var labelWidth = 200; var minWidth = 156; var dsDialog = app.dialogs.add({name:"Data Source Selection"}); var myDropDown; with (dsDialog.dialogColumns.add()) { with (dialogRows.add()) { with (dialogColumns.add()) { with (dialogRows.add()) { staticTexts.add({staticLabel:"Please select a datasource to update:", minWidth:labelWidth}); //CREATING A LIST OF DATASOURCES AND ADDING THEM TO AN ARRAY var dsArray = app.easycatalogObject.datasources.everyItem().name; myDropDown = dropdowns.add(); myDropDown.stringList = dsArray; myDropDown.selectedIndex = 0; } } } } //SHOW DIALOG var dsResult = dsDialog.show(); //IF SELECTION HAS BEEN MADE WILL RETURN DATASOURCE TO EASYCATALOG if (dsResult) { myDS = app.easycatalogObject.datasources.item(myDropDown.selectedIndex); return myDS; } //RETURN NULL IF NO SELECTION HAS BEEN MADE alert("No datasource selected"); return false; }; |