셸 통합
Electron의 `shell` 모듈을 통해 운영 체제의 기본 애플리케이션으로 외부 링크, 파일 및 디렉터리를 여는 방법을 배웁니다.
셸 통합은(는) CoddyKit의 무료 Electron Desktop App Development 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Electron Desktop App Development 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Electron Desktop App Development 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Intro to Shell Integration
Welcome to Shell Integration! In Electron, your desktop app can interact with the user's operating system (OS) in powerful ways.
This means opening external links, files, or even showing items in the OS file explorer, just like native apps do.
Meet the `shell` Module
Electron provides the built-in shell module to make these OS interactions easy and safe. It's available in both the main and renderer processes.
The shell module lets your app delegate tasks like opening URLs or files to the OS's default applications.
Opening External URLs
The most common use case is opening web links in the user's default browser. You can do this with shell.openExternal(url).
This is crucial for security: instead of rendering external web content inside your app, you hand it off to a trusted browser.
Code: Open an External Link
Here's how you can open an external URL. This code would typically be in your main.js or called from a preload script if triggered by the renderer.
Try running this example:
const { app, shell } = require('electron');
app.whenReady().then(() => {
// In a real app, this would be triggered by a user action.
// For this demo, it opens when the app is ready.
shell.openExternal('https://www.electronjs.org/docs')
.then(() => console.log('Opened Electron docs!'))
.catch(err => console.error('Failed to open URL:', err));
// A real Electron app would also create a BrowserWindow here.
});Opening Local Files & Folders
Need to open a document, image, or even a directory from your app? Use shell.openPath(path).
This function attempts to open the given file or directory with the OS's default application for that file type.
Code: Open a Local File
This example creates a simple text file and then opens it using the OS's default text editor.
Try running this example:
const { app, shell } = require('electron');
const path = require('path');
const fs = require('fs');
app.whenReady().then(() => {
const userDataPath = app.getPath('userData');
const filePath = path.join(userDataPath, 'my-document.txt');
// Create a dummy file for demonstration
fs.writeFileSync(filePath, 'Hello from CoddyKit! This is your document.');
shell.openPath(filePath)
.then(() => console.log(`Opened: ${filePath}`))
.catch(err => console.error(`Failed to open path: ${err}`));
});Showing Items in Explorer/Finder
Sometimes you don't want to open a file, but just show where it is. shell.showItemInFolder(fullPath) does just that.
It reveals the specified file in the user's operating system file manager (e.g., File Explorer on Windows, Finder on macOS), and optionally selects it.
Code: Show Item in Folder
Let's create another file and then show its location to the user.
Try running this example:
const { app, shell } = require('electron');
const path = require('path');
const fs = require('fs');
app.whenReady().then(() => {
const desktopPath = app.getPath('desktop');
const fileToShowPath = path.join(desktopPath, 'coddykit_demo_file.txt');
// Create a dummy file on the desktop
fs.writeFileSync(fileToShowPath, 'Find me!');
shell.showItemInFolder(fileToShowPath);
console.log(`Showing item in folder: ${fileToShowPath}`);
});Security & Best Practices
When using shell methods, especially with user-provided input, always consider security:
- Validate URLs: Before calling
openExternal(), ensure the URL is safe and intended. - Context Isolation: In the renderer process, use a preload script to expose only necessary
shellfunctions securely. - Error Handling: Always include
.catch()for promise-based methods likeopenExternalandopenPath.
Quick Check on Shell
Which shell module method would you use to open a web link in the user's default browser?
Recap: Shell Integration
Great job! You've learned how Electron's shell module helps your app interact with the native OS:
shell.openExternal()for web links.shell.openPath()for local files/folders.shell.showItemInFolder()to reveal items in the file manager.
These methods provide a seamless and secure way to extend your app's functionality to the native environment.
자주 묻는 질문
“셸 통합” 강의는 무료인가요?
네 — “셸 통합” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Electron Desktop App Development 강의 전체를 잠금 해제할 수 있습니다. Electron Desktop App Development 강의에는 총 4개의 강의가 포함되어 있습니다.
“셸 통합”에서 뭘 배우나요?
Electron의 `shell` 모듈을 통해 운영 체제의 기본 애플리케이션으로 외부 링크, 파일 및 디렉터리를 여는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Electron Desktop App Development을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Electron Desktop App Development을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Electron Desktop App Development은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“셸 통합” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Electron Desktop App Development 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Electron Desktop App Development 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.