Skip to content

Instantly share code, notes, and snippets.

@indiscripts
Last active February 22, 2023 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indiscripts/b13f81317b0462a9e4b6953d38a9efc9 to your computer and use it in GitHub Desktop.
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
(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);
@indiscripts
Copy link
Author

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…

@indiscripts
Copy link
Author

Should work now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment