Skip to content

Instantly share code, notes, and snippets.

@mariovalney
Last active November 13, 2019 19:20
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 mariovalney/e525789c84f44e5026e90b2684498b44 to your computer and use it in GitHub Desktop.
Save mariovalney/e525789c84f44e5026e90b2684498b44 to your computer and use it in GitHub Desktop.
This script is designed to be used as a script that runs after a save event. It's based on default 'Save Extra JPEG' and will save a PNG next to the current active if 'X-Plane' or 'Aircraft' are presented on path. This script does not handle 'as a copy' when saving.
/**
* Developed by Mário Valney <mariovalney@gmail.com>
*
* Inspired on "Save Extra JPEG" and created using:
* https://www.adobe.com/content/dam/acom/en/devnet/photoshop/pdfs/photoshop_scriptref_js.pdf
*/
/**
* @@@BUILDINFO@@@ Save PNG 4 XP.jsx 1.0.0 Thu Oct 24 2019 19:21:44 GMT-0300
*/
var begDesc = "This script is designed to be used as a script that runs after a save event. It's based on default 'Save Extra JPEG' and will save a PNG next to the current active if 'X-Plane' or 'Aircraft' are presented on path. This script does not handle 'as a copy' when saving."
var begName = "Save PNG 4 XP"
try {
if ( ! IsSaveEvent( arguments[0] ) ) {
alert( 'Save PNG 4 XP should only be used with the Save Document event. Aborting...' );
throw( "DONE" );
}
var data = GetDataFromDocument( activeDocument );
/**
* Check it's inside XP environment and if the current file
* is not already a PNG one. Then save PNG.
*/
if ( ! data.fromXP || 'png' == data.extension.toLowerCase() || 'PNG' == data.fileType ) {
return;
}
if ( UsingAsACopy( arguments[0] ) ) {
alert( 'Saving "As A Copy" may not save the X-Plane PNG correctly.' );
}
SavePNG4XP( data );
} catch( e ) {
// Remove comments below to see error for debugging
// alert( e );
}
/**
* Will save the current document as a copy using PNG.
* - Compression: none
* - Interlaced: none
*
* @param {object} Return of GetDataFromDocument function
*/
function SavePNG4XP( data ) {
var options = new PNGSaveOptions();
options.compression = 0;
options.interlaced = false;
// Check we are using extension today
var extension = ( "" == data.extension ) ? '' : '.png';
// Third option is as a copy, set that to true
// so the activeDocument doesn't switch underneath the user
activeDocument.saveAs(
File( data.folder + '/' + data.fileName + extension ),
options,
true
);
}
/**
* Find the options "as a copy" is active.
*
* @param {ActionDescriptor}
* @return boolean
*/
function UsingAsACopy( actionDescriptor ) {
if ( undefined == actionDescriptor || "ActionDescriptor" != actionDescriptor.typename ) {
return false;
}
var keyCopy = charIDToTypeID( "Cpy " );
if ( ! actionDescriptor.hasKey( keyCopy ) ) {
return false;
}
return actionDescriptor.getBoolean( keyCopy );
}
/**
* Check the description is about "saveStage" but not "saveBegin".
*
* @param {ActionDescriptor}
* @return boolean
*/
function IsSaveEvent( actionDescriptor ) {
if ( undefined == actionDescriptor || "ActionDescriptor" != actionDescriptor.typename ) {
return false;
}
var keySaveStage = stringIDToTypeID( "saveStage" );
if ( ! actionDescriptor.hasKey( keySaveStage ) ) {
return false;
}
var typeSaveStage = actionDescriptor.getEnumerationType( keySaveStage );
var typeSaveStageType = stringIDToTypeID( "saveStageType" );
var enumSaveStage = actionDescriptor.getEnumerationValue( keySaveStage );
var enumSaveStageBegin = stringIDToTypeID( "saveBegin" );
return enumSaveStage != enumSaveStageBegin || typeSaveStage != typeSaveStageType;
}
/**
* Get data from Document to be used on script
*
* @param {Document} The activeDocument
* @return object {
* folder {string}
* fileName {string}
* fileType {string}
* extension {string}
* fromXP {boolean} True if we have X-Plane or Aircraft on document path
* }
*/
function GetDataFromDocument( inDocument ) {
var fullPathStr = inDocument.fullName.toString();
var lastDot = fullPathStr.lastIndexOf( "." );
var fileNameNoPath = fullPathStr.substr( 0, lastDot );
var lastSlash = fullPathStr.lastIndexOf( "/" );
var data = new Object();
data.folder = fileNameNoPath.substr( 0, lastSlash );
data.fileName = fileNameNoPath.substr( lastSlash + 1, fileNameNoPath.length );
data.fileType = inDocument.fullName.type;
data.extension = fullPathStr.substr( lastDot + 1, fullPathStr.length );
data.fromXP = fullPathStr.lastIndexOf( "X-Plane" ) > -1 || fullPathStr.lastIndexOf( "Aircraft" ) > -1;
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment