Copy link to clipboard
Copied
I encountered a small problem in my scripts when upgrading to Illustrator 2020. When running through a batch of files, I'm generating a file name, and then displaying a save dialog to the user with the generated file name as the default option. In AI 2020, the dialog shows 'Untitled' instead of the file name. I'm not sure what's causing this. I have scripts in Photoshop 2020 that are similar, and they still work as before. Here's a snippet of what I'm doing:
var someGeneratedFileName = 'test.pdf';
var destinationFile = new File('~/Desktop/' + someGeneratedFileName);
var saveFile = destinationFile.saveDlg('Save file'); // This defaults to 'Untitled.pdf' instead of 'test.pdf'
if (saveFile) app.activeDocument.saveAs(saveFile);
I think my issue might be related to this thread, but I'm not sure how to translate the AppleScript solution into JavaScript. I'm running MacOS 10.14.6 (Mojave) and Illustrator 24.0.1 if that helps.
Thanks!
Copy link to clipboard
Copied
Yes, this is definitely a bug in 2020. Anybody got a solution?
Copy link to clipboard
Copied
This has messed up my workflow as well. I ended up having to work around the problem as a two-stepper:
1. Write a custom file input dialog using ScriptUI
2. Specify the destination with the Folder.select() method
It's not perfect but at least the script is functional again. As an example for the code you provided:
#target illustrator
#targetengine "main"
// create a custom input window for specifying the
// new document's file name
function setNewDocName(docName) {
// strip away the file extension so it's easier for
// the user to quickly modify the filename
// we'll append the extension later if it's not specified
docName = docName.split('.')[0];
var fileNameInputWindow = new Window("dialog", "Save as PDF...");
var fileNameForm = fileNameInputWindow.add("group");
fileNameForm.add("statictext", undefined, "Name:");
var fileNameTextField = fileNameForm.add("edittext", undefined, docName);
fileNameTextField.characters = 40;
fileNameTextField.active = true;
var confirmationButtons = fileNameInputWindow.add("group");
confirmationButtons.alignment = "right";
confirmationButtons.add("button", undefined, "Save", { name: "ok" });
confirmationButtons.add("button", undefined, "Cancel");
if (fileNameInputWindow.show() == 1) {
return fileNameTextField.text;
}
}
function setNewDocLocation(currentFileLocation) {
// default to current document's folder
// if document has not yet been saved, default to user's home folder
var parentDirectory = currentFileLocation.toString();
parentDirectory = parentDirectory.substr(0, parentDirectory.lastIndexOf('/')) + "/";
parentDirectory = parentDirectory === '/' ? "~/" : parentDirectory;
// display browser dialog to allow user to override save location
var destination = Folder.selectDialog('Select save location...', parentDirectory);
return destination;
}
function buildTargetFile(destination, docName) {
var newName = "";
var extension = '.pdf';
if (docName.indexOf('.') < 0) {
newName = docName + extension;
} else {
var dot = docName.lastIndexOf('.');
newName += docName.substring(0, dot);
newName += extension;
}
var newFile = new File(destination + '/' + newName);
return newFile;
}
function saveAsPDF() {
if (!app.documents.length) { return }
var sourceDoc = app.activeDocument;
// Use ScriptUI to let user specify filename
// Exits script if cancel button is used
var newDocName = setNewDocName(sourceDoc.name);
if (!newDocName) { return }
// Use Folder dialog to let user specify save location
// Exits script if cancel button is used
var newDocDestination = setNewDocLocation(sourceDoc.fullName);
if (!newDocDestination) { return }
var newFileObject = buildTargetFile(newDocDestination, newDocName);
var pDFSaveOptions = new PDFSaveOptions();
sourceDoc.saveAs(newFileObject, pDFSaveOptions);
}
saveAsPDF();
I'm hoping someone will come along and say it's just a matter of using the File class's decode() or encode() methods, I just haven't had any luck with either. I'm also sure there's a way to include the location selection in the ScriptUI dialog...but I couldn't be bothered to write all the logic for a file browser. Good luck, I hope at least pieces of this are helpful.