====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
script_external_editor_script_importer [2016/01/30 11:06] cdfa |
script_external_editor_script_importer [2017/07/06 13:15] cdfa 1.3 |
||
---|---|---|---|
Line 2: | Line 2: | ||
* Purpose: automatically import .js files elsewhere on your device into lightning launcher when they have changed. This allows you to use external script editors! | * Purpose: automatically import .js files elsewhere on your device into lightning launcher when they have changed. This allows you to use external script editors! | ||
* Author: cdfa | * Author: cdfa | ||
- | * Version: 1.1 | + | * Version: 1.3 |
* Link: https://plus.google.com/100430440392224314007/posts/KAoKQ317trG | * Link: https://plus.google.com/100430440392224314007/posts/KAoKQ317trG | ||
+ | |||
======Changelog===== | ======Changelog===== | ||
- | * added ability to create scripts for .js files that aren't in LL yet | + | 1.3 |
- | * updated toasts | + | * Add support for subfolders |
+ | |||
+ | 1.2 | ||
+ | * fixed importing of large scripts | ||
+ | * updated to new api | ||
====== How to use the script ====== | ====== How to use the script ====== | ||
Recommended to run the script on resumed event of the container which is your test environment, but you can use it really anytime anywhere. | Recommended to run the script on resumed event of the container which is your test environment, but you can use it really anytime anywhere. | ||
+ | |||
====== Script code ====== | ====== Script code ====== | ||
- | <code> | + | <sxh javascript> |
var scriptFolder = "/storage/sdcard0/LightningLauncher/script"; | var scriptFolder = "/storage/sdcard0/LightningLauncher/script"; | ||
+ | var importPath = "/"; | ||
- | LL.bindClass("java.lang.StringBuilder") | + | bindClass("java.io.FileReader") |
- | LL.bindClass("java.io.FileReader") | + | bindClass("java.io.BufferedReader") |
- | LL.bindClass("java.io.BufferedReader") | + | bindClass("java.io.File") |
- | LL.bindClass("java.io.File") | + | bindClass("java.lang.StringBuilder") |
- | LL.bindClass("java.text.SimpleDateFormat") | + | bindClass("android.widget.Toast") |
- | LL.bindClass("android.widget.Toast") | + | |
- | scriptNames = [] | + | var context = getActiveScreen().getContext(); |
- | folder = new File(scriptFolder) | + | |
- | files = folder.listFiles() | + | function read(filePath){ |
- | for(i=0;i<files.length;i++){ | + | try{ |
- | var name = files[i].getName() | + | var r=new BufferedReader(new FileReader(filePath)); |
- | var length = name.length | + | var s= new StringBuilder() ; |
- | if(name.substring(length-3,length)==".js"){ | + | var l; |
- | scriptNames.push(name.substring(0,length-3)); | + | while((l=r.readLine())!=null)s.append(l+"\n"); |
+ | return s; | ||
+ | }catch(e){ | ||
+ | alert(e) | ||
+ | return ""; | ||
} | } | ||
} | } | ||
- | var date = new Date() | + | function updateScripts(folder){ |
- | var time = date.getTime(); | + | folder.listFiles().forEach(function(file){ |
- | var updatedScripts = []; | + | var fileName = file.getName() |
- | for(i=0;i<scriptNames.length;i++){ | + | var length = fileName.length |
- | //alert(scriptNames[i]) | + | if(file.isDirectory()){ |
- | var script=LL.getScriptByName(scriptNames[i]); | + | updateScripts(file); |
- | if(script==null){ | + | }else if(fileName.substring(length-3,length)==".js"){ |
- | Toast.makeText(LL.getContext() , "creating new script: "+scriptNames[i], Toast.LENGTH_SHORT).show(); | + | var name = fileName.slice(0,-3); |
- | script = LL.createScript(scriptNames[i],"",0); | + | var path = folder.getPath().substring(scriptFolder.length); |
- | var tagdate=0; | + | var script=getScriptByPathAndName(path, name); |
- | }else{ | + | if(script==null){ |
- | var tagdate = script.getTag("lmdate"); | + | Toast.makeText(context, "creating new script: "+name, Toast.LENGTH_SHORT).show(); |
- | } | + | script = createScript(path, name,"",0); |
- | file = new File(scriptFolder,scriptNames[i]+".js") | + | var tagdate=0; |
- | lmdate = file.lastModified() | + | }else{ |
- | if(lmdate>tagdate||typeof tagdate == "undefined"){ | + | var tagdate = script.getTag("lmdate"); |
- | updatedScripts.push(scriptNames[i]); | + | |
- | text = new StringBuilder(); | + | |
- | + | ||
- | //try { | + | |
- | br = new BufferedReader(new FileReader(file)); | + | |
- | var line; | + | |
- | + | ||
- | while ((line = br.readLine()) != null) { | + | |
- | text.append(line); | + | |
- | text.append('\n'); | + | |
} | } | ||
- | br.close(); | + | var lmdate = file.lastModified() |
- | script.setText(text); | + | if(lmdate>tagdate||typeof tagdate == "undefined"){ |
- | script.setTag("lmdate",time) | + | updatedScripts.push(name); |
- | //}catch (err) { | + | script.setText(read(folder.getPath()+"/"+fileName)); |
- | //throw(err) | + | script.setTag("lmdate",time) |
- | //} | + | } |
- | } | + | } |
+ | }); | ||
} | } | ||
- | if( updatedScripts.length>0)Toast.makeText( LL.getContext(), "Updated scripts: "+JSON.stringify( updatedScripts), Toast.LENGTH_SHORT).show(); | + | //var scriptNames = [] |
- | </code> | + | var time = new Date().getTime(); |
- | + | ||
- | <sxh javascript> | + | |
- | var scriptFolder = "/storage/sdcard0/LightningLauncher/script"; | + | |
- | + | ||
- | LL.bindClass("java.lang.StringBuilder") | + | |
- | LL.bindClass("java.io.FileReader") | + | |
- | LL.bindClass("java.io.BufferedReader") | + | |
- | LL.bindClass("java.io.File") | + | |
- | LL.bindClass("java.text.SimpleDateFormat") | + | |
- | LL.bindClass("android.widget.Toast") | + | |
- | + | ||
- | scriptNames = [] | + | |
- | folder = new File(scriptFolder) | + | |
- | files = folder.listFiles() | + | |
- | for(i=0;i<files.length;i++){ | + | |
- | var name = files[i].getName() | + | |
- | var length = name.length | + | |
- | if(name.substring(length-3,length)==".js"){ | + | |
- | scriptNames.push(name.substring(0,length-3)); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | var date = new Date() | + | |
- | var time = date.getTime(); | + | |
var updatedScripts = []; | var updatedScripts = []; | ||
- | for(i=0;i<scriptNames.length;i++){ | + | var folder = new File(scriptFolder) |
- | //alert(scriptNames[i]) | + | updateScripts(folder) |
- | var script=LL.getScriptByName(scriptNames[i]); | + | |
- | if(script==null){ | + | |
- | Toast.makeText(LL.getContext() , "creating new script: "+scriptNames[i], Toast.LENGTH_SHORT).show(); | + | |
- | script = LL.createScript(scriptNames[i],"",0); | + | |
- | var tagdate=0; | + | |
- | }else{ | + | |
- | var tagdate = script.getTag("lmdate"); | + | |
- | } | + | |
- | file = new File(scriptFolder,scriptNames[i]+".js") | + | |
- | lmdate = file.lastModified() | + | |
- | if(lmdate>tagdate||typeof tagdate == "undefined"){ | + | |
- | updatedScripts.push(scriptNames[i]); | + | |
- | text = new StringBuilder(); | + | |
- | + | ||
- | //try { | + | |
- | br = new BufferedReader(new FileReader(file)); | + | |
- | var line; | + | |
- | + | ||
- | while ((line = br.readLine()) != null) { | + | |
- | text.append(line); | + | |
- | text.append('\n'); | + | |
- | } | + | |
- | br.close(); | + | |
- | script.setText(text); | + | |
- | script.setTag("lmdate",time) | + | |
- | //}catch (err) { | + | |
- | //throw(err) | + | |
- | //} | + | |
- | } | + | |
- | } | + | |
- | if( updatedScripts.length>0)Toast.makeText( LL.getContext(), "Updated scripts: "+JSON.stringify( updatedScripts), Toast.LENGTH_SHORT).show(); | + | if( updatedScripts.length>0)Toast.makeText(context, "Updated scripts: "+JSON.stringify( updatedScripts), Toast.LENGTH_SHORT).show(); |
</sxh> | </sxh> |