====== About the script ====== * Purpose : Allows to organize a bit the bindings and have them stored for multiple use. * Author : [[https://plus.google.com/u/1/105066926163073195690|TrianguloY]] * Link: https://plus.google.com/u/1/105066926163073195690/posts/MV9GHHLjHE4 ====== How to use the script ====== * simply run it and choose what you want to do. Check the features for more information. Features: * Add bindings directly from text, writing/pasteing them. * Add binding from the current item (you need to run the script from an item) * Add all bindings on the launcher: this will search all bindings from all items from the launcher to save them (will ask one by one without duplicates) * Rename a saved binding * See/Edit/Delete a saved binding * Set a saved binding to the current item (you need to run the script from an item) at a chosen property. Configuration: * You can change the folder where the data (the bindings) are saved. Notes: * When choosing a property, _VOID_ means _DUMMY_ * Editing the bindings folder from outside will only take effect after restarting the script (and is recommended to not do it while the script is running) * You can remove/add/edit manually the files without problem (if you don't do strange things). You can make a copy, delete all at once, add some, etc Important: * Due to the complexity of the script, it may contain bugs. If you find one don't be afraid of tell me in the comments (from the link) ====== Script code ====== var directory = "LightningLauncher/binding_dictionary"; //classes LL.bindClass("android.app.AlertDialog"); LL.bindClass("android.content.DialogInterface"); LL.bindClass("java.io.File"); LL.bindClass("java.io.FileWriter"); LL.bindClass("android.os.Environment"); LL.bindClass("java.io.BufferedReader"); LL.bindClass("java.io.FileReader"); //vars var MAX = 50; var item = LL.getEvent().getItem(); var script = LL.getCurrentScript(); var saved; var properties; var folder = new File(Environment.getExternalStorageDirectory()+"/"+directory); folder.mkdirs(); //start loadSaved(); main(); /// menu /// function main(){ var entries = ["-- Add binding to the dictionary... --\n"] .concat(saved.names .map(function(v,i,a){ return v+"\n{"+cut(saved.formulas[i])+"}\n"; })); list(entries ,f_main ,null ,"Binding dictionary, choose"); } //gets the chosen entry on the main menu function f_main(which){ if(which==0) toDictionary(); else main2(which-1); } //what to do with the binding selected function main2(data){ list(["Add to the item...","See/Edit/delete binding...","Rename binding..."] ,f_main2 ,main ,"Choose what to do with the binding" ,data); } //gets the chosen option in the main2 menu function f_main2(which,data){ if(which==0) toItem(data); else if(which==1) editor(data); else if(which==2) rename(data); } /// add to item /// //shows the list of properties to choose function toItem(data){ if(item==null){ noItemAlert() main() return; } list(properties ,f_toItem ,main2 ,"Choose the property where to set the binding" ,data); } //gets the chosen property //adds the binding to the item function f_toItem(which,data){ var target=Property[properties[which]]; var binding=saved.formulas[data]; //alert(item+"\n"+binding+"\n"+target); var old=item.getBindingByTarget(target); if(old!=null && !confirm("There is already a binding on that property, do you want to override it?\n Current binding:\n"+old.getFormula())){ toItem(data); return; } if(confirm("Confirm the data:\nTarget:"+target+"\nBinding:"+ binding)){ item.setBinding(target, binding,true); Android.makeNewToast("Binding set",true).show() main(); }else{ toItem(data); } } /// add to dictionary /// //shows the list of sources to choose function toDictionary(){ list(["From text...","From item...","From launcher..."] ,f_toDictionary ,main ,"Choose from where to take it"); } //gets the source chosen function f_toDictionary(which){ if(which==0) textToDictionary(); else if(which==1) itemToDictionary(); else if(which==2) fromLauncher(); } //prompts the user to enter a name and formula to save function textToDictionary(){ var formula=""; var name=""; while(true){ formula = prompt("Enter the formula of the binding",formula); if(formula==null){ toDictionary(); return; } name = prompt("Enter the name of the binding",name||""); if(name!=null && addToDictionary(name,formula)){ toDictionary(); return; } }//end while } //shows the list of bindings of the item to choose function itemToDictionary(){ if(item==null){ noItemAlert(); toDictionary(); return; } var rawBindings = item.getAllBindings(); var names = []; var itemBindings=[]; for(var t=0;tMAX?"...":"") } //reads a file and gets the content as text function read(file){ var r=new BufferedReader(new FileReader(file)); var s=""; var l; while((l=r.readLine())!=null)s+=(l+"\n"); r.close(); return s.substring(0, s.length - 1); } //function to display a List in a Popup, where the user can select one item. Adapted from Lukas Morawietz's Multi tool script function list(items,onClickFunction,onCancelFunction,title,data){ var builder=new AlertDialog.Builder(/*new ContextThemeWrapper(*/LL.getContext()/*, R.style.Theme_DeviceDefault)*/); var listener=new DialogInterface.OnClickListener(){ onClick:function(dialog,which){ dialog.dismiss(); setTimeout(function(){onClickFunction(which,data);},0); return true; } } var cancelListener = new DialogInterface.OnClickListener(){ onClick:function(dialog,id){ dialog.cancel(); if(onCancelFunction!=null) setTimeout(function(){onCancelFunction(data)},0); } } builder.setItems(items,listener); builder.setCancelable(true); builder.setTitle(title); builder.setNegativeButton(onCancelFunction==null?"Exit":"Back",cancelListener);//it has a Cancel Button builder.show(); }