I'm currently using InDesign CS5.5
I have an automation script I wrote for datamerge in javascript but I've had trouble finding any references to searching for files with javascript. I'm well aware you can't in javascript for the web but there is server side scripting but can't find where any tutorials are online.
What im trying to acheive is this line here... but in javascript.
var rootName = app.doScript('set theFile to (do shell script "ls /Path/To/Folder")', ScriptLanguage.APPLESCRIPT_LANGUAGE);
now to explain why im using this incase anyone wants this or wants to know... this will give me a list of filenames in a directory..
I then apply..
var filename = rootName.split('.').reverse().slice(1).reverse().join('.');
which strips everything but the name of the file without the extension.
Part of my program is.. each csv file that comes in will not have the same name so I use this to get the name and then later apply it to my datasource and for saving out the PDF.
I'd appreciate if anyone can help me acheive looking for a file within javascript or if they could send me a link to where some documentation is.
I have tried using getFiles("*.csv") but I get errors.. when I slap an alert after getting the file it shows the file with the entire path and I can't use slice because it doesn't do anything. literally it doesn't slice. This is why I was using applescript. I'm not sure if its more efficient or not to use Javascript for this or Applescript just would like to keep it all one language.
Eventually I'd like to figure out how to set up a for function or some sort of count to grab only 1 file at a time in the case that 2 files make it to the directory.
Also if anyone wants the code to do a datamerge and save out to pdf I'll gladly post.
Thanks
jtcdesigns1 wrote:
I have tried using getFiles("*.csv") but I get errors.. when I slap an alert after getting the file it shows the file with the entire path and I can't use slice because it doesn't do anything. literally it doesn't slice
I think the problem lies in what getFiles is returning. This is not a single file, but it's always an array -- even when there are no files found (in which case the length of the array is 0). Your slicing-and-dicing does not work on an array, hence the error. Your alert, on the other hand, does know how to display an array -- it shows all members, comma separated, but if there is only one member, you cannot tell from the alert text if it was an array or not.
Eventually I'd like to figure out how to set up a for function or some sort of count to grab only 1 file at a time in the case that 2 files make it to the directory.
Yep, doable. Use getFiles with your file mask and check the length of the returned array. If it's zero, there are no files. If it's 1, you can use item[0] of the returned array for your file name; if it's more than one, you can show your select-a-file dialog.
jtcdesigns1 wrote:
I'm better at modifying than creating
![]()
Modify this to taste ![]()
filename = fetch_me_a_file ("*.csv");
if (filename == null)
alert ("No file found, or user canceled");
else
alert (filename);
function fetch_me_a_file (mask)
{
var fileList = Folder.myDocuments.getFiles(mask);
if (fileList.length == 0)
{
alert ("No files found");
return null;
}
if (fileList.length == 1)
return fileList[0].name;
var selectedFile = Folder.myDocuments.openDlg("Fetch me a file", mask);
// Canceled?
if (selectedFile == null)
return null;
return selectedFile.name;
}
I was wondering if and how your Split Off Filename line worked, but decided to use this RegExp instead:
name = filename.replace(/^.+\//, '');
(replacing everything before and including the final forward slash with nothing) but then I thought, wait a minute. The 'object' returned by both getFiles and by openDlg is not actually a file name -- even though JS conveniently translates it to a fully qualified path when asked --, it's a file object. And a regular File object has this useful property 'name': that's just the file name.
Your the man! I'll give this a go soon. Unfortunately I have to present this to a client as "automated" to them... and eventually will get it working correctly. Its weird im using an automation system to move a file to that folder.. then I'll have a folder action script to trigger if any files are found then run another script to launch the script im working on for automerge... once I get this it will help with the multiple files.. Just need to get the folder action working and should be good to go.
I'll post my results once I get back.. I hate getting pulled away from projects ![]()
so now I feel stupid.. I understand this code.. except Folder.myDocuments.getFiles(mask);
Not sure if I should be replacing this with something else
for filename I knew to change it to the real location
filename = File("/Users/jamescallahan/Desktop/Automation/csv/").getFiles("*.csv" );
also where you have if (filename == null) it seems to not alert if there is no file.. but if I change it to " " it works fine
North America
Europe, Middle East and Africa
Asia Pacific