• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Rename a document

New Here ,
Nov 04, 2016 Nov 04, 2016

Copy link to clipboard

Copied

Hi,

I'm trying to check the document name and ask the user to rename it if it doesn't conform to this format xxxxxx Issxx.

I'm having trouble renaming the file. I understand you have to close the document before you can rename it.

Can anyone offer any advice for me code?

var doc = app.activeDocument;
function renameMe () {
    var p = prompt( "Please enter a valid file name" );
    checkInput(p);
    var file = doc.filePath,
    myFileName = doc.name.substr( 0, doc.name.indexOf( "." ) ),
    myNewFileName = myFileName.replace( myFileName, p + ".indd" );
    doc.close();
    //need to close the doc and find the folder to rename
    var newFile = file.rename( myNewFileName );
    app.open( file );
    }

function checkInput (check) {
     if ( !check.match( "\\d{6}\\sIss\\d+" ) ) {
    alert( "File name should follow convention 000000 Iss0" );
    renameMe();
    }
}

if (!doc.name.match( "\\d{6}\\sIss\\d+" ) ) {
    renameMe();
    }

Thanks,

Jake

TOPICS
Scripting

Views

727

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Mentor , Nov 04, 2016 Nov 04, 2016

Hi,

Replace theline:

var file = doc.filePath,     //     which in fact is a file parent (folder)

with

var file = doc.fullName,

Jarek

Votes

Translate

Translate
Mentor ,
Nov 04, 2016 Nov 04, 2016

Copy link to clipboard

Copied

Hi,

Replace theline:

var file = doc.filePath,     //     which in fact is a file parent (folder)

with

var file = doc.fullName,

Jarek

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 07, 2016 Nov 07, 2016

Copy link to clipboard

Copied

LATEST

Perfect! You gave me the last piece of the puzzle

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines