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

Script: Check for bookmark, add bookmark name to export filename

Community Beginner ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

I've been working on a script that does three things:

1. Exports all pages as pngs
2. Adds the page dimensions at the end of the file names

3. If there is a bookmark on a page, the name of that bookmark is added to the file name for that page.

 

I'm not a scripter and this is based of other scripts, but what I'm struggling with is no. 3. I'm not really sure how to set up a working if-statement that somehow checks for bookmarks on each page.

Thankful for any suggestions/solutions.

 

main();
function main() {


	//Suffix
    var mySuffix = '';
    if (mySuffix == "") {
        mySuffix = prompt("Ev. språkversion", mySuffix);
    }

    if (mySuffix != "") {
        mySuffix = "_" + mySuffix  //add underscore separator
    }
    
    
    // Variables
    var SysFolder = Folder.selectDialog(); //prompts normal save as location chooser dialog

    path = SysFolder + "\/"; // have to add a slash to end of the path to get the file to actually save INSIDE this folder

    var myDoc = app.activeDocument;

    var pageLabel = "";


	// Export settings
    app.pngExportPreferences.pngExportRange = PNGExportRangeEnum.EXPORT_RANGE;
    app.pngExportPreferences.exportingSpread = false;
    app.pngExportPreferences.antiAlias = true;
    app.pngExportPreferences.transparentBackground = false;
    app.pngExportPreferences.exportResolution = 72;
    app.pngExportPreferences.pngQuality = PNGQualityEnum.MAXIMUM;
    app.pngExportPreferences.pngColorSpace = PNGColorSpaceEnum.RGB;
	


for(var i = 0; i < myDoc.pages.length; i++) {

        app.pngExportPreferences.pageString = myDoc.pages[i].name;

        myPage = myDoc.pages[i];
        myWidth =   myPage.bounds[3] -   myPage.bounds[1];  
        myHeight =   myPage.bounds[2] -   myPage.bounds[0];  


		if (myDoc.bookmarks[i].destination.destinationPage.name == myDoc.pages[i].name) {
        pageLabel = "_" + myDoc.bookmarks[i].name
	    }


        var myPath = path + myDoc.name.substring(0, (myDoc.name.search(".indd"))) + pageLabel + mySuffix + "_" + myWidth + "x" + myHeight + ".png";

        var myFile = new File(myPath);

        myDoc.exportFile(ExportFormat.PNG_FORMAT, myFile, false);
    }

}

 

TOPICS
Import and export , Scripting

Views

450

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

Community Expert , Jan 25, 2021 Jan 25, 2021

Hi,

Try following snippet. I have not used your existing script buyt edited mine that I alreday have. Give it a try

var doc = app.activeDocument;
var docName = doc.name.split('.')[0];
var _pages = doc.pages;
var _destinationFolder = Folder.selectDialog("Select folder for export");
var _bookMarks = doc.bookmarks;
if (_destinationFolder != null) {
    for (var p = 0; p < _pages.length; p++) {
        var _width = Math.round(_pages[p].bounds[3] - _pages[p].bounds[1]);
        var _height = Math.rou
...

Votes

Translate

Translate
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Hi,

Try following snippet. I have not used your existing script buyt edited mine that I alreday have. Give it a try

var doc = app.activeDocument;
var docName = doc.name.split('.')[0];
var _pages = doc.pages;
var _destinationFolder = Folder.selectDialog("Select folder for export");
var _bookMarks = doc.bookmarks;
if (_destinationFolder != null) {
    for (var p = 0; p < _pages.length; p++) {
        var _width = Math.round(_pages[p].bounds[3] - _pages[p].bounds[1]);
        var _height = Math.round(_pages[p].bounds[2] - _pages[p].bounds[0]);
        var _pageName = _pages[p].name;
        var _pageLabel = ''
        for (var b = 0; b < _bookMarks.length; b++) {
            if (_bookMarks[b].destination.destinationPage.name == _pageName) {
                _pageLabel = _bookMarks[b].name;
                break;
            }
        }
        if (_pageLabel != '') {
            var _file = _destinationFolder + "/" + docName + "_" + _pageLabel + "_" + _width + "x" + _height;
        }
        else {
            var _file = _destinationFolder + "/" + docName + "_" + _width + "x" + _height;
        }
        app.pngExportPreferences.pngExportRange = ExportRangeOrAllPages.EXPORT_RANGE;
        app.pngExportPreferences.pageString = _pageName;
        app.pngExportPreferences.antiAlias = true;
        app.pngExportPreferences.transparentBackground = false;
        app.pngExportPreferences.exportResolution = 72;
        app.pngExportPreferences.pngQuality = PNGQualityEnum.MAXIMUM;
        app.pngExportPreferences.pngColorSpace = PNGColorSpaceEnum.RGB;
        try {
            doc.exportFile(ExportFormat.PNG_FORMAT, File(_file), false);
        }
        catch (e) { }
    }
    alert("Exported Successfully!")
}

 

Best regards

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
Community Beginner ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Nice! I was able to add my own small modifications. This will save me A LOT of time. Thanks!

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
Community Expert ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

LATEST

Great!

You are welcome. 🙂

Best regards

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