Skip to content

Instantly share code, notes, and snippets.

@milligramme
Last active July 13, 2016 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milligramme/42db859acb7be844c81475578f6ca1db to your computer and use it in GitHub Desktop.
Save milligramme/42db859acb7be844c81475578f6ca1db to your computer and use it in GitHub Desktop.
sample.jsx change pdf filename if already exists
//@target "InDesign"
var increase_or_suffix = function (filename, suffix) {
if (suffix == undefined) {
var reg = new RegExp('(\\d+)(\\..+$)');
var ret = filename.replace(reg, function (m) {
var mm = m.match(reg);
if(mm) {
return (mm[1] * 1 + 1) + mm[2]
}
})
return ret;
}
else {
var reg = new RegExp('(.+)(\\..+$)');
var ret = filename.replace(reg, function (m) {
var mm = m.match(reg);
if (mm) {
return mm[1] + suffix + mm[2]
}
})
return ret;
}
}
var increase_filename = function (path, suffix) {
if (File(path).exists) {
path_fix = increase_or_suffix(path);
if (path_fix !== path) {
path = path_fix;
}
else {
path_fix = increase_or_suffix(path, suffix);
if (path_fix) {
path = path_fix;
}
}
return path
}
return path
}
var doc = app.documents[0];
var story = doc.stories[0];
var export_path = "~/Desktop/dont.pdf";
for (var i=0, len=story.paragraphs.length; i < len ; i++) {
export_path = increase_filename(export_path, "-copy1");
doc.exportFile(ExportFormat.PDF_TYPE, File(export_path));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment