Skip to main content
Braniac
February 21, 2023
Question

Script Help

  • February 21, 2023
  • 3 replies
  • 4439 views

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;
};

This topic has been closed for replies.

3 replies

Marc Autret
Braniac
February 22, 2023

Hi @Eugene Tyson 

 

Quick note.

 

The Second script (check-embed) might be slightly improved and made safer, I think:

→ https://gist.github.com/indiscripts/b13f81317b0462a9e4b6953d38a9efc9

 

(EDIT: I made a confusion on the purpose of the script, the fixed code now detects and reports UN-embedded links!)

 

I'm not sure what was wrong with the 1st script (check-slug), but you can use a very similar template:

→ https://gist.github.com/indiscripts/518d3dbe8570548055ddc4d5d91150e6

 

In both cases the jsx can be used in the `startup scripts` folder (not required but usually the desired behavior).

 

Note to my colleagues. — The solutions I suggest do not use the #targetengine directive (although it's the usual way to handle ID events). Using the script file itself as the event handler is a different approach that turns out to be much more flexible during debugging, and it doesn't cost more execution time on such small pieces of code.

 

Best,

Marc

rob day
Braniac
February 22, 2023

Hi Marc, If I run this:

 

app.addEventListener("afterOpen", saveConverted);

function saveConverted(e) {
    if (e.parent.constructor.name == "LayoutWindow") {
        var doc = e.parent.parent;
        if (doc.converted) {
            doc.save (File(doc.filePath + "/" + doc.name))
        }
    }
}

 

I get this error if the opened file is being converted. Seems like there needs to be a way to wait for the conversion to finish:

 

 

rob day
Braniac
March 3, 2023

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

 


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();
        }
    }
}

 

 

rob day
Braniac
February 21, 2023

Hi Eugene, I checked the 2nd script with a file that needed to be converted, and it worked, but that may have been because the file was small and didn’t take much time to convert. You might need to use an idle task event to wait for the conversion. Can you share a file that doesn’t work?

Braniac
February 21, 2023

I can't share the files unfortunately - and it's so random I can't really put my finger on it. 

 

You might need to use an idle task event to wait for the conversion. 

How would I do this?

 

add this to the end

 

// add an idle event task

var idleTask = app.idleTasks.add({name: "checkSlugBoundsIdleTask", sleep: 500}); idleTask.addEventListener(IdleEvent.ON_IDLE, checkSlugBounds); };

 

rob day
Braniac
February 21, 2023

Did you try renaming to #targetengine to a unique name?

saschak38136531
Known Participant
February 21, 2023

I'm not sure - but maybe there is a problem when both script's targetengine is called "session".

Try to rename one of them. 

Script 1: #targetengine "session1"

Script 2: #targetengine "session2"

I hope that helps. But I'm not sure.

Braniac
February 21, 2023

Ok thanks I'll try

 

rob day
Braniac
February 21, 2023

@saschak38136531 might be right. you can name the targetengine anything—it could be "checkSlug". Restart ID after making the change