Script to populate the file info from Paragraph Styles?

Contributor ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

Is there a way auto-populate the file info of my document using paragraph styles? basically like variable text.

or is there a script that would fill in the file info with text that is assigned a paragraph so i could run the same script on multiple documents but it will fill in the doc specific info?

 

Screen Shot 2021-12-16 at 11.38.09 AM.png

TOPICS
How to , Scripting

Views

129

Likes

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

Adobe Community Professional , Dec 16, 2021 Dec 16, 2021

found is an arraty so you need to use the subscript operator to reference the items within the array. Try the following

var myDoc = app.activeDocument;
var docTitle = "";
var docDesc = "";
var docKeywords = "";
var docCopyright = "";

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
found = app.activeDocument.findGrep(true);
for (j = found.length - 1; j >= 0; j--) {
    if (found[j].texts[0].appliedParagraphStyle.name == "main_title") {
       
...

Likes

Translate

Translate
Contributor ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

app.activeDocument.metadataPreferences gives to access to the File Info. So I'm thinking you could use FindGrep() to get the contents of the first paragraph using your main_title style, and copy it to app.activeDocument.metadataPreferences.description, for example. 

Likes

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
Contributor ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

I found some language that looks like it should work ... but gives me an error, any thoughts on how to fix?

var myDoc = app.activeDocument;
var docTitle ="";
var docDesc ="";
var docKeywords ="";
var docCopyright ="";

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
found = app.activeDocument.findGrep (true);
for (j = found.length-1; j >=0 ; j--){

    
var foundText = found.contents;
    if(found.texts[0].appliedParagraphStyle.name == "main_title") {
         docTitle=found.contents;
     }
    else if(found.texts[0].appliedParagraphStyle.name == "metadata_desc") {
         docDesc=found.contents;
     }
    else if(found.texts[0].appliedParagraphStyle.name == "metadata_keywords") {
         docKeywords=found.contents;
     }
    else(found.texts[0].appliedParagraphStyle.name == "metadata_copyright") {
         docCopyright=found.contents;
     }
}

with (myDoc.metadataPreferences){
    documentTitle = docTitle;
    copyrightNotice = docCopyright;
    copyrightStatus = CopyrightStatus.yes;
    description = docDesc;
    keywords = [docKeywords];

    }

 

Likes

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
Contributor ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

Screen Shot 2021-12-16 at 9.51.02 PM.png

Likes

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
Adobe Community Professional ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

found is an arraty so you need to use the subscript operator to reference the items within the array. Try the following

var myDoc = app.activeDocument;
var docTitle = "";
var docDesc = "";
var docKeywords = "";
var docCopyright = "";

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
found = app.activeDocument.findGrep(true);
for (j = found.length - 1; j >= 0; j--) {
    if (found[j].texts[0].appliedParagraphStyle.name == "main_title") {
        docTitle = found[j].contents;
    }
    else if (found[j].texts[0].appliedParagraphStyle.name == "metadata_desc") {
        docDesc = found[j].contents;
    }
    else if (found[j].texts[0].appliedParagraphStyle.name == "metadata_keywords") {
        docKeywords = found[j].contents;
    }
    else (found[j].texts[0].appliedParagraphStyle.name == "metadata_copyright") {
        docCopyright = found[j].contents;
    }
}
with (myDoc.metadataPreferences) {
    documentTitle = docTitle;
    copyrightNotice = docCopyright;
    copyrightStatus = CopyrightStatus.yes;
    description = docDesc;
    keywords = [docKeywords];

}

-Manan

Likes

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
Contributor ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

Thank you so much! I'm still learning to understand arrays!

Likes

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
Adobe Community Professional ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

No worries, we are here to help. Happy learning.

-Manan

Likes

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
Adobe Community Professional ,
Dec 16, 2021 Dec 16, 2021

Copy link to clipboard

Copied

Another suggestion, you might wanna use the appliedParagraphStyle property of findGrepPreferences to find the text with the required paragraphstyle instead of looping on all the text in the document.

Also reset the findGrepPreferences at the end

app.findGrepPreferences = null;

 -Manan

Likes

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
Contributor ,
Dec 17, 2021 Dec 17, 2021

Copy link to clipboard

Copied

LATEST

that is a good idea! thank you!

Likes

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