跳到主要內容

shell

使用預設應用程式管理檔案和網址。

程序:主程序渲染程序 (僅限非沙箱化)

shell 模組提供與桌面整合相關的功能。

以下是在使用者預設瀏覽器中開啟網址的範例

const { shell } = require('electron')

shell.openExternal('https://github.com')

注意:雖然 shell 模組可以在渲染程序中使用,但在沙箱化的渲染程序中將無法運作。

方法

shell 模組有下列方法

shell.showItemInFolder(fullPath)

  • fullPath 字串

在檔案管理員中顯示指定的檔案。如果可以,請選取該檔案。

shell.openPath(path)

  • path 字串

傳回 Promise<string> - 如果發生失敗,則解析為包含對應失敗錯誤訊息的字串,否則為 ""。

以桌面預設方式開啟指定的檔案。

shell.openExternal(url[, options])

  • url 字串 - 在 Windows 上最多 2081 個字元。
  • options 物件 (選用)
    • activate 布林值 (選用) macOS - true 將開啟的應用程式帶到前景。預設值為 true
    • workingDirectory 字串 (選用) Windows - 工作目錄。
    • logUsage 布林值 (選用) Windows - 指示使用者啟動的啟動,可追蹤經常使用的程式和其他行為。預設值為 false

傳回 Promise<void>

以桌面預設方式開啟指定外部協定的網址。(例如,使用者預設郵件代理程式中的 mailto: 網址)。

shell.trashItem(path)

  • path 字串 - 要移動到垃圾桶的項目路徑。

傳回 Promise<void> - 在操作完成時解析。如果刪除要求項目時發生錯誤,則拒絕。

這會將路徑移動到作業系統特定的垃圾桶位置 (macOS 上的「垃圾桶」、Windows 上的「資源回收筒」,以及 Linux 上特定於桌面環境的位置)。

shell.beep()

播放嗶聲。

shell.writeShortcutLink(shortcutPath[, operation], options) Windows

  • shortcutPath 字串
  • operation 字串 (選用) - 預設為 create,可以是下列其中一個
    • create - 建立新的捷徑,必要時會覆寫。
    • update - 僅更新現有捷徑上的指定屬性。
    • replace - 覆寫現有的捷徑,如果捷徑不存在,則會失敗。
  • options ShortcutDetails

傳回 boolean - 是否成功建立捷徑。

shortcutPath 建立或更新捷徑連結。

shell.readShortcutLink(shortcutPath) Windows

  • shortcutPath 字串

傳回 ShortcutDetails

解析 shortcutPath 的捷徑連結。

當發生任何錯誤時,會擲回例外狀況。