User Tools

Site Tools


script_bindings_dictionary

====== 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 ====== <sxh javascript> 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;t<rawBindings.getLength();++t){ var form=rawBindings.getAt(t).getFormula(); names.push(rawBindings.getAt(t).getTarget()+"{"+cut(form)+"}"); itemBindings.push(form); } if(names.length==0){ alert("The selected item has no bindings"); toDictionary(); return; } list(names ,f_itemToDictionary ,toDictionary ,"Choose the binding to save into the dictionary" ,itemBindings); } //gets which binding to add function f_itemToDictionary(which,itemBindings){ var formula = itemBindings[which]; var name = ""; while(true){ name = prompt("Enter the name of the binding",name); if(name==null){ itemToDictionary(); return; } if(addToDictionary(name,formula)){ itemToDictionary(); return; } }//end while } /// editor /// function editor(index){ var output=saved.formulas[index]; while(true){ output=prompt(saved.names[index]+" (save an empty string to delete)",output); if(output==null){ main2(index); return; }else if(output==""){ if(deleteBinding(index)){ main(); return; } }else if(output!=saved.formulas[index]){ if(updateBinding(index,output)){ main2(index); return } }else{ main2(index); return; } }//end while } /// rename /// function rename(index){ var name = saved.names[index]; while(true){ name = prompt("New name:",name); if(name==null){ main2(index); return; }else if(name==""){ if(deleteBinding(index)){ main(); return; } }else if(name!=saved.names[index]){ if(renameBinding(index,name)){ main2(index); return; } }else{ main2(index); return; } }//end while } /// import from launcher /// var ids; function fromLauncher(){ if(!confirm("This will search all bindings from all items in all desktops + the app drawer and ask to save them in the dictionary.\nBindings already in the dictionary will be skipped.\nYou will be asked one by one\n\n Start the process?")){ toDictionary(); return; } ids=[];//ids of containers, to avoid recursion var desktops=LL.getAllDesktops(); for(var t=0;t<desktops.getLength();++t){ importFrom(LL.getContainerById(desktops.getAt(t))); } importFrom(LL.getContainerById(99)); Android.makeNewToast("Done importing", true).show(); toDictionary(); } function importFrom(c){ if(ids.indexOf(c.getId())!=-1) return; ids.push(c.getId()); var items=c.getItems(); for(var t=0;t<items.getLength();++t){ var item = items.getAt(t); var bindings = item.getAllBindings(); for(var j=0;j<bindings.getLength();++j){ var bind=bindings.getAt(j); var formula=bind.getFormula(); if(saved.formulas.indexOf(formula)!=-1) continue; addToDictionary(item.getId()+"-"+bind.getTarget(),formula); } if(item.getType()=="Panel" || item.getType()=="Folder") importFrom(item.getContainer()); } } /// global /// //@return true if saved, false if not function addToDictionary(name,formula){ if(name==null||name==""){ alert("Error: You can't use an empty name"); return false; } if(formula==null||formula==""){ alert("Error: You can't use an empty formula"); return false; } name=name.replace(/[^a-zA-Z0-9 _.-]/g, "_"); var indexformula = saved.formulas.indexOf(formula); var indexname = saved.names.indexOf(name); if(indexname!=-1){ if(indexformula==indexname){ Android.makeNewToast("Already in the dictionary", true).show(); return true; } alert("There is already a binding with this name ("+name+") in the dictionary, you can't have two bindings with the same name."); return updateBinding(indexname,formula); } if(indexformula!=-1 && confirm("The binding '"+saved.names[indexformula]+"' has the same formula. Do you prefer to rename that one to '"+name+"'?...")) return renameBinding(indexformula,name); if(!confirm("Confirm the data to be saved:\nName:\n"+name+"\n\nFormula:\n"+formula)) return false; saved.names.push(name); saved.formulas.push(formula); setFile(name, formula); Android.makeNewToast("Binding saved", true).show(); return true; } function setFile(name, formula){ var createFile = new File(folder,name+".txt"); createFile.createNewFile(); // write to the file var createFileWriter = new FileWriter(createFile, false); createFileWriter.write(formula); createFileWriter.flush(); createFileWriter.close(); } //@return true if deleted, false if not function deleteBinding(index){ var name=saved.names[index]; if(!confirm("Are you sure you want to delete the binding '"+ name+"'?")) return false saved.names.splice(index,1); saved.formulas.splice(index,1); new File(folder,name+".txt").delete(); Android.makeNewToast("Binding deleted", true).show(); return true; } //@return true if updated, false if not function updateBinding(index, formula){ var name=saved.names[index]; if(!confirm("Are you sure you want to update the binding '"+name+"'?")) return false; saved.formulas[index]=formula; setFile(name, formula); Android.makeNewToast("Binding updated", true).show(); return true; } //@return true if renamed, false if not function renameBinding(index,name){ var prename=saved.names[index]; if(prename==name) return true; if(saved.names.indexOf(name)!=-1){ alert("There is another binding with that name, please choose a different one."); return false; } if(!confirm("Are you sure you want to rename the binding '"+prename+"' to '"+name+"'?")) return false; saved.names[index]=name; new File(folder,prename+".txt") .renameTo(new File(folder,name+".txt")); Android.makeNewToast("Binding renamed", true).show(); return true; } function loadSaved(){ saved = {names:[],formulas:[]}; var files=folder.listFiles(); for(var t=0;t<files.length;++t){ var file=files[t]; saved.names.push(file.getName().substring(0,file.getName().length-4)); saved.formulas.push(read(file)); } properties=[]; for(p in Property){ if(p.substring(0,5)=="PROP_") properties.push(p); } properties.sort(); } function noItemAlert(){ alert("no item selected, please run this script from the item you want to use"); } //cuts the input string if more than MAX characters function cut(s){ return s.substring(0,MAX)+(s.length>MAX?"...":"") } //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(); } </sxh>

script_bindings_dictionary.txt · Last modified: 2015/12/19 15:32 by trianguloy