Created
June 18, 2012 03:32
-
-
Save bugsysop/2946714 to your computer and use it in GitHub Desktop.
An InDesign CS3--CS5 JavaScript. Writes the current find/change preferences to a text file so that you can copy/paste them into a find/change list file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RecordFindChange_CS3-CS5.jsx | |
// | |
// An InDesign CS3--CS5 JavaScript | |
// Writes the current find/change preferences to a text file so that | |
// you can copy/paste them into a find/change list file. | |
// Kasyan Servetsky - http://kasyan.ho.com.ua/scripts_by_categories.html | |
String.prototype.cleanProps = function () | |
{ | |
var s = this; | |
// split parameters | |
s = s.replace( /, /g, '\r' ); | |
// delete breaks at start and end | |
s = s.replace( /^\({/, '' ); | |
s = s.replace( /\}\)$/, '' ); | |
// delete undefined | |
s = s.replace( /[\w]+:1851876449\r/g, '' ); | |
s = s.replace( /[\w]+:\"\"\r/g, '' ); | |
s = s.replace( /[\w]+:null\r/g, '' ); | |
// delete some dirty or useless parameters | |
s = s.replace( /[^\r]+\/@find-[^\r]+\r+/g, '' ); | |
s = s.replace( /[^\r]+\/@change-[^\r]+\r+/g, '' ); | |
s = s.replace( /parent:resolve\(\"\/\"\)/g, '' ); | |
s = s.replace(/\b\w+\b\:\(\{\}\)/g,''); | |
// column or single line | |
if ( _param.single_line == true ) | |
{ | |
s = s.replace( /\r\r*/g, ', ' ); | |
s = '{' + s + '}'; | |
s = s.replace( /, }/g, '}' ); | |
} | |
else | |
s = s.replace( /\r\r+/g, '\r' ); | |
return s; | |
} | |
// =================== Preferences / Voreinstellungen ================ | |
var _param = get_param(); | |
app.documents.firstItem().insertLabel('FindChangeByList', _param.toSource()); | |
// =============================================================== | |
// =============================================================== | |
// main / Hauptprogramm | |
// =============================================================== | |
var myResult; | |
var myFindTextProperties = app.findTextPreferences.properties.toSource().cleanProps(); | |
var myChangeTextProperties = app.changeTextPreferences.properties.toSource().cleanProps(); | |
var myFindChangeTextOptions = ( _param.catch_options == true ) | |
? app.findChangeTextOptions.properties.toSource().cleanProps() | |
: '{}'; | |
var myFindGrepProperties = app.findGrepPreferences.properties.toSource().cleanProps(); | |
var myChangeGrepProperties = app.changeGrepPreferences.properties.toSource().cleanProps(); | |
var myFindChangeGrepOptions = ( _param.catch_options == true ) | |
? app.findChangeGrepOptions.properties.toSource().cleanProps() | |
: '{}'; | |
var myFile = myGetFileName(); | |
var myData = collectData(); | |
writeData ( myData ); | |
if ( _param.final_message == true ) | |
alert('Done!'); | |
// =============================================================== | |
// functions / Funktionen | |
// =============================================================== | |
function myGetFileName() | |
{ | |
if ( _param.file_2desk == true ) | |
{ | |
var myFile = new File( '~/Desktop/findChangeStrings.txt' ) | |
} | |
else | |
{ | |
if( File.fs != 'Macintosh' ) | |
{ | |
//Filter files by extension. | |
var myFile = File.saveDialog( 'Save Text File As:', 'Text Files:*.txt;All Files:*' ) | |
} | |
else | |
{ | |
var myFile = File.saveDialog( 'Save Text File As:' ) | |
} | |
if ( myFile == null ) | |
exit(); | |
} | |
return myFile; | |
} | |
function collectData() | |
{ | |
if ( _param.single_line == true ) | |
{ | |
if ( myFindTextProperties == '{}' && myChangeTextProperties == '{}' ) | |
{ | |
var myText = ''; | |
} | |
else | |
{ | |
var myText = 'text' + '\t' + | |
myFindTextProperties + '\t' + | |
myChangeTextProperties + '\t' + | |
myFindChangeTextOptions + | |
'\t//' + _param._comment + '\r'; | |
} | |
if ( myFindGrepProperties != '{}' || myChangeGrepProperties != '{}' ) | |
{ | |
myText += 'grep' + '\t' + | |
myFindGrepProperties + '\t' + | |
myChangeGrepProperties + '\t' + | |
myFindChangeGrepOptions + | |
'\t//' + _param._comment + '\r'; | |
} | |
} | |
else | |
{ | |
var myText = '//' + _param._comment + '\r\r'; | |
if ( myFindTextProperties != '' ) | |
{ | |
myText += '//FindTextProperties\r' + | |
myFindTextProperties + '\r\r' + | |
'//ChangeTextProperties\r' + | |
myChangeTextProperties + '\r\r' + | |
'//FindChangeTextOptions\r' + | |
myFindChangeTextOptions + '\r\r'; | |
} | |
if ( myFindGrepProperties != '' || myChangeGrepProperties != '' ) | |
{ | |
myText += '//FindGrepProperties\r' + | |
myFindGrepProperties + '\r\r' + | |
'//ChangeGrepProperties\r' + | |
myChangeGrepProperties + '\r\r' + | |
'//FindChangeGrepOptions\r' + | |
myFindChangeGrepOptions; | |
} | |
} | |
return myText; | |
} | |
function writeData ( aData ) | |
{ | |
if( myFile!='' ) | |
{ | |
//Open the file for writing. | |
myResult = myFile.open( 'w', undefined, undefined ); | |
} | |
if( myResult != false ) | |
{ | |
myFile.writeln( aData ); | |
myFile.close(); | |
if ( _param.file_open == true ) | |
myFile.execute(); | |
} | |
} | |
function get_param() | |
{ | |
var _pref_string = app.documents.firstItem().extractLabel('FindChangeByList'); | |
if (_pref_string == '') | |
_pref_string ="({single_line:true, catch_options:true, file_2desk:true, file_open:true, _comment:'Comment', final_message:true})"; | |
var _pref = eval(_pref_string); | |
var _pref_rc = (_pref.single_line == true) | |
? true | |
: false; | |
var myDialog = app.dialogs.add({name:"Record FindChange Properties"}); | |
var min_width = 300; | |
with(myDialog.dialogColumns.add()){ | |
with(dialogRows.add()){ | |
staticTexts.add( {staticLabel: "Output:"} ); | |
} | |
with(dialogRows.add()){ | |
var single_multi = radiobuttonGroups.add(); | |
with(single_multi) { | |
radiobuttonControls.add({staticLabel: "single line (for FindChangeByList.jsx)", checkedState: _pref_rc, minWidth: min_width}); | |
radiobuttonControls.add({staticLabel: "multiline (list view)", checkedState: !_pref_rc, minWidth: min_width}); | |
} | |
} | |
with(dialogRows.add()){ | |
staticTexts.add( {staticLabel: "-----"} ); | |
} | |
with(dialogRows.add()){ | |
staticTexts.add( {staticLabel: "Further options:"} ); | |
} | |
with(dialogRows.add()){ | |
var the_cb_options = checkboxControls.add({staticLabel: "Record findChangeTextOptions and findChangeGrepOptions", checkedState: _pref.catch_options, minWidth: min_width}); | |
} | |
with(dialogRows.add()){ | |
var the_cb_file2desk = checkboxControls.add({staticLabel: "Show result on desktop (findChangeStrings.txt)", checkedState: _pref.file_2desk, minWidth: min_width}); | |
} | |
with(dialogRows.add()){ | |
var the_cb_open = checkboxControls.add({staticLabel: "Open TXT-file after running script", checkedState: _pref.file_open, minWidth: min_width}); | |
} | |
with(dialogRows.add()){ | |
var the_cb_final = checkboxControls.add({staticLabel: "Show message when done", checkedState: _pref.final_message, minWidth: min_width}); | |
} | |
with(dialogRows.add()){ | |
staticTexts.add( {staticLabel: "Comment: "} ); | |
var x = _pref._comment; | |
var the_cb_comment = textEditboxes.add({staticLabel: "Comment", editContents: x, minWidth: min_width}); | |
} | |
} | |
var myResult = myDialog.show(); | |
var mySingleLine = (single_multi.selectedButton == 0) | |
? true | |
: false; | |
var myCatchOptions = the_cb_options.checkedState; | |
var myFile2Desk = the_cb_file2desk.checkedState; | |
var myFileOpen = the_cb_open.checkedState; | |
var myComment = the_cb_comment.editContents; | |
var myFinalMessage = the_cb_final.checkedState; | |
myDialog.destroy(); | |
if(myResult == false) | |
exit(); | |
else | |
return {single_line: mySingleLine, catch_options: myCatchOptions, file_2desk: myFile2Desk, file_open: myFileOpen, _comment: myComment, final_message: myFinalMessage}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment