Copy link to clipboard
Copied
TL;DR
How can I get the name of the document (copy) created by ‘Save a Copy’ from a script?
Problem description
I have found a script that automatically creates an IDML file of the currently open document when the document is saved. It works even if it is ‘Saved As’ after appending
app.addEventListener('afterSaveAs', saveIDML, false);
I want to append another line (actually, same line as above, but substitute `afterSaveAs` with `afterSaveACopy`) in order to create IDML files of the copied documents.
However, I have no idea how to get the name of the copied document.
Is there a way to get the filename of that document?
Update
The direct answer for this question is in this comment. The accepted answer incorporates that solution into a script in which I needed to know the copied file path and name (for more info, read all comments in the chronological order).
Hi mike,
In "afterSaveACopy" event you only need to export IDML as named of copied file then why are you putting all other codes for active document?
Instead you can only have your code like this:
#targetengine "session"
/////////////////////////////////////////////////////////////////////////////////////////////////////
function saveIDML() {
if(app.layoutWindows.length == 0) return;
else if (!app.activeDocument.saved) {
alert('Sorry, there was a problem and the document was not sav
Copy link to clipboard
Copied
Hello tukusejssirs,
If you are truly going to do a "Save A Copy" without modifing the document name copy.Indd in the dialog window, you can simply change the line below.
\\Change:
var idmlName = inddName.replace("indd", "idml");
\\Change to:
var idmlName = inddName.replace(".indd", " copy.idml");
Regards,
Mike
Copy link to clipboard
Copied
Hello tukusejssirs,
I just realized that you wanted all three options for creating the IDML file.
"afterSave"
"afterSaveAs"
"afterSaveACopy"
"Save A Copy" without modifing the "document name copy.Indd" in the dialog.
#targetengine "session"
function saveIDML() {
if(app.layoutWindows.length == 0) {
return;
} else {
var doc = app.activeDocument;
if (!doc.saved) {
alert('Sorry, there was a problem and the document was not saved.');
exit();
}
var inddName = doc.name;
var idmlName = inddName.replace(".indd", ".idml");
var theFile = File(File(doc.filePath).fsName + "/" + idmlName);
doc.exportFile(ExportFormat.INDESIGN_MARKUP, theFile, false);
}
}
function saveCopyIDML() {
if(app.layoutWindows.length == 0) {
return;
} else {
var doc = app.activeDocument;
if (!doc.saved) {
alert('Sorry, there was a problem and the document was not saved.');
exit();
}
var inddName = doc.name;
var idmlName = inddName.replace(".indd", " copy.idml");
var theFile = File(File(doc.filePath).fsName + "/" + idmlName);
doc.exportFile(ExportFormat.INDESIGN_MARKUP, theFile, false);
}
}
// Listen for the save event
app.addEventListener('afterSave', saveIDML, false);
app.addEventListener('afterSaveAs', saveIDML, false);
app.addEventListener('afterSaveACopy', saveCopyIDML, false);
Regards,
Mike
Copy link to clipboard
Copied
Hi tukusejssirs,
You can get file saved as copy like this:
#targetengine "session"
function getFileName(e) {
var newFileName = File(e.properties.fullName).fsName.toString().replace(/\\/g,'/');
alert(newFileName);
}
app.addEventListener('afterSaveACopy', getFileName, false);
Best
Sunil
Copy link to clipboard
Copied
Thank you, @mikeb41294032 and @Sunil_Yadav1!
However, it seems I didn’t describe my issue here properly.
When I a document copy via ‘Save a Copy’, I am ask for the location and name of the copy. It could anything (both location and the filename). And what is most important difference between ‘Save As’ and ‘Save a Copy’ is that currently open document does change to the newly created document, therefore `app.activeDocument` won’t get (obviously) the copied document data.
I more thought of something like `app.coppiedDocument` or just catch the path and name via the listener or something.
Copy link to clipboard
Copied
Hi tukusejssirs,
Try that code which I posted will not give you app.activeDocument, Instead it will give you copied document full path when you use "Save A Copy".
Best
Sunil
Copy link to clipboard
Copied
Aargh, sorry, did not tested it until now.
Your function works as expected, but when I try to implement it into the function linked in the OP, it does not work. What does not work?
Here is the code I tried.
// // Set a targetengine to make this work
#targetengine 'session'
// function saveIdml(copiedFilename) {
function saveIdml(copiedFilename) {
alert(app.activeDocument.name);
// Exit if no documents are open.
if(app.layoutWindows.length == 0) {
return;
} else {
// Get the current document
var doc = app.activeDocument;
var inddName;
if (typeof copiedFilename == 'undefined') {
inddName = doc.name;
} else {
inddName = copiedFilename;
}
// Output inddName
// Note: This is used for debugging only.
alert(inddName);
// Catch errors
if (!doc.saved) {
alert('A problem occured while saving ' + inddName + '.\nThe document was not saved as IDML.', 'Saving IDML');
exit();
}
// Create a new .idml file name from the .indd file name
var idmlName = inddName.replace('indd', 'idml');
// Create the new .idml file next to the .indd file
var idmlFile = File(File(doc.filePath).fsName + '/' + idmlName);
doc.exportFile(ExportFormat.INDESIGN_MARKUP, idmlFile, false);
}
}
function getCopiedFilename(e) {
var copiedFilename = File(e.properties.fullName).fsName.toString().replace(/\\/g,'/');
saveIdml(copiedFilename);
}
// Listen for the save event
app.addEventListener('afterSave', saveIdml, false); // After the document is saved
app.addEventListener('afterSaveAs', saveIdml, false); // After the document is saved as
app.addEventListener('afterSaveACopy', getCopiedFilename, false); // After a copy of the document is saved
Copy link to clipboard
Copied
@Sunil_Yadav1 wrote:
"
#targetengine "session"
function getFileName(e) {
var newFileName = File(e.properties.fullName).fsName.toString().replace(/\\/g,'/');
alert(newFileName);
}
app.addEventListener('afterSaveACopy', getFileName, false);
Best
Sunil
"
😉
nice formatting
(hope so that "forum code formatting bug" will be fixed soon)
Copy link to clipboard
Copied
I see no difference between your code and my code (yes, I have changed the variable names, but else it is the same code).
Copy link to clipboard
Copied
(Ähem? That wasn't my code - it was only a quote.)
But:
Do you really see no (color) difference?
Screenshot:
Copy link to clipboard
Copied
> Do you really see no (color) difference?
@pixxxel_schubser, that comment of mine was not directed on your comment.
However, I forgot to comment on your (original) comment. I does not work as I'd like to. You just replace `.indd` with ` copy.idml` and save the file in the same directory as the currrently active document, which is not desired because:
I suggest you to try in your InDesign to Save a Copy of your currently open document. You can do that either using File and then Save a Copy or (on Windows) by pressing Ctrl+Alt+S. Then you will be presented by ad model dialogue in which you can choose the copied file location (any) and save it as INDD file. And the script, which I want to create (with the help of other users), would then (`afterSaveACopy`) automatically create an IDML file in the same location as the copied file with the same name as the copied file expect for the file extension.
This works for `afterSave` and `afterSaveAs`.
@Sunil_Yadav1 has a solution, how to get the copied file path and its name, but it does not work, when I have implemented it into the original function (see this comment for the code that needs to be fixed).
Copy link to clipboard
Copied
Hi tukusejssirs,
Remove all other event listeners, just run this below code.
This will work like if you as a file as copy after that with the same name idml will be exported in the folder where you saved you file as copy.
#targetengine "session"
function getFileName(e) {
var newFileName = File(e.properties.fullName).fsName.toString().replace(/\\/g,'/');
doc.exportFile(ExportFormat.INDESIGN_MARKUP, newFileName, false);
}
app.addEventListener('afterSaveACopy', getFileName, false);
Best
Sunil
Copy link to clipboard
Copied
Hello tukusejssirs,
Try the script below, it's working for all three scenarios..........
"afterSave"
"afterSaveAs"
"afterSaveACopy"
// Set a targetengine to make this work as Startup script
#targetengine "session"
function saveIDML() {
if(app.layoutWindows.length == 0) {
return;
} else {
var doc = app.activeDocument;
if (!doc.saved) {
alert('Sorry, there was a problem and the document was not saved.');
exit();
}
var inddName = doc.name;
var idmlName = inddName.replace(".indd", ".idml");
var theFile = File(File(doc.filePath).fsName + "/" + idmlName);
doc.exportFile(ExportFormat.INDESIGN_MARKUP, theFile, false);
}
}
function getFileName(e) {
if(app.layoutWindows.length == 0) {
return;
} else {
var doc = app.activeDocument;
if (!doc.saved) {
alert('Sorry, there was a problem and the document was not saved.');
exit();
}
var newFileName = File(e.properties.fullName).fsName.toString().replace(/\\/g,'/').replace(".indd", ".idml");
doc.exportFile(ExportFormat.INDESIGN_MARKUP, newFileName, false);
}
}
// Listen for the save event
app.addEventListener('afterSave', saveIDML, false);
app.addEventListener('afterSaveAs', getFileName, false);
app.addEventListener('afterSaveACopy', getFileName, false);
Regards,
Mike
Copy link to clipboard
Copied
Hi mike,
In "afterSaveACopy" event you only need to export IDML as named of copied file then why are you putting all other codes for active document?
Instead you can only have your code like this:
#targetengine "session"
/////////////////////////////////////////////////////////////////////////////////////////////////////
function saveIDML() {
if(app.layoutWindows.length == 0) return;
else if (!app.activeDocument.saved) {
alert('Sorry, there was a problem and the document was not saved.');
return;
}
var idmlPath = app.activeDocument.filePath.fsName.replace(/\\/g,'/')+'/'+app.activeDocument.name.replace(/\.indd|\.indt/g,'.idml');
app.activeDocument.exportFile(ExportFormat.INDESIGN_MARKUP, idmlPath, false);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function saveACopyIDML(e) {
var idmlPath = File(e.properties.fullName).fsName.toString().replace(/\\/g,'/').replace(/\.indd|\.indt/g,'.idml');
app.activeDocument.exportFile(ExportFormat.INDESIGN_MARKUP, idmlPath, false);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Listen for the save event
app.addEventListener('afterSave', saveIDML, false);
app.addEventListener('afterSaveAs', saveIDML, false);
app.addEventListener('afterSaveACopy', saveACopyIDML, false);
/////////////////////////////////////////////////////////////////////////////////////////////////////
P.S : Always try not to put a lot of extra code which is not needed for better understanding for users.
Best
Sunil
Copy link to clipboard
Copied
Perfect! Thank you all for the help!