2009-10-07

Find the Scripts Panel Folder

Part of a Script to Install Scripts

I've been thinking for some time of a foolproof method of installing scripts at remote sites. Almost all of the script writing I do is done for sites that I never get to visit. The sites contract me to do the automation and I send the scripts and the invoice. Generally the installation process isn't a problem, but some sites have never looked inside the Adobe InDesign or Adobe InCopy application folder to see where the scripts get installed. And some sites don't have any managed approach to installing scripts other than walking around to each machine (it is actually hard for me to imagine that, but it is true). So to make things easier, I've wanted a script that would either self install or an installer script would do the installation.
This generic function is the start of that process. If your script contained something like:

#target indesign
var scriptsPanelFolder = findScriptsPanelFolder() ;

Then that 'scriptsPanelFolder' variable would contain a Folder object for the Adobe InDesign CS3 or CS4 Scripts Panel folder. Remember this isn't 'true' JavaScript where working with folders isn't allowed, this is ExtendScript where working folders is encouraged.

From that location, it would be easy to go up one more parent and test for the 'Startup Scripts' if you needed to install something there.

By the way, add a line like this:

scriptsPanelFolder.execute()

And that scripts panel folder will open. And that .execute() method works in Windows or Mac OS. It is both an ExtendScript File and Folder object method.


//
function findScriptsPanelFolder () {
//-------------------------------------------------------------------------
//-- F I N D S C R I P T S P A N E L F O L D E R
//-------------------------------------------------------------------------
//-- Generic: Yes, for both Mac OS and Windows
//-- any version of ExtendScript, but only for
//-- the CS3 and CS4 versions of Adobe InCopy or Adobe InDesign
//-------------------------------------------------------------------------
//-- Purpose: to locate the Scripts Panel for Adobe InCopy or Adobe
//-- InDesign when a script is launched from an unknown location
//-- (like the downloads folder). The reason is that if you want
//-- to use a script to install a script, this is a necessary part
//-- of the process. This function requires the user of a
//-- #target incopy
//-- or
//-- #target indesign
//-- line in the script file calling this function. Without that
//-- there will be nothing to signal the script where to start
//-- looking.
//-------------------------------------------------------------------------
//-- Returns: A Folder object for the Scripts Panel folder
//-------------------------------------------------------------------------
//-- Calls: Itself, the function is recursive.
//-------------------------------------------------------------------------
//-- Sample Use:
//~ var scriptsPanelFolder = findScriptsPanelFolder() ;
//-------------------------------------------------------------------------
//-- Written by Jon S. Winters starting on 2009.07.18
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------

//-- If the function was not passed any arguments, then start at what
//-- ExtendScript calls the 'startup' folder. This will be several
//-- layers deep within the Adobe Application.
//-- However, if the function was passed an argument there will be one
//-- and we want to use it as the starting point. This allows the
//-- funciton to call itself recursively. See the last line of the
//-- function to see the recursive call.
//-- Note: Wanted to use the alternative form of the If statement,
//-- but it wouldn't post on the blog, so the long form is here.
if ( arguments.length == 0 ) { var currentFolder = Folder.startup }
else { var currentFolder = Folder ( arguments[0] ) }


//-- Calculate the path for the 'Scripts Panel' folder from the curentFolder.
//-- Then check to see if that folder exists. If it exists, return to the caller.
var possible = Folder ( unescape ( currentFolder + '/Scripts Panel' )) ;
if ( possible.exists ) return possible ;
//-- implied else;
//-- If here, look one layer up from the current folder. Do this by
//-- calling the function recursively.
return findScriptsPanelFolder ( currentFolder.parent )
}
//

2009-10-05

Safe for XML files

I've been working more and more with creating and parsing XML files with ExtendScript. Came across a problem when trying to write a string that was enclosed in < and >
ExtendScript complained when asked to convert the file's contents to an XML reference that there was a missing tag. And it was correct, at least for its way of thinking.
So below is the quick little function to parse a string to convert the 4 reserved characters into their entity references necessary to write them.

Note, publishing this requires that a bunch of things get further escaped to post. I've got no idea how it will copy back out.


function entityReference ( str ) {
//-------------------------------------------------------------------------
//-- E N T I T Y R E F E R E N C E
//-------------------------------------------------------------------------
//-- Generic: Yes. Works for ExtendScript and likely for all ECMAScript
//-- and even JavaScript providing the Regular Expression converts.
//-------------------------------------------------------------------------
//-- Purpose: To replace the XML Reserved Characters in a passed string
//-- with their Entity References
//-------------------------------------------------------------------------
//-- Arguments: A string to clean up.
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: The string with the reserved characters replaced with their
//-- entitity references
//-------------------------------------------------------------------------
//-- Sample Use:
//~ var unfitForXML = '< open & ampersand > close % percent'
//~ var safeForXML = entityReference ( unfitForXML ) ;
//-------------------------------------------------------------------------
//-- Notes:
//-- 1) You can add any other cleanups you need for your particular XML
//-- 2) Be wary of using something to excape the entity references
//-- prior to sending to this function as you will escape all the
//-- ampersands have have areal mess in the XML.
//-------------------------------------------------------------------------
//-- Written: 2009.10.04 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- replacing each individually to make it more portable and easier to read
str = str.replace ( new RegExp ('&' , 'gm' ) , '&amp;' ) ;
str = str.replace ( new RegExp ('>' , 'gm' ) , '&gt;' ) ;
str = str.replace ( new RegExp ('<' , 'gm' ) , '&lt;' ) ;
str = str.replace ( new RegExp ('%' , 'gm' ) , '&#37;' ) ;
//-- return to caller
return str ;
}
//