Skip to content

Instantly share code, notes, and snippets.

@littlebusters
Last active December 18, 2015 13:49
Show Gist options
  • Save littlebusters/5792707 to your computer and use it in GitHub Desktop.
Save littlebusters/5792707 to your computer and use it in GitHub Desktop.
please change the extension. .js -> .jsf this function must have fillColor attribute & object named.
// Fireworks Extension: Command
// Create XML from Fill Color of Named Objects.
// version: 0.0.2
// auther: @littlebusters
// license: MIT
(function()
{
var d = fw.getDocumentDOM();
var attributes = new Array(); // objct name & fillColor
var parentIdx = new Array(); // index of parent object index
// scan fillColor in named objects
for ( var i = 0; i < d.topLayers.length - 1; i++ ) {
attributes.push( d.topLayers[ i ].name, new Array() );
findObject( d.topLayers[i].elems, i );
};
// build XML
var docTitle = d.docTitleWithoutExtension;
if( !docTitle ) {
fw.saveDocument( null );
docTitle = d.docTitleWithoutExtension;
}
if( '.fw' == docTitle.substr( -3 ) ) {
docTitle = docTitle.slice( 0, -3 );
}
var XMLString = '<panel>\n';
for ( var i = 0; i < attributes.length; i += 2 ) {
if( 0 < attributes[ i + 1 ].length ) {
XMLString += '\t<palette name="' + attributes[ i ] + '">\n';
for ( var j = 0; j < attributes[ i + 1 ].length; j += 2 ) {
XMLString += '\t\t<color hex="' + attributes[ i + 1 ][ j ] + '">\n';
XMLString += '\t\t\t<description>' + attributes[ i + 1 ][ j + 1 ] + '</description>\n\t\t</color>\n';
};
XMLString += '\t</palette>\n';
}
};
XMLString += '</panel>';
// // create XML file
var savePoint = Files.getDirectory( d.filePathForSave ) + '/' + docTitle + '.xml';
fw.textOutputEncoding = 'utf-8';
if( Files.exists( savePoint ) ) {
Files.deleteFile( savePoint );
} else {
Files.createFile( savePoint, '.xml', '');
}
var fileWrite = Files.open( savePoint, true );
fileWrite.writeUTF8( '' );
fileWrite.writeUTF8( XMLString );
fileWrite.close();
// ------------------------------------------------------------
function findObject( _obj, order )
{
var orgOrder = order;
order = order * 2 + 1;
for ( var i = 0; i < _obj.length; i++ ) {
parentIdx.push( i );
if( _obj[ i ].toString() == '[object Group]' ) {
findObject( _obj[ i ].elements, orgOrder );
} else if( _obj[ i ].toString() == '[object Layer]' ) {
findObject( _obj[ i ].elems, orgOrder );
} else {
if( _obj[ i ].pathAttributes.fill && _obj[ i ].name ) {
attributes[ order ].push( _obj[ i ].pathAttributes.fillColor );
attributes[ order ].push( _obj[ i ].name );
}
parentIdx.pop();
}
};
parentIdx.pop();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment