User Tools

Site Tools


script_backup_scripts

====== About the script ====== * Purpose : It saves all your current scripts in a custom folder (in Lightning folder by default) deleting the previous backup (all the items in that folder take care). * Author : [[https://plus.google.com/u/1/105066926163073195690|TrianguloY]] * Link: https://plus.google.com/u/1/105066926163073195690/posts/a1KU6GH5PWm ====== How to use the script ====== This script is a modification of +Lukas Morawietz​​'s save script in his [[script_multitool|multi tool script]]. It asks for confirmation, but you can change that in case you want to do it programmatically (in the resumed event, from tasker...) If you want a 'export' scripts feature, check Lukas's script. The settings are: the directory, to ask or not for confirmation, and to write extra data in the file to use with Lukas's import script. ====== Script code ====== <sxh javascript> var directory="LightningLauncher/Backup scripts";//use the slash as folder separator var confirmation=false;//set to false to save without user interaction var extradata=true;//if true it will save the flags and the name of the script in the file. set to false for an exact copy of the script //binds LL.bindClass("java.io.File"); LL.bindClass("java.io.FileWriter"); LL.bindClass("android.os.Environment"); if(confirmation&&!confirm("Do you want to make a backup of the scripts?\n Old saved scripts will be overwritten, and deleted if they are not in the editor. Your current scripts won't be modified"))return; directory=File.separator+directory.replace(/\//g,File.separator);//not necessary I guess, but just in case // Create a new directory var createDirectory = new File(Environment.getExternalStorageDirectory()+directory); createDirectory.mkdirs(); //backup previous one, in case something goes wrong var files=createDirectory.listFiles(); for(var i=files.length-1;i>=0;--i){ var file=files[i]; files[i]=new File(file.getAbsolutePath()+".tmp"); file.renameTo(files[i]); } //get all scripts var scripts=LL.getAllScriptMatching(Script.FLAG_ALL); //save current scripts for(var t=scripts.getLength()-1;t>=0;--t){ var script=scripts.getAt(t); // create a new file var createFile = new File(Environment.getExternalStorageDirectory()+directory+File.separator+script.getId()+" _ "+converttofile(script.getName())+".js"); createFile.createNewFile(); // create the content of the script var content=""; if(extradata)content+="//Flags: "+ (script.hasFlag(Script.FLAG_APP_MENU)?"app ":"")+ (script.hasFlag(Script.FLAG_ITEM_MENU)?"item ":"")+ (script.hasFlag(Script.FLAG_CUSTOM_MENU)?"custom ":"")+ "\n"+ "//Name: "+script.getName()+ "\n"; content+=script.getText(); //write to file var createFileWriter = new FileWriter(createFile, false); createFileWriter.write(content); createFileWriter.flush(); createFileWriter.close(); } // readme file var createFile = new File(Environment.getExternalStorageDirectory()+directory+File.separator+"[readme].txt"); createFile.createNewFile(); // write to the file var createFileWriter = new FileWriter(createFile, false); createFileWriter.write("This folder contains a backup of the scripts in the editor. Any file here will be deleted at the backup"); createFileWriter.flush(); createFileWriter.close(); //delete tmp files...perhaps I need to check before if nothing went wrong for(var i=files.length-1;i>=0;--i){ files[i].delete(); } Android.makeNewToast("Saved "+scripts.getLength()+" scripts", true).show(); function converttofile(n){ return n.replace(/[,./\:*?""<>|]/g,"_"); } </sxh>

script_backup_scripts.txt · Last modified: 2015/01/20 17:49 by trianguloy