Skip to main content
Participating Frequently
July 17, 2019
Answered

Script to run action on predefined folder

  • July 17, 2019
  • 1 reply
  • 5043 views

I need a script that runs an action (action is already loaded on the machine) on a predefined folder with no dialogues.

-------------------------

Narrative use:
1.) Windows 10 operates with batch files to pull constantly updating PDF artwork from web query, stores links / data in xml, outputs to an html page where jquery is used to compose a wget command for each file based off info in xml, phantomjs reads the rendered page and creates an plain txt file from rendered html, windows "type" command and others turns that into a cmd file and condenses into one line so that it is executable, system scheduler runs that cmd file routinely to download new print files.

2.) Those print files are all in one folder. I need to process those in illustrator using 1 action and I would like it to be hands-free, hence the use of the script to invoke the action. So, I would set up system scheduler to launch the .js or .jsx file with illustrator, at a specified time each day, to process the pdf files.

Alternative, hands-on, currently being used version to step 2.) Action panel > batch > already have the source folder, already have the action, just clicking "run" routinely. The action already compensates for destination folder.

--------------------------
There doesn't seem to be a very clean template in the forums for a simple SOURCE FOLDER > ACTION batch script, with no dialogues or prompts for additional info, like "selectFolder" etc.

Can anyone help? If this has already been answered, or if you're better at navigating the forums and have any related posts, please link me.

This topic has been closed for replies.
Correct answer jk__keller

For the public: marked this as the answer since it's finalized, please review williamadowling'scomments and helpful resolutions to this issue.

------

In response to williamadowling​: The action needed a "select all" before each "make clipping mask" action. Although "select all" existed earlier in the action, illustrator did not recognize that every layer was properly selected. I'm not great with javascript

Awesome! This works like a charm. Almost punched my computer when it all worked seamlessly.

Saved this as a .jsx and put it on a schedule, via the "System Scheduler" windows app to routinely run and sync with other processes.

Path to folder starts with a letter, single slash, then a double slash before each directory aside from the one immediately after letter drive.

I had no idea how to set this up. You helped a TON Willie Streeter​. I liked and marked your comments as helpful. If there's any other way I can boost your rank, on here, let me know. Also, if there's a cool place to post this project or learn from others, in a more organized way than grinding through forums, let me know because I'd love to join.

Have a wonderful day, you're the bomb

-Andrew

------

FULL OUTLINED PROCESS, TO SHOW WHERE JSX FILE FITS INTO WORKFLOW:
(This is a little messy and over-exposed to errors. As a bare-bones, solely machine-executed process it works really well and has a cycle length of 12 hours. This process replaces a ton of labor)

Process begins via a REST API url that directs me to an ASP/AJAX REST API xml output page littered with API tags > xml file from that page is saved via wget cmd > jquery renders that xml on an html file > phantomjs renders jquery html page to txt file > TYPE cmd pulls  cmd file > powershell script removes unnecessary lines in cmd file which caused it to error out > command file launches which simply does a "wget -nc -O" command that updates a local directory with print files > cmd file copies recent files to a temporary location [Folder("J:\PATH\\TO\\FOLDER");] > after copying new files to an isolated location the system scheduler launches this jsx file which runs through that location and saves to production folder > after the process is successfully completed the temporary art folder is emptied via cmd file > success.

-----------------------------------------------------------------------------
JSX TO RUN ACTION ON PREDEFINED FOLDER / JUST LIKE ACTIONS, BATCH PROCESS:
-----------------------------------------------------------------------------

/* Best if invoked via your OS command line- that's the purpose of this. It's basically just performing a batch operation. You can't run an action via system launch arguments or the command line so performing routine batch operations are difficult. Now you dont have to manually set up your batch operations every time. */

// uncomment to suppress Illustrator warning dialogs

// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var destFolder, sourceFolder, files, fileType

sourceFolder = Folder("J:\PATH\\TO\\FOLDER");

//Alerts for troubleshooting folder and file setup (thanks to williamadowling on adobe forums )

//alert("sourceFolder.exists = " + sourceFolder.exists);

//alert("sourceFolder.fsName = " + sourceFolder.fsName);

//alert("sourceFolder contains " + sourceFolder.getFiles().length + " total files.");

//alert("sourceFolder contains " + sourceFolder.getFiles("*.pdf").length + " .pdf files.");

// If a valid folder is selected

if ( sourceFolder != null )

{

  files = new Array();

    //What kind of file in the source folder are you seeking?

    fileType = "*.pdf";

    // Get all files matching that file type

files = sourceFolder.getFiles( fileType );

if ( files.length > 0 )

{

for ( i = 0; i < files.length; i++ )

{

//Open a file

app.open(files)

{

   //On that file, perform an action

   app.doScript ("ACTION NAME", "ACTION SET/GROUP NAME")

}

}

    alert( 'Complete' );

}

else

{

alert( 'No matching files found' );

}

}


Just an update for those who may try using the above code... to work as intended, I had to change the line:

app.open(files)

to:

app.open(files[i])

 

so that it would loop through the array of found files

1 reply

Disposition_Dev
Legend
July 17, 2019

certainly doable. Are you looking for help to code it yourself, or are you looking to have it written?

Participating Frequently
July 17, 2019

I have something like this, pulled and edited from another forum post, and I'm not even sure if this is correct. I just completed all the tasks in first step and my brain is melted.

The original and edited code are both below.
-Commented characters are crossed out w/the adobe forum's text editor.
-Unedited code is in normal formatting.
-Edited code is bold.

Right now it's getting hung up and saying "no matching files found".
(potential contingency: The source folder is a network location that I have mapped as a letter drive.)

The app.doScript ("Name of Action", "Name of Action Set") is just a guess as to where that belongs.

/*Script Begin*/

// uncomment to suppress Illustrator warning dialogs

// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var destFolder, sourceFolder, files, fileType /* sourceDoc, targetFile, svgSaveOpts;*/

sourceFolder = Folder("DriveLetter:\Path\to\folder");

// If a valid folder is selected

if ( sourceFolder != null )

{

  files = new Array();

    fileType = "*.pdf";

// Get all files matching the pattern

files = sourceFolder.getFiles( fileType );

if ( files.length > 0 )

{

// Get the destination to save the files

//commented out//destFolder = Folder('DriveLetter:\Path\to\folder');

for ( i = 0; i < files.length; i++ )

{

app.doScript ("Name of Action", "Name of Action Set")

//commented out//sourceDoc = app.open(files); // returns the document object

// Call function getNewName to get the name and file to save the SVG

//commented out//targetFile = getNewName();

// Call function getSVGOptions get the SVGSaveOptions for the files

//commented out//svgSaveOpts = getSVGOptions( );

// Save as svg

//commented out//sourceDoc.exportFile(targetFile, ExportType.SVG, svgSaveOpts );

//commented out//sourceDoc.close();

}

alert( 'Files are saved as SVG in ' + destFolder );

}

else

{

alert( 'No matching files found' );

}

}

/*********************************************************

getNewName: Function to get the new file name. The primary

name is the same as the source file.

**********************************************************/

/*********************************************************

function getNewName()

{

var ext, docName, newName, saveInFile, docName;

docName = sourceDoc.name;

ext = '.svg'; // new extension for svg file

newName = "";

for ( var i = 0 ; docName != "." ; i++ )

{

newName += docName;

}

newName += ext; // full svg name of the file

// Create a file object to save the svg

saveInFile = new File( destFolder + '/' + newName );

return saveInFile;

}

function getSVGOptions()

{

var svgSaveOpts = new ExportOptionsSVG();

//just using defaults aside from what's written below

  //see http://cssdk.host.adobe.com/sdk/1.0/docs/WebHelp/references/csawlib/com/adobe/illustrator/ExportOptionsSVG.html

  svgSaveOpts.embedRasterImages = true;

return svgSaveOpts;

}*/

Disposition_Dev
Legend
July 17, 2019

Is this the exact code you're running? Or did you obscure the paths and action names?