User Tools

Site Tools


Sidebar

Privacy Policy

News

Version 2.50 is out since Jan 1st 2022


Frequently Asked Questions


Namespaces

Note for contributors

If you wish to create a new page in the DroidScript wiki, please click on the most appropriate namespace above and follow the notes for contributors there.

Because of spam, it has been necessary to add a CAPTCHA to the registration form and the save option for editing pages. You will not need to prove you are human if you are logged in, so please register.

Please feel free to improve any existing page, as well as adding new pages to increase the sum of public knowledge about DroidScript.

Formatting Syntax

sample_code:new_file

This is an old revision of the document!


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 create a new folder too)

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.

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

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.

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();    
}
sample_code/new_file.1423135219.txt.gz · Last modified: 2015/02/05 19:20 (external edit)