0

Write data to file

I want to write data to a file. Basically, iterate through the selected records and create a file and write the exact data I require, in the specified way. This would be relatively simple in a *nix shell script (other programming languages are available), but at first glance it seems beyond Ninox's ability as it seems to have only very simplistic print and/or export options that I've not had need to study before.

I originally used individual text files to store the data and then a simple script to write what I needed to a file, but being data, I assumed Ninox was the better tool, further assuming I could create the required scripting to manipulate the data and write the file. Was I wrong?

12 replies

null
    • Ninox developper
    • Jacques_TUR
    • 1 yr ago
    • Reported - view

    Do you want to save a file in your Ninox record or on your computer?

      • UKenGB
      • 1 yr ago
      • Reported - view

      Thanks Jacques TUR. I just want to create a text file from the Ninox data, for use elsewhere.

      Specifically, I have created a database of Satellite and Digital Terrestrial TV channel stream parameters and want to write that to an M3U file that will be used in my media system.

      However, I have just discovered the 'createTextFile' function and after placing the required data in a multiline text field, I have been able to write that to the M3U file exactly as I require.

      Easy when you know how. 馃檪

      The problem as always is knowing what to search for and 'print' and 'export' did not lead me to this simple command which is exactly what I was looking for.

      Now I can create text files to my heart's content. 馃榾

    • Ninox developper
    • Jacques_TUR
    • 1 yr ago
    • Reported - view

    And with this code (and NativeJS from Ninext), you can also download it automatically and without the white window:

    "----------------------------
    Download file
    -----------------------------";
    function downloadFile(fileNinox : file,destFileName : text) do
        #{:text:callback
    // separate the table id, the record number and the file name
    var file = util.parseNIDFile(fileNinox);
    // If the destination of the file name is not defined, it is set as the current name by default.
    if (!destFileName) destFileName = file[2];
    // create the URL of the file
    database.downloadURL1(fileNinox)
        .then((fileURL) => {
            fetch(fileURL)
                .then(resp => resp.blob())
                .then(blob => {
                    const url = window.URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.style.display = 'none';
                    a.href = url;
                    a.download = destFileName;
                    document.body.appendChild(a);
                    a.click();
                    window.URL.revokeObjectURL(url);
                    callback(url.url);
                })
        });

    }#
    end;

    • UKenGB
    • 1 yr ago
    • Reported - view

    I wasn't sure what you were getting at about downloading, but having tested further with the createTextFile function, it appears it is not possible to create the text file where you actually want it. So far I've only been able to create it as an attachment, which is NOT what I want.

    Creating the file works perfectly and it contains what I want, but buried in Ninox's folder hierarchy is no good at all. Well, I could then manually save it elsewhere, but if they expect manual intervention, why not just tell us to simply type the *** file in the first place.

    The Manual gives little help as "createTextFile(nid, string, string)" is less than helpful when they do not define what those strings are. Doh!

    Looking at the examples, it then states:-

    "If you don鈥檛 define where the new file should be saved (as you do in this example), it will be attached to the record."

    I took that to mean that if you only provide a filename, it will be saved as an attachment, but a full path would "define where the new file should be saved". Apparently not.

    When I supplied the full path, the script executed with no error, but no file either. No attachment and nothing of that name saved anywhere on the Mac. At least that I can find.

    So have Ninox provided a useful function, but then crippled it by only allowing it to be created as an attachment, rather than where you actually need it? How dumb is that, especially when the Manual implies it IS possible to "define where the new file should be saved". Except it looks like you cannot.

    Jacques TUR , I don't, for this db, want to get involved in externals like your Ninext, but am I right that Ninox will not allow you to save a created file where you want it?

    • UKenGB
    • 1 yr ago
    • Reported - view

    Basically, as far as I can see, if there's a '/' in the last string (filename), Ninox simply does nothing. No error, but doesn't create the file.

    Someone tell me I'm wrong please.

    • Ninox developper
    • Jacques_TUR
    • 1 yr ago
    • Reported - view

    Use createTextFile with importFile :

    File := createTextFile(this, raw('Rich text'), "MyTextFileExample.html");
    importFile(this, File);
    
      • UKenGB
      • 1 yr ago
      • Reported - view

      Jacques TUR Unless I'm really misunderstanding the meaning of 'import', then importFile is doing the exact opposite of what I want which is to basically 'export' some data.

      Using createTextFile() I can create the text file exactly as I want, but it is simply an attachment and what I want is to be able to specify the precise path to where I want it saved and NOT have it as an attachment or stored in any Ninox field. I have the data, I have created the export file as I need (m3u file) and just want to be able to place it where it is required.

      I realise it is just a file that Ninox has stored in its file/folder structure and I will be able to access it there, but it is bizarre IMO that Ninox has the ability to create any text file you need, but then won't let you save it where you want. Daft even.

      I need to rsync a couple of these files to a local server and currently am just working on doing that directly from Ninox's attachment storage location, which will work, but it's kinda dumb when I should be able to specify where Ninox places the files - especially as the user manual clearly implies that you can do this. I guess that mistake got added in during translation. 馃槈

    • Ninox developper
    • Jacques_TUR
    • 1 yr ago
    • Reported - view
    UKenGB said:
    especially as the user manual clearly implies that you can do this.

     Where ? Can you send me a link ?

    As far as I know, it is not possible to save a file to the computer's disk from Ninox without the user's intervention (or else you have to go through code or a server that allows this). 

      • UKenGB
      • 1 yr ago
      • Reported - view

      Jacques TUR In the manual entry for createTextFile() (https://docs.ninox.com/en/script/function-overview/functions/createtextfile) it states:

      "If you don鈥檛 define where the new file should be saved (as you do in this example), it will be attached to the record."

      That is a pretty clear statement that it IS possible to "define where the new file should be saved" and it will then NOT be attached to the record.

      • Sean
      • 1 yr ago
      • Reported - view

      UKenGB I thought the same way as you when I first read the documentation, but in the example it is being saved to the Image field "File". I think that is the only option they are referring to in the documentation which is kind of funny because image fields are saved as attachments.

      I agree, it would be nice if Ninox implemented an option to use the "Save as" dialog.

    • Ninox developper
    • Jacques_TUR
    • 1 yr ago
    • Reported - view
    Sean said:
    I agree, it would be nice if Ninox implemented an option to use the "Save as" dialog.

    Vous pouvez le faire en cr茅ant une url avec shareFile et un bouton de t茅l茅chargement avec une formule html (sans Ninext). Here is the code to put in the formula. It also has the advantage of not opening an empty window.

    var s := mySharedFile;
    var myDestFileName := "image.jpg";
    html("
    <script>
    function downloadFile(fileURL, destFileName) {
        debugger;
        fetch(fileURL)
            .then(resp => resp.blob())
            .then(blob => {
                const url = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.style.display = 'none';
                a.href = url;
                a.download = destFileName;
                document.body.appendChild(a);
                a.click();
                window.URL.revokeObjectURL(url);
            })  };  </script>
    <button onclick=""debugger; downloadFile('" +
    mySharedFile +
    "', '" +
    myDestFileName +
    "')""> donwload me </button>")
      • Sean
      • 1 yr ago
      • Reported - view

      Jacques TUR 

      Thanks Jacques, I was trying to get your code to work when I discovered the "Save as" functionality already exists in Ninox via the Hamburger menu button. It is available on both the Image field and the Attachments tab. At least in the Mac app, you can activate the "Save as" dialog popup and choose what you want to name the file and where you want to save it.

Content aside

  • 1 yr agoLast active
  • 12Replies
  • 277Views
  • 4 Following