Last active
February 22, 2023 20:32
-
-
Save indiscripts/b13f81317b0462a9e4b6953d38a9efc9 to your computer and use it in GitHub Desktop.
InDesign script: Prompts a warning if some embedded links exist in the doc before saving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(/*any*/ev, ff,t,cond,s,z) | |
//---------------------------------- | |
// [This InDesign script CAN be used as a startup script.] | |
// Prompts a warning if some UNembedded links exist in the doc; | |
// the save/saveAs task is then interrupted. | |
// [FIX230222] Made a confusion on the script purpose: | |
// now detect UNembedded links! | |
// Targeted events: beforeSave|beforeSaveAs | |
{ | |
const EMBD = LinkStatus.LINK_EMBEDDED.toString(); | |
if( (ev||0).isValid ) | |
{ | |
cond = 0===ev.eventType.indexOf('beforeSave') // Make sure you're managing the right event | |
&& (!app.modalState) // app.modalState would cause `alert` to fail. | |
&& ev.fullName // Safer: the target File should be known at this point. | |
&& (t=ev.target).isValid // Valid document required. | |
&& (t=t.links).length // No need to work if there are no links. | |
; | |
if( cond ) | |
{ | |
s = ('§'+t.everyItem().status.join('§')).replace(RegExp('§'+EMBD,'g'),''); | |
z = -1 + s.split('§').length ; | |
if( z > 0 ) | |
{ | |
alert( "There are " + z + " un-embedded links. Please ensure they are embedded before saving." ); | |
ev.preventDefault(); | |
} | |
} | |
} | |
else | |
{ | |
// Install the event listener if not yet installed. | |
// --- | |
const UID = 'myCheckEmbedHandler'; | |
(ff=File($.fileName)).exists | |
&& !((t=app.eventListeners).itemByName(UID)).isValid | |
&& | |
( | |
(t.add('beforeSave',ff)).name=UID, | |
(t.add('beforeSaveAs',ff)).name=UID+'2' | |
); | |
} | |
})($.global.evt); |
Should work now!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oups: my original code identifies embedded links while the expected behavior is, on the contrary, to point out the unembedded links. Well, that's the complementary count! Can be easily fixed…