Packaging script working fine on local drive, but not on network drive
Hi everyone. I've been trying to make a script to help generate a Document fonts folder in the same dirrectory as the .indd file.
The script does a package on the desktop in a temporary folder named with the date and time, to prevent any error of an existing folder with the same name. Then it checks if a Document font folder is present in the directory of the .indd file. If there is, it renames the folder by adding the current date and time (to prevent deleting anything). The script then waits for 3 seconds to prevent any problem caused by a delay in renaming on the NAS. Then the script creates a new Document fonts folder in the source directory, and copies every font present in the packaged Document font folder on the Desktop to it. Finally, the temporary package folder created on the desktop is emptied and deleted and the user is prompted that the script has done it's job.
The script works fine on a local drive, but we are all working on a NAS and the script fails to produce a package when the fource file is located on the NAS. I can't figure out what's causing it to fail.
We are all working on macs and I have not taken in consideration a Windows environement in this script. We are connecting to the NAS trough AFP protocol. The machine this was tested on is using MacOs Mojave 10.14.6, and InDesign 15.0.1
I apologize for the messy script, I don't usually dive in this deep in scripting. I have experience in modifying existing script to adapt them to my needs. This script is kind of a Frankenstein's monster of multiple scripts I copied parts of.
So, here's the script:
var scriptName = "Collect Document fonts",
doc;
PreCheck();
//===================================== FUNCTIONS ======================================
function Main() {
var today = new Date();
var monthNum = today.getMonth()+1;
if(monthNum < 10){
var month = '0' + monthNum;
}else{
var month = monthNum;
}
var date = today.getFullYear()+'-'+month+'-'+today.getDate();
var hour = today.getHours();
var minute = today.getMinutes();
if(minute < 10){
var time = hour + 'h0' + minute;
}else{
var time = hour + 'h' + minute;
}
var dateTime = date + "_" + time;
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
var doc = app.activeDocument;
var source = Folder(doc.filePath);
var desktopPath = "~/Desktop/package temporaire " + dateTime;
var fontPath = (desktopPath + "/Document fonts");
fontsFolder = new Folder(desktopPath);
if (!fontsFolder.exists) fontsFolder.create();
doc.packageForPrint(to = Folder(desktopPath), true, false, false, false, false, false, false);
var vieux = Folder(source + "/Document fonts");
if (vieux.exists) {
vieux.rename ("Document fonts (" + dateTime +")");
}
wait(3000);
var files = Folder(fontPath).getFiles();
var files_a = Folder(desktopPath).getFiles();
var destinationFolder = new Folder(source + "/Document fonts");
if (!destinationFolder.exists) destinationFolder.create();
for (var i = files.length; i >= 0; i--) {
var file = File(files[i]);
file.copy(destinationFolder.fsName + "/" + file.name)
file.remove();
}
for (var i = files_a.length; i >= 0; i--) {
var file_a = File(files_a[i]);
file_a.remove();
}
var lastFolder = Folder(desktopPath);
lastFolder.remove();
alert("Done!", scriptName);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function PreCheck() {
if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
doc = app.activeDocument;
if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);
if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
Main();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
alert(error, scriptName, icon);
exit();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------