Created
July 6, 2021 02:56
-
-
Save indiscripts/7137d475edc68bdded3bb15abd770987 to your computer and use it in GitHub Desktop.
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
#targetengine 'StyleToGrep' | |
// Experimental InDesign/ExtendScript/ScriptUI code for testing the idea of two interacting palettes. | |
// Discussion: https://community.adobe.com/t5/indesign/what-is-the-best-practices-to-call-palette-from-another-dialog/td-p/12154542 | |
// [DISCLAIMER] The below script is just a quick proof of concept. Do not use it in production. | |
if( 'function' != typeof $.global.StyleToGrep ) | |
{ | |
Object.prototype.setup=function(o,k){for(k in o)o.hasOwnProperty(k)&&(this[k]=o[k]);return this}; | |
$.global.StyleToGrep = function StyleToGrep( ui) | |
//---------------------------------- | |
{ | |
if( !callee.getDocument() ) | |
{ | |
alert("No InDesign documents are open. Please open a document and try again."); | |
return; | |
} | |
(callee.WIN=callee.uiMain()).show(); | |
} | |
.setup | |
({ | |
//--- Settings | |
UI_TYPE: 'palette', | |
MAIN_TITLE: "Assigned GREP Results Container to Object Styles - Version 2.0x", | |
GREP_TITLE: "GREP Query Manager", | |
// --- | |
QUERY_EXT: '_GREP_Queries.txt', | |
DEF_QUERIES: ["\\(.+?\\)", "\\d+", ".+:", ".+!?", "!?"], | |
// --- | |
WIN_PROPS: {resizable:false, closeButton:true, maximizeButton:false, minimizeButton:false, independent:false}, | |
WIN: false, | |
SUB: false, | |
}) | |
.setup | |
({ | |
//--- Methods | |
about: function(){ alert("blabla...") }, | |
getDocument: function(){ return app.properties.activeDocument||false }, | |
styleNames: function( doc){ return (doc=this.getDocument()) ? doc.objectStyles.everyItem().name : [] }, | |
queryFile: function( doc,ff) | |
{ | |
ff = (doc=this.getDocument()) && doc.properties.fullName; | |
ff && (ff=ff.fsName.substr(0,ff.fsName.lastIndexOf('.'))); | |
ff && (ff=File(ff + this.QUERY_EXT)); | |
return ff || false; | |
}, | |
loadQueries: function( ff,r,msg) | |
{ | |
r = (ff=this.queryFile()).exists && (ff.encoding='BINARY') && ff.open('r'); | |
r && (r=[ff.read().split(/[\r\n]+/g),ff.close()][0]); | |
msg = r | |
? ( r.length ? "Done, GREP Queries Loaded." : "Empty GREP Queries file!" ) | |
: "Error. The GREP Queries file could not be opened!"; | |
alert( msg ); | |
return r; | |
}, | |
saveQueries: function(/*str[]*/data, ff) | |
{ | |
if( !data.length ) return; | |
if( (ff=this.queryFile()) && (ff.encoding='BINARY') && ff.open('w') ) | |
{ | |
ff.write(data.join('\r')); ff.close(); | |
alert("Done, File Saved in Same InDesign Document Location"); | |
} | |
}, | |
assign: function(/*str*/style,/*str*/grep, doc,os,a,t,z) | |
{ | |
os = (doc=this.getDocument()) && doc.objectStyles.itemByName(style); | |
if( (!os) || !os.isValid ) return; | |
app.findGrepPreferences = app.changeGrepPreferences = +NothingEnum.NOTHING; | |
app.findGrepPreferences.findWhat = grep; | |
for | |
( | |
z=0, a=doc.findGrep()||0, i=a.length||0 ; | |
i-- ; | |
(t=a[i].parentTextFrames).length && (++z, t[0].appliedObjectStyle=os) | |
); | |
if( 0 < z ) alert( z + " containers(s) processed.") | |
}, | |
}) | |
.setup | |
({ | |
// --- Main UI | |
uiMain: function uiMain( w,t,u) | |
{ | |
if( w=Window.find(this.UI_TYPE,this.MAIN_TITLE) ){ return w; } | |
w = new Window(this.UI_TYPE, this.MAIN_TITLE, u, this.WIN_PROPS); | |
w.preferredSize.width = 600; | |
w.alignChildren = ['center','top']; | |
w.orientation = 'row'; | |
w.spacing = w.margins = 16; | |
// --- | |
t = w.add('group'); | |
t.orientation = 'column'; | |
t.alignChildren = ['left','top']; | |
t.spacing = 4; | |
// --- | |
t.add('statictext', u, "GREP: "); | |
w.grep = t.add('edittext', u, ""); | |
w.grep.characters = 20; | |
w.grep.enabled = true; | |
w.grep.preferredSize.width = 350; | |
// --- | |
t.add('statictext', u, "Object Styles: "); | |
w.oss = t.add('dropdownlist', u, this.styleNames()); | |
w.oss.preferredSize.width = 350; | |
w.oss.selection = 0; | |
t = w.add('group'); | |
t.orientation = 'column'; | |
t.add('button', u, "Find GREP and Apply Style", {cmd:'apply'}); | |
t.add('button', u, "GREP Query Manager", {cmd:'grep'}); | |
t.add('button', u, "Exit", {cmd:'exit'}); | |
t.add('button', u, "About", {cmd:'about'}); | |
t.addEventListener('click',callee.BTNS); | |
w.center(); | |
return w; | |
} | |
.setup | |
({ | |
BTNS: function onButton(/*MouseEvent*/ev, t,w) | |
{ | |
w = (t=ev.target).window; | |
switch( (t.properties||0).cmd ) | |
{ | |
case 'apply': | |
(t=w.oss.selection) && StyleToGrep.assign(t.text,w.grep.text); | |
break; | |
case 'exit': | |
StyleToGrep.SUB && StyleToGrep.SUB.close(); | |
w.close(); | |
break; | |
case 'grep': | |
t = StyleToGrep.SUB = StyleToGrep.uiGrep(); | |
t.show(); | |
t.active = true; | |
break; | |
case 'about': | |
StyleToGrep.about(); | |
break; | |
default:; | |
} | |
}, | |
}), | |
}) | |
.setup | |
({ | |
// --- Grep UI | |
uiGrep: function( w,t,u) | |
{ | |
if( w=Window.find(this.UI_TYPE,this.GREP_TITLE) ){ return w; } | |
w = new Window(this.UI_TYPE, this.GREP_TITLE, u, this.WIN_PROPS); | |
w.orientation = 'row'; | |
w.margins = w.spacing = 16; | |
w.alignChildren = ['left', 'top']; | |
t = w.add('group'); | |
t.orientation = 'column'; | |
w.input = t.add('edittext'); | |
w.input.characters = 30; | |
w.input.active = true; | |
w.list = t.add('listbox', u, this.DEF_QUERIES, {multiselect:true}); | |
w.list.minimumSize.width = 300; | |
w.list.minimumSize.height = 200; | |
w.list.onChange = callee.LIST; | |
// --- | |
t = w.add('group'); | |
t.orientation = 'column'; | |
t.alignChildren = ['left','top']; | |
t.add('button', u, "Insert", {cmd:'insert'}); | |
t.add('button', u, "Delete Selection", {cmd:'delete'}); | |
t.add('button', u, "Delete All", {cmd:'deleteall'}); | |
t.add('button', u, "Load", {cmd:'load'}); | |
t.add('button', u, "Save", {cmd:'save'}); | |
t.add('button', u, "Restore", {cmd:'restore'}); | |
t.addEventListener('click',callee.BTNS); | |
w.center(); | |
return w; | |
} | |
.setup | |
({ | |
LIST: function onListChange( w,t) | |
{ | |
w = this.window; | |
t = this.selection; // ListItem[] | |
w.input.text = t && t.length ? t[0].text : ''; | |
if( t=Window.find(StyleToGrep.UI_TYPE,StyleToGrep.MAIN_TITLE) ) | |
{ | |
try{ t.grep.text=w.input.text }catch(_){} | |
} | |
}, | |
BTNS: function onButton(/*MouseEvent*/ev, t,w,lb,a,i) | |
{ | |
w = (t=ev.target).window; | |
lb = w.list; // multisel | |
switch( (t.properties||0).cmd ) | |
{ | |
case 'insert': | |
t = w.input.text; | |
if( !lb.find(t) ) | |
{ | |
for( i=(a=lb.items).length ; i-- && t < a[i].text ; ); | |
lb.add("item", t, 1+i); | |
} | |
w.input.text = ""; | |
w.input.active = true; | |
break; | |
case 'delete': | |
a = lb.selection; | |
if( (!a) || !a.length ) break; | |
t = a[0].index; | |
for( i=a.length ; i-- ; lb.remove(a[i]) ); | |
lb.selection = Math.min(t,lb.items.length-1); | |
break; | |
case 'deleteall': | |
lb.removeAll(); | |
break; | |
case 'load': | |
lb.removeAll(); | |
if( !(a=StyleToGrep.loadQueries()) ) break; | |
for( i=-1 ; ++i < a.length ; lb.add('item',a[i]) ); | |
break; | |
case 'save': | |
a = lb.items; | |
for( t=[], i=-1 ; ++i < a.length ; t[i]=a[i].text ); | |
StyleToGrep.saveQueries(t); | |
break; | |
case 'restore': | |
lb.removeAll(); | |
a = StyleToGrep.DEF_QUERIES; | |
for( i=-1 ; ++i < a.length ; lb.add('item',a[i]) ); | |
break; | |
default:; | |
} | |
} | |
}), | |
}); | |
delete Object.prototype.setup; // cleanup | |
} | |
StyleToGrep(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment