Skip to main content
 首页 » 编程设计

python之Firefox 附加组件 : 1) Linking Python script to add-on main code | 2) win32 api in JPM/NPM | 3) set file attributes in Windows with OS. 文件

2024年10月24日5wayfarer

我想使用 win32 API在 Windows 操作系统上创建隐藏文件。 使用 JS/Node.js 似乎是不可能的。

使用 Python,导入 API 是可能的(使用 SetFileAttributes 例程和 FILE_ATTRIBUTE_HIDDEN 参数)。

那么如何将 Python 脚本链接到我的 Firefox 附加组件的主要 JS 代码呢?你能给我一些关于这件事的引用吗?我在网上什么也没找到。

请您参考如下方法:

XPCOM 将被弃用。虽然这可以用 nsIFile 来完成,但我没有在这里展示它,因为主线程的性能更差。目前推荐的文件系统访问方式是`OS.File。

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

Windows 上的隐藏文件/目录

这就是您使用 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)); 

这会将桌面上名为my hidden file.txt 的文件设置为隐藏。以下是其他 winAttributes:

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

*nix/Mac 上的隐藏文件/目录

重命名或创建文件以将 . 作为其名称中的第一个字符并将其隐藏。使用 OS.File 这是通过 OS.File.move 函数完成的,因为重命名只是文件系统上的一个移动:

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)); 

这会将桌面上的文件从 my hidden file.txt 重命名为 .my hidden file.txt。

平台API

如果您需要利用平台 API,则无需 python。我们有 js-ctypes:

这里有一些关于 js-ctypes 的文档:

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

这里是类型和函数声明的库/集合:

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