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

Script Help

Community Expert ,
Feb 21, 2023 Feb 21, 2023

Copy link to clipboard

Copied

I have two scripts that are just not working 100% of the time - namely I think on files that are converted.

 

Is there a better way to implement these scripts?

 

 

First Script 

 

Check Embed - this should check if the file has all the links embedded when the file is saved - but it is not working all the time

 

#targetengine "session"

// Attach a listener to the beforeSave event
app.addEventListener("beforeSave", function (event) {
var doc = app.activeDocument;

// Get all the links in the document
var links = doc.links;

var unembeddedLinks = [];

// Loop through each link and check if it's embedded
for (var i = 0; i < links.length; i++) {
if (links[i].status != LinkStatus.LINK_EMBEDDED) {
unembeddedLinks.push(links[i]);
}
}

// If there are any unembedded links, prompt the user
if (unembeddedLinks.length > 0) {
// Create a message string with the number of unembedded links
var message = "There are " + unembeddedLinks.length + " unembedded links. Please ensure they are embedded before saving.";

// Display an alert dialog box to the user
alert(message);
}
});

 

Second Script

This is in the Startup Scripts folder - and it should check on opening a file if the slug is set to a minimum 130mm.

 

But it doesn't work on all files and I've noticed on files that are being converted.

 

 

#targetengine "session"
app.addEventListener("afterOpen", checkSlugBounds, false);

function checkSlugBounds(e) {
//set the script’s units to millimeters
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
//checks if the event’s parent is a Layout Window—the document is fully open
if (e.parent.constructor.name == "LayoutWindow") {
//the document is the layout window’s parent
var doc = e.parent.parent
var slugBounds = doc.documentPreferences.slugInsideOrLeftOffset;
if (slugBounds < 130) {
alert("The left slug area is less than 130mm.");
}
}
//reset units
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
};

TOPICS
How to , Scripting

Views

2.9K

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 ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

I just want it to work all the time. However that works I'm willing to accept.

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 ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

 

(That's an interesting question, but it seems distinct from the OP.)

 

Both of Eugene’s scripts seem to be breaking when the file has been converted. For the embedding script your idea of afterSaveAs seems to work.

 

Eugene see if this works, instead of an alert I’m displaying a dialog with the option to embed everything or cancel when there are any linked files (Edit: need both afterSave and afterSaveAs listeners)

 

 

 

#targetengine "embedAll"
app.addEventListener("afterSave", checkEmbedding);
app.addEventListener("afterSaveAs", checkEmbedding);

var res, d;
function checkEmbedding(e) {
    d = app.dialogs.add({name:"Dialog", canCancel:true});
    with(d){
        with(dialogColumns.add()){
            with(dialogRows.add()){
                staticTexts.add({staticLabel:"Some Links are not embedded, would you like to embed all links?", minWidth: 250});
            }
        }
    }
    var doc = e.parent;
    var l = doc.links
    var c = 0
    for (var i = 0; i < l.length; i++){
        if (l[i].status != LinkStatus.LINK_EMBEDDED) {
            c++
        } 
    };
    if (c) {
        res = d.show();
        if (res) {
            e.parent.links.everyItem().unlink()
        } 
    }
}


 

 

 

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 ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Gonna test this today - apologies I have been really busy for the last few days so put this on the backburner

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 ,
Mar 03, 2023 Mar 03, 2023

Copy link to clipboard

Copied

@rob day this won't let me cancel - only the OK button works.

 

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 ,
Mar 03, 2023 Mar 03, 2023

Copy link to clipboard

Copied

LATEST

Hi Eugene, not sure why you can’t cancel, it’s working for me. Might be because I’m not destroying the dialog. Try this:

 

#targetengine "embedAll"
app.addEventListener("afterSave", checkEmbedding);
app.addEventListener("afterSaveAs", checkEmbedding);

var res, d;
function checkEmbedding(e) {
    d = app.dialogs.add({name:"Dialog", canCancel:true});
    with(d){
        with(dialogColumns.add()){
            with(dialogRows.add()){
                staticTexts.add({staticLabel:"Some Links are not embedded, would you like to embed all links?", minWidth: 250});
            }
        }
    }
    var doc = e.parent;
    var l = doc.links
    var c = 0
    for (var i = 0; i < l.length; i++){
        if (l[i].status != LinkStatus.LINK_EMBEDDED) {
            c++
        } 
    };
    if (c) {
        res = d.show();
        if (res) {
            e.parent.links.everyItem().unlink()
        } else{
            d.destroy();
        }
    }
}

 

 

Or, we could add a checkbox to the dialog to make the choice more explicit:

 

#targetengine "embedAll"
app.addEventListener("afterSave", checkEmbedding);
app.addEventListener("afterSaveAs", checkEmbedding);

var res, d;
function checkEmbedding(e) {
    d = app.dialogs.add({name:"Link Embedding", canCancel:true});
    with(d){
        with(dialogColumns.add()){
            staticTexts.add({staticLabel:"Embed all links:"});
        }
        with(dialogColumns.add()){
            ck = checkboxControls.add({checkedState:false, minWidth:150});
        }
    }
    var doc = e.parent;
    var l = doc.links
    var c = 0
    for (var i = 0; i < l.length; i++){
        if (l[i].status != LinkStatus.LINK_EMBEDDED) {
            c++
        } 
    };
    if (c) {
        res = d.show();
        if (res) {
            ck = ck.checkedState;
            if (ck) {
                e.parent.links.everyItem().unlink()
            } else {
                d.destroy();
            }
        } else{
            d.destroy();
        }
    }
}

 

 

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 ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Thanks Marc - I'll try it.

 

Just want to point out that InDesign Preflight Options can check the slug area - but it doesn't work correctly for me. I set. the values in the preflight and get a warning all the time. 

If the Preflight worked I wouldn't need a script 😄

 

Screenshot 2023-02-27 at 08.46.08.png

Screenshot 2023-02-27 at 08.47.01.png

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 ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Ok the Check Embed one seems to work

But it won't save the document unless the images are embedded which is a problem.

 

Check Slug Area - worked ok but stops working on a converted document for some reason.

That is - it's not working all the time - maybe because both scripts are in the startup folder and would be conflicting?

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 ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Just removed the check embed - the slug script doesn't check all the time. Slug script - I converted a doc to 2019 - reduced the slug area to below 130mm. 
Then opened in InDesign 2022 and it opened with a warning. Saved it. Closed it. Opened it again and don't get the warning.

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 ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Ok the Check Embed one seems to work

But it won't save the document unless the images are embedded which is a problem.

 

Hi Eugene, Is the problem with Marc’s script or the last one I posted, which listens for afterSave and afterSaveAs?

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 ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

It was with Marcs script.

I'll be testing your script version later. Just got bogged down with some work for the moment.

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 ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

On the check slug script, instead of afterOpen could you use beforeClose? Seems like the problem with converted files is there’s a delay as the file is being converted on an open.

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