Copy link to clipboard
Copied
I found this script which work greats on any currently open Illustrator files, but I would like it to be able to run the action on a Mac OS folder, open the first Illustrator, runs this javascript, save & close that file, then loop through all other Illustrator files in a folder and repeat the process, until the last file.
Copy link to clipboard
Copied
Hello.
Using AppleScript or getting folder by "Folder.selectDialog" via ExtendScript will work great.
Copy link to clipboard
Copied
So I'm really new to all of this, but I'm assuming I would have to use Apple script to apply an action to a folder in Mac OS, go to the first file that is an Illustrator file, open it, then run the javascript on that file, save the change, close it, then jump back to the AppleScript to go to the next found Illustrator file and do it over again, looping through till all Illustrator files within that folder have had the change made.
I'm just not so sure how to do the AppleScript part. Any help in this area?
Copy link to clipboard
Copied
You can do all of this natively in JS. there's no reason to invoke applescript.
Copy link to clipboard
Copied
To my knowledge, AppleScript makes it easier if you want to run a script with folder actions like "droplet". Otherwise, JS is easy.
Copy link to clipboard
Copied
perhaps i misunderstood what was actually needed.
Susan, did you imagine running a script that would loop through the files of a folder upon execution? Or are you looking for a script that will watch the folder for files being added, and then run the script on the newly added files?
The former can easily be done in JS alone. However, the latter gets much deeper into the weeds with regards to cross-language coordination.
Copy link to clipboard
Copied
Yes, williamadowling, the first opt. I want to execute a loop through the files of a folder upon execution.
Copy link to clipboard
Copied
Run the script in Illustrator.
Copy link to clipboard
Copied
Hi OMOTI, thanks for the help.
I made the change to the script but now get 2 errors:
Here is the script as it currently stands:
// Opens the built-in platform-specific file-browsing dialog.
// If the user clicks OK, returns a File or Folder object for the selected file or folder. If the user cancels, returns null.
var folderObject = Folder.selectDialog("select Folder");
// Gets files in the folder.
var fileObjectList = folderObject.getFiles();
// Opens the file
for (var i = 0; i < fileObjectList.length; i++) {
app.open(fileObjectList);
}function FindAndReplaceScript_AllOpenDocuments(){
for(var i=app.documents.length -1; i > -1; i--){
app.documents.activate();
var aDoc = app.documents;
var searchString = /VT/gi;
var replaceString = " VHB " ;
var theTF = aDoc.textFrames;
if (theTF.length > 0) {
for (var j = 0 ; j <theTF.length; j++) {
var aTF = theTF
var newString = aTF.contents.replace(searchString, replaceString);
if (newString != aTF.contents) {
theTF
}
}
}
}
};
FindAndReplaceScript_AllOpenDocuments();
Copy link to clipboard
Copied
Bonjour,
Je vais au plus simple (sans les options d'ouverture et de sauvegarde)
//----------------------------------------------
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
// Gets files in the folder.
var folderObject = Folder.selectDialog("select Folder");
var fileObjectList = folderObject.getFiles("*.ai");var docRef;
for (var i = 0; i < fileObjectList.length; i++) {
docRef = app.open(fileObjectList);
FindAndReplaceScript_OpenDocuments(docRef);
docRef.saveAs (fileObjectList);
docRef.close(SaveOptions.DONOTSAVECHANGES);
}/-----------------------------------------
function FindAndReplaceScript_OpenDocuments(aDoc){
var searchString = /VT/gi;
var replaceString = "VHB";
var theTF = aDoc.textFrames;
if (theTF.length > 0) {
for (var j = 0 ; j <theTF.length; j++) {
var aTF = theTF;
var newString = aTF.contents.replace(searchString, replaceString);
if (newString != aTF.contents) {
theTF.contents = newString;
}
}
}
}
Copy link to clipboard
Copied
try this:
// Opens the built-in platform-specific file-browsing dialog.
// If the user clicks OK, returns a File or Folder object for the selected file or folder. If the user cancels, returns null.
var folderObject = Folder.selectDialog("select Folder");
// Gets files in the folder.
var fileObjectList = folderObject.getFiles();
//regex to check that the file is an .ai file
var filePat = /\.ai$/;
// Opens the file
for (var i = 0; i < fileObjectList.length; i++) {
if(filePat.test(fileObjectList.name))
{
continue;
}
app.open(fileObjectList[i]);
}function FindAndReplaceScript_AllOpenDocuments(){
for(var i=app.documents.length -1; i > -1; i--){
app.documents.activate();
var aDoc = app.documents;
var searchString = /VT/gi;
var replaceString = " VHB " ;
var theTF = aDoc.textFrames;
if (theTF.length > 0) {
for (var j = 0 ; j <theTF.length; j++) {
var aTF = theTF[j];
var newString = aTF.contents.replace(searchString, replaceString);
if (newString != aTF.contents) {
theTF.contents = newString;
}
}
}
}
};
FindAndReplaceScript_AllOpenDocuments();
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @Disposition_Dev thanks for posting that script here. I'm trying to replace periods for commas across multiple Illustrator documents. I want to use your script to do so but when I run it I get the attached error message. If it helps I'm running it on the 2022 version of Illustrator.
Any help much appreciated
Copy link to clipboard
Copied
Hey friend. When the forums were migrated to the new format a while back, the indexes got removed from all the code that was written in. I've edited that reply to have the appropriate indeces. But i also rewrote that script into a cleaner and more reliable version which you can find below:
function container()
{
//enter find and replace terms here
var searchString = /VT/gim;
var replaceString = " VHB ";
// Opens the built-in platform-specific file-browsing dialog.
// If the user clicks OK, returns a File or Folder object for the selected file or folder. If the user cancels, returns null.
var folderObject = Folder.selectDialog("select Folder");
// Gets files in the folder.
var fileObjectList = folderObject.getFiles();
//regex to check that the file is an .ai file
var filePat = /\.ai$/;
var aDoc;
var theTF;
var aTF;
var newString;
// Opens each file in the given folder
for (var i = 0; i < fileObjectList.length; i++)
{
if (filePat.test(fileObjectList.name))
{
continue;
}
app.open(fileObjectList[i]);
}
//activate each open document in turn and find replace
function FindAndReplaceScript_AllOpenDocuments()
{
for (var i = app.documents.length - 1; i > -1; i--)
{
app.documents.activate();
aDoc = app.documents;
theTF = aDoc.textFrames;
for (var j = 0; j < theTF.length; j++)
{
aTF = theTF[j];
newString = aTF.contents.replace(searchString, replaceString);
if (newString !== aTF.contents)
{
theTF.contents = newString;
}
}
}
};
FindAndReplaceScript_AllOpenDocuments();
}
container();