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
Next revision
Previous revision
Last revision Both sides next revision
script_external_editor_script_importer [2015/04/11 20:44]
cdfa [About the script]
script_external_editor_script_importer [2016/10/18 10:51]
cdfa
Line 1: Line 1:
 ====== About the script ====== ====== About the script ======
-  * 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.2
   * Link: https://​plus.google.com/​100430440392224314007/​posts/​KAoKQ317trG   * Link: https://​plus.google.com/​100430440392224314007/​posts/​KAoKQ317trG
 +======Changelog=====
 +  * 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
-//Old tag +var scriptFolder = "/​storage/​sdcard0/​LightningLauncher/​script"; 
-  ​var scriptFolder = "/​storage/​emulated/0/​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")
  
-scriptNames ​[] +var context ​getActiveScreen().getContext();
-folder = new File(scriptFolder) +
-files = folder.listFiles() +
-for(i=0;i<​files.length;​i++){ +
-  name = files[i].getName() +
-  length = name.length +
-  if(name.substring(length-3,​length)=="​.js"​){ +
-    scriptNames[scriptNames.length] = name +
-  } +
-}+
  
-for(i=0;​i<​scriptNames.length;​i++){ +function read(filePath){ 
-  var script=LL.getScriptByName(scriptNames[i].substring(0,​scriptNames[i].length-3)); +  ​try{ 
-  file = new File(scriptFolder,​scriptNames[i]) +    ​var r=new BufferedReader(new FileReader(filePath));  
-  lmdate = file.lastModified() +    ​var s= new StringBuilder() ;  
-  tagdate = script.getTag("​lmdate"​) +    ​var l;  
-  if(lmdate>​tagdate||typeof tagdate == "​undefined"​){ +    while((l=r.readLine())!=null)s.append(l+"\n"); 
-    ​text = new StringBuilder();​ +    ​return s;  
- +  }catch(e){ 
-    ​try { +    alert(e
-      br = new BufferedReader(new FileReader(file))+    ​return ""; ​
-      var line +
- +
-      ​while ((line br.readLine()) != null) +
-        text.append(line); +
-        text.append('​\n'); +
-      } +
-      br.close()+
-      ​script.setText(text);​ +
-      date = new Date() +
-      script.setTag("​lmdate",​date.getTime()) +
-    ​} +
-    ​catch (err) { +
-      throw(err+
-    ​}+
   }   }
 } }
-</​code>​ 
  
-<sxh javascript>​ +//var scriptNames = [] 
-//New tag, line completion and colors +var time new Date().getTime(); 
-var scriptFolder ​"/​storage/​emulated/​0/​LightningLauncher/​script";​ +var updatedScripts ​= []; 
- +var folder = new File(scriptFolder) 
-LL.bindClass("​java.lang.StringBuilder"​) +var files = folder.listFiles() 
-LL.bindClass("​java.io.FileReader"​+if(!files)return;​
-LL.bindClass("​java.io.BufferedReader"​) +
-LL.bindClass("​java.io.File"​) +
-LL.bindClass("​java.text.SimpleDateFormat"​) +
- +
-scriptNames ​= [] +
-folder = new File(scriptFolder) +
-files = folder.listFiles()+
 for(i=0;​i<​files.length;​i++){ for(i=0;​i<​files.length;​i++){
-  ​name = files[i].getName() +  ​var fileName ​= files[i].getName() 
-  length = name.length +  ​var length = fileName.length 
-  if(name.substring(length-3,​length)=="​.js"​){ +  if(fileName.substring(length-3,​length)=="​.js"​){ 
-    ​scriptNames[scriptNames.length] = name +    ​var name = fileName.slice(0,-3); 
-  } +    ​var ​script=getScriptByName(name); 
-+    ​if(script==null){ 
- +      Toast.makeText(context,​ "​creating new script: "​+name,​ Toast.LENGTH_SHORT).show(); 
-for(i=0;​i<​scriptNames.length;​i++){ +      script ​= createScript(name,"",​0,​importPath); 
-  var script=LL.getScriptByName(scriptNames[i].substring(0,scriptNames[i].length-3)); +      ​var tagdate=0; 
-  file = new File(scriptFolder,​scriptNames[i]) +    }else{ 
-  lmdate = file.lastModified() +      ​var tagdate = script.getTag("​lmdate"​);
-  tagdate = script.getTag("​lmdate"​) +
-  if(lmdate>​tagdate||typeof tagdate ​== "​undefined"​){ +
-    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); +
-      ​date new Date() +
-      script.setTag("​lmdate"​,​date.getTime())+
     }     }
-    ​catch (err) { +    ​var lmdate = files[i].lastModified(
-      ​throw(err)+    if(lmdate>​tagdate||typeof tagdate == "​undefined"​){ 
 +      ​updatedScripts.push(name);​ 
 +      script.setText(read(scriptFolder+"/"​+fileName));​ 
 +      script.setTag("​lmdate",​time)
     }     }
   }   }
 } }
 +
 +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