Skip to content

Instantly share code, notes, and snippets.

@indiscripts
Created January 16, 2023 10:20
Show Gist options
  • Save indiscripts/4fa2401237cefba65e6dc53336d6d8e7 to your computer and use it in GitHub Desktop.
Save indiscripts/4fa2401237cefba65e6dc53336d6d8e7 to your computer and use it in GitHub Desktop.
InDesign script: change Page in placed PDF (selection)
;(function selPlacePDFPage( dt,doc,sel,t,ok,ff)
//----------------------------------
// Simple interface for changing page in placed PDF.
// (Just select the PDF container and run.)
{
dt = +new Date;
try
{
doc = app.properties.activeDocument;
if( !doc ) throw "No active document.";
sel = doc.properties.selection;
if( !(sel && sel.length) ) throw "No selection.";
if( !(sel=sel[0]).isValid ) throw "Invalid selection.";
for( t=sel ; 'PDF' != t.constructor.name ; t=t[0] )
{
ok = t.hasOwnProperty('contentType')
&& ContentType.graphicType == t.properties.contentType
&& 1 == (t=t.properties.allGraphics||[]).length;
if( !ok ) throw "The selection has no direct PDF content.";
}
ff = (t=t.properties.itemLink||0).isValid && (t.properties.filePath||0);
if( !(ff && File(ff).exists) ) throw "PDF File is missing. You probably should relink.";
// Runtime error if canceled by the user (hence the `dt` timer.)
// ---
sel.place(ff, true);
alert( "PDF page sucessfully placed!" );
}
catch( e )
{
dt = +new Date - dt; // full delay, in ms.
if( 500 > dt ) alert( e ); // Only display instant error.
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment