User Tools

Site Tools


sample_code:new_file

Differences

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

Link to this comparison view

sample_code:new_file [2015/02/05 11:20]
octazid add Text
sample_code:new_file [2015/02/05 19:20]
Line 1: Line 1:
-======New File Dialog====== 
-Sometimes you have to create new files in a folder.\\ 
-Its easy with if you use a dialog.\\ \\ 
-(look for the example to [[sample_code:new_folder|create a new folder]] too)\\ \\ 
-{{:pics:newfile.png?400|}} 
  
-Save the **DlgNewFile.js** in the Droidscriptfolder where you need the file.\\  
-You can call the dialog after you load the Script into your app at the OnStart() Event. 
- 
-<code Javascript DlgNewFile.js> 
-/******************************************************************** 
- * CreateNewFile Dialog  
-===================================================================== 
- * Creation date: 20-01-2015 by octazid 
- * Last update: 22-01-2015 by octazid  
-===================================================================== 
- * Simple dialog to create a new file in a folder 
-********************************************************************/ 
- 
- 
-//*** CreateNewFile Dialog***// 
-function NewFileDialog(savepath) 
-{ 
-  var self = this; 
-  this.Nfdlg = app.CreateDialog("Create a new File:"); 
-  this.Nflay = app.CreateLayout( "Linear", "FillXY" ); 
-  this.txtpath = app.CreateText(savepath,0.9,0.02,"Left,Autoscale"); 
-  this.txtpath.SetMargins(0,0.01,0,0); 
-  this.txtpath.parent = self; 
-  this.Nflay.AddChild(this.txtpath); 
-  this.Nfedt = app.CreateTextEdit("",0.9,0.05,"Left"); 
-  this.Nfedt.SetTextSize(18); 
-  this.Nfedt.SetHint("Filename.js"); 
-  this.Nfedt.parent = self; 
-  this.Nflay.AddChild(this.Nfedt); 
-  this.Nfbtnlay = app.CreateLayout( "Linear", "Horizontal,FillXY" ); 
-  this.Nfdlgbtn = app.CreateButton("OK", 0.45); 
-  this.Nfdlgbtn.parent = self; 
-  this.Nfdlgbtn.SetOnTouch(Nfdlgbtn_OnTouch); 
-  this.Nfbtnlay.AddChild(this.Nfdlgbtn); 
-  this.NfdlgbtnCancel = app.CreateButton("Chancel",0.45); 
-  this.NfdlgbtnCancel.parent = self; 
-  this.NfdlgbtnCancel.SetOnTouch(Nfdlgbtn_OnTouch); 
-  this.Nfbtnlay.AddChild(this.NfdlgbtnCancel); 
-  this.Nflay.AddChild(this.Nfbtnlay) 
-  this.Nfdlg.AddLayout(this.Nflay); 
-  this.Show = function(){self.Nfdlg.Show();} 
-  this.Hide = function(){self.Nfdlg.Hide();} 
-}//function NewFileDialog() 
- 
- 
-// Called if a button is touched 
-function Nfdlgbtn_OnTouch() 
-{ 
-    var par = this.parent; 
-    file = par.Nfedt.GetText(); 
-    path = par.txtpath.GetText(); 
-    if(this.GetText()  == "OK") CreateFile(file, path); 
-    par.Hide(); 
-}//function dlgbtn_OnTouch 
- 
- 
-// Called if Button Ok is touched 
-function CreateFile(filename, path) 
-{ 
-    if (filename != "") 
-    { 
-    //Replace illegal letters 
-    filename = filename.replace(/(=|\\|\/|\*|:|,|;|\+|<|>|\"|\[|\]|\?|\|)/g,"");   
- 
-    //Replace double Whitespaces    
-    filename = filename.replace(/(\s+)/g," ");  
- 
-    //Replace Whitespaces at the start and at the end             
-    filename = filename.trim(); 
- 
-        //Check if File exists. if yes, show a message 
-        if (app.FileExists(path + filename)) 
-        { 
-        app.Alert("Please select another Filename!",  
-                  "Error - Filename already exists!"); 
-        } 
-        else 
-        { 
-        //Create the file 
-        app.WriteFile(path + filename); 
-        //show a message if creation was ok 
-        if (app.FileExists(path + filename)) 
-            app.ShowPopup("File Created!"); 
-        } 
-    } 
-}//function CreateFile 
-</code> 
- 
-In your app you have to do just three things,\\ \\  
-1. Load the Script with 
-    app.LoadScript("DlgNewFile.js" 
-2. Create a Button to call the Dialog and add it to the layout 
-    btn = app.CreateButton( "Create a new File", 0.3, 0.1, "Alum" ); 
-    btn.SetOnTouch(CreateNewFile); 
-    lay.AddChild( btn ); 
-3. Write a function to call the Dialog 
-    function CreateNewFile() 
-    { 
-        // write the path where you want create the file  
-        DlgNewFile = new NewFileDialog("/mnt/sdcard/"); 
-        DlgNewFile.Show();     
-    } 
-Or you can use this Example. 
-<code Javascript exampleapp.js> 
-//Called when application is started. 
-function OnStart() 
-{ 
-    app.LoadScript("DlgNewFile.js"      
-    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );     
-    btn = app.CreateButton( "Create a new File", 0.3, 0.1, "Alum" ); 
-    btn.SetOnTouch(CreateNewFile); 
-    lay.AddChild( btn ); 
-    app.AddLayout( lay );     
- 
- 
- 
-//Create a new Dialog  
-function CreateNewFile() 
-{ 
-    // write the path where you want create the file 
-    DlgNewFile = new NewFileDialog("/mnt/sdcard/"); 
-    DlgNewFile.Show();     
-} 
-</code> 
sample_code/new_file.txt ยท Last modified: 2015/02/05 19:20 (external edit)