Search code examples
pythonfirefox-addonfilesystemsfile-attributes

Firefox add-ons: 1) Linking Python script to add-on main code | 2) win32 api in JPM / NPM | 3) set file attributes in Windows with OS.file


I would like to use the win32 API to create an hidden file on Windows OS. It seems impossible using JS / Node.js.

With Python, importing the API, it is possible (using SetFileAttributes routine and FILE_ATTRIBUTE_HIDDEN parameter).

So how can I link a Python script to the main JS code for my Firefox add-on? Can you please give me some reference about this matter? I've found nothing on the web.


Solution

  • XPCOM is going to be deprecated. While this can be accomplished with nsIFile I don't show it here because the performance is worse for the main thread. The recommended way for file system access right now is `OS.File.

    https://developer.mozilla.org/en-US/docs/JavaScript_OS.File/OS.File_for_the_main_thread

    Hidden files/directories on windows

    This is how you do it with OS.File:

    OS.File.setPermissions(
            OS.Path.join(OS.Constants.Path.desktopDir, 'my hidden file.txt'),
              {
                winAttributes: {
                    hidden: true
                }
            }
    )
    .then(x => console.log('success:', x), y => console.error('failure:', y));
    

    This will set the file on the deskto named my hidden file.txt to be hidden. Here are the other winAttributes:

    https://dxr.mozilla.org/mozilla-central/source/toolkit/components/osfile/modules/osfile_win_front.jsm#1204-1227

    Hidden files/directories on *nix/Mac

    Rename or create the file to have a . as the first chracter in its name and it is hidden. Using OS.File this is done with the OS.File.move function as rename is just a move on the file system:

    OS.File.move(
            OS.Path.join(OS.Constants.Path.desktopDir, 'my hidden file.txt'),
            OS.Path.join(OS.Constants.Path.desktopDir, '.my hidden file.txt')
    )
    .then(x => console.log('success:', x), y => console.error('failure:', y));
    

    This will rename, and thus making it hidden, the file on the desktop from my hidden file.txt to .my hidden file.txt.

    Platform APIs

    If you need to tap into the platform APIs, no need for python. We have js-ctypes:

    Here are some docs on js-ctypes:

    https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Standard_OS_Libraries

    And here is a library/collection of type and function declarations:

    https://github.com/Noitidart/ostypes/issues/1#issuecomment-199492249