InDesign scripting : lesson 35

miracles happen

macgrunt has now received at least two undeniable confirmations of the medicinal properties of applescript : the first was from a generous commenter on one of the earlier lessons which you can read here ; and the second was from a user who asked for assistance with a different exporting problem (which is what this post will be about, eventually). to quote :

The joy also helped ease my sciatic nerve pain I have been suffering for more than a month now.

now, the cynical among you may think that these people are exaggerating or just being flippant — but do you really have a good and verifiable reason to doubt the written testimony of fellow mac users? not likely.

here’s the brief for the sciatica-easing workflow : export the active document to 300ppi CMYK jpegs and 150ppi RGB pngs.

the first part of the script is pretty similar to all the other exporting scripts on this site — get the filename and path of the active document, shorten the filename so that the names of the exported images won’t be too long, create some folders to hold the exported images :

-- developed by macgrunt for use with InDesign
-- last updated April 2014
-- macgrunt.com

-- export jpgs and pngs from active InDesign document

tell application id "com.adobe.InDesign"
  set mgFolder to file path of active document
  set mgDocName to name of active document
end tell

----------------------------------
-- this bit shortens filename to first space
set text item delimiters of AppleScript to {" "}
set mgDocName to text item 1 of mgDocName
set text item delimiters of AppleScript to ""
----------------------------------

----------------------------------
-- this bit creates subfolders in the same location as the InDesign file
tell application "Finder"
  if (exists folder "JPGs" of folder mgFolder) is false then
    set mgJPGfolder to make new folder at mgFolder with properties {name:"JPGs"}
  else
    set mgJPGfolder to (mgFolder & "JPGs") as string
  end if
  set mgJPGfolder to mgJPGfolder as alias
  if (exists folder "PNGs" of folder mgFolder) is false then
    set mgPNGFolder to make new folder at mgFolder with properties {name:"PNGs"}
  else
    set mgPNGFolder to (mgFolder & "PNGs") as string
  end if
  set mgPNGFolder to mgPNGFolder as alias
end tell
----------------------------------

hopefully it’s clear that mgFolder is where the InDesign file is saved, so the new JPGs and PNGs folders are created in that same folder. these new folders are created using an if/then/else routine — if you just tried to create the new folder when there was already one there, the script would throw an error — and that’s tedious.

the second half of the script sets the export parameters and does the actual exporting — pretty simple :

tell application id "com.adobe.InDesign"
  ----------------------------------
  -- here is where we set the export parameters
  set Exporting Spread of JPEG export preferences to false
  set JPEG export range of JPEG export preferences to export range
  set JPEG Quality of JPEG export preferences to maximum
  set jpeg color space of JPEG export preferences to CMYK
  set export resolution of JPEG export preferences to 300
  
  set Exporting Spread of PNG export preferences to false
  set PNG export range of PNG export preferences to export range
  set PNG Quality of PNG export preferences to maximum
  set PNG color space of PNG export preferences to RGB
  set export resolution of PNG export preferences to 150
  ----------------------------------
  
  repeat with mgCount from 1 to (count pages of active document)
    set mgCount to mgCount as string
    set Page String of JPEG export preferences to mgCount
    set Page String of PNG export preferences to mgCount
    set mgJpgFilePath to (mgJPGfolder & mgDocName & "_" & mgCount & ".jpg") as string
    set mgPNGFilePath to (mgPNGFolder & mgDocName & "_" & mgCount & ".png") as string
    tell active document
      export format JPG to mgJpgFilePath without showing options
      export format PNG format to mgPNGFilePath without showing options
    end tell
  end repeat
  
  display dialog "ALL DONE!" buttons {"Okey-dokey"} default button 1
end tell

this block of code highlights one of the frustrating things about trying to write applescript — the inconsistency of syntax. notice how ‘jpeg color space’ is not capitalised like all the other export preference properties? that’s not really a big deal because the script editor will correct that automatically if you accidentally type it in caps.

but notice the two export commands : ‘export format JPG’ vs ‘export format PNG format’ — why do they have different structures? who knows — adobe are just weird. it’s even weirder when you include the two PDF export commands : ‘export format PDF type’ vs ‘export format interactive PDF’. best not to think about it.

that repeat loop at the end is purely to get consistency of filenaming. if we just export a document “mgThis_file.indd” to jpegs, the filenames would be “mgThis_file.jpg”, “mgThis_file2.jpg”, “mgThis_file3.jpg”, etc. again with the inconsistency … why doesn’t InDesign add ‘1’ to that first jpg? — adobe really do have some serious explaining to do.

anyway, as we were adding a repeat loop to include ‘1’ on the first image, we took the opportunity to add an undescore as well. so these files would now come out as “mgThis_file_1.jpg”, “mgThis_file_2.jpg”, “mgThis_file_3.jpg”, etc.

obviously, this is a very basic script which exports a single document at a time, using one set of export preferences. but like all the PDF export scripts on this site, it doesn’t take much to adapt it to export all open documents, or give the user the chance to choose export preferences.

keep grunting.

macgrunt icon

Advertisement

thoughtful and respectful comments welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s