User Tools

Site Tools


script_external_editor_script_importer

====== Differences ====== This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
script_external_editor_script_importer [2016/01/30 11:06]
cdfa
script_external_editor_script_importer [2016/10/18 10:51]
cdfa
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.2
   * 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 +  * fixed importing of large scripts 
-  * updated ​toasts+  * 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 snew 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() +//var scriptNames ​[] 
-var time = date.getTime();+var time = new Date().getTime();
 var updatedScripts = []; var updatedScripts = [];
-for(i=0;​i<​scriptNames.length;​i++){ +var folder ​= new File(scriptFolder) 
-  //​alert(scriptNames[i]) +var files folder.listFiles() 
-  ​var script=LL.getScriptByName(scriptNames[i]);​ +if(!files)return;
-  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();​ +
-</​code>​ +
- +
-<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++){ for(i=0;​i<​files.length;​i++){
-  var name = files[i].getName() +  var fileName ​= files[i].getName() 
-  var length = name.length +  var length = fileName.length 
-  if(name.substring(length-3,​length)=="​.js"​){ +  if(fileName.substring(length-3,​length)=="​.js"​){ 
-    ​scriptNames.push(name.substring(0,length-3)); +    ​var name = fileName.slice(0,-3); 
-  } +    var script=getScriptByName(name); 
-+    if(script==null){ 
- +      Toast.makeText(context,​ "​creating new script: "+name, Toast.LENGTH_SHORT).show();​ 
-var date = new Date() +      script = createScript(name,"",​0,importPath); 
-var time = date.getTime();​ +      var tagdate=0;​ 
-var updatedScripts = []; +    }else{ 
-for(i=0;​i<​scriptNames.length;​i++){ +      var tagdate = script.getTag("​lmdate"​);​ 
-  //​alert(scriptNames[i]) +    
-  ​var script=LL.getScriptByName(scriptNames[i]); +    var lmdate ​files[i].lastModified() 
-  if(script==null){ +    if(lmdate>​tagdate||typeof tagdate == "​undefined"​){ 
-    Toast.makeText(LL.getContext() ​,​ "​creating new script: "+scriptNames[i], Toast.LENGTH_SHORT).show();​ +      updatedScripts.push(name); 
-    script = LL.createScript(scriptNames[i],"",​0);​ +      ​script.setText(read(scriptFolder+"/"​+fileName));
-    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)       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>​
script_external_editor_script_importer.txt · Last modified: 2017/07/06 13:15 by cdfa