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;
};
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Ok thanks I'll try
Copy link to clipboard
Copied
@saschak38136531 might be right. you can name the targetengine anything—it could be "checkSlug". Restart ID after making the change
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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); };
Copy link to clipboard
Copied
Did you try renaming to #targetengine to a unique name?
Copy link to clipboard
Copied
Yeh I've done that. Going to test over few days. But first file didn't work until I double clicked the script. Then it works. This is for slug area.
Copy link to clipboard
Copied
On the script that checks the link status, you are using the reserved JS object name links as a variable name and that might be a problem.
var links = doc.links;
See if this works—I’m using l as the variable name, and a simple counter rather than storing the unembedded links in an array:
#targetengine "checkembedded"
app.addEventListener("beforeSave", checkEmbedding);
function checkEmbedding(e) {
var doc = e.parent;
var l = doc.links;
var c = 0 //a counter for counting un embedded links
for (var i = 0; i < l.length; i++){
if (l[i].status != LinkStatus.LINK_EMBEDDED) {
c++
}
};
if (c) {
alert(c + " unembedded link(s). Please embed all links before saving.")
}
}
Copy link to clipboard
Copied
It didn't work on a converted document.
I saved it - opened the document again
Made some minor changes - saved - and still didn't work.
I then double click the script - and it works from then.
But it seems to be 'turning off' under certain circumstances - or at least 'refusing to run the check'.
I don't get it.
Copy link to clipboard
Copied
Maybe there’s a problem with your startup scripts folder(s), which folder are you using?
Applications ▸ Adobe InDesign 20XX ▸ Scripts ▸ startup scripts
or
User ▸ Library ▸ Preferences ▸ Adobe InDesign ▸ Version X.0 ▸ en_US ▸ Scripts ▸ startup scripts
Have you tried cleaning out both folders except for the one script to make sure there isn’t a conflict?
Copy link to clipboard
Copied
Folder is
Library/Preferences/Adobe InDesign/Version XX.0/en_US/Scripts/Scripts Panel
and
Library/Preferences/Adobe InDesign/Version XX.0/en_US/Scripts/Scripts Panel/Startup Scripts
there's only 1 script in Startup for the slug bounds.
Copy link to clipboard
Copied
Also, if you really do want to enforce embedding on every save, there’s this:
#targetengine "embedAll"
app.addEventListener("beforeSave", checkEmbedding);
function checkEmbedding(e) {
e.parent.links.everyItem().unlink(); //the unlink() function embeds the link
}
Copy link to clipboard
Copied
I thought about this for a while. It's really just to warn if not embedded.
As it there's more to think about than just embedding on save.
It's a reminder that the links need to be embedded - but you may not want to embed at that time.
Copy link to clipboard
Copied
And, I just realized the problem with the beforeSave listener and a converted ID file would be, a converted file has no file path, so you are actually doing a Save As not a Save—the event doesn’t get triggered with a Save As.
You could listen for both:
#targetengine "embedAll"
app.addEventListener("beforeSave", checkEmbedding);
app.addEventListener("beforeSaveAs", checkEmbedding);
Copy link to clipboard
Copied
I've added this - and the same thing happens.
It doesn't start working until I double click it.
Should it be in the Startup Scripts folder?
I was told by someone not to put it there. But maybe that was wrong.
Copy link to clipboard
Copied
It doesn't start working until I double click it.
Should it be in the Startup Scripts folder?
Yes, if you don't put the script into the startup scripts folder, the script will not start when indesign starts up.
Copy link to clipboard
Copied
I just checked a converted file save and it doesn’t trigger either the beforeSave or beforeSaveAs event, so I don’t think it’s a problem with the startup scripts folder. If you are OK with overwriting a converted file you could force the save converted with an afterOpen event.
Copy link to clipboard
Copied
afterOpen also doesn’t work because you can’t get at the open document until the conversion is complete.
Copy link to clipboard
Copied
That would work - no problem with that
Copy link to clipboard
Copied
I haven’t been having the same problems with converted files because I have this script assigned to Command-S, which automatically overwrites converted files by forcing a save. See if assigning this to Command-S works for you:
var doc = app.activeDocument;
var fp = doc.filePath;
var n = doc.name;
if (doc.converted) {
doc.save (File(fp + "/" + n))
} else {
doc.save()
}
Copy link to clipboard
Copied
Ah ok - I'll give it a try - thank you.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
Indeed you cannot save a document from within an opening event handler (no matter whether it's being converted, BTW); afterOpen is probably not the right place to execute such a command. I don't think that afterOpen means that the doc is ready for further processing right now, that's just a non-cancelable event that needs to terminate. It is usually fired on two distinct targets, the Document being opened and the LayoutWindow where the opened document shows up. I would use an IdleTask (need to be tested though) to trigger a particular process once the event loop is completed. Maybe the afterOpen handler is anyway the optimal location to initiate such an IdleTask…
(That's an interesting question, but it seems distinct from the OP.)
Best,
Marc