Tauri provides rich APIs to access system functionality, mainly including the following modules:
File System API
typescriptimport { readTextFile, writeTextFile, exists } from '@tauri-apps/api/fs'; // Read file const content = await readTextFile('path/to/file.txt'); // Write file await writeTextFile('path/to/file.txt', 'Hello World'); // Check if file exists const fileExists = await exists('path/to/file.txt');
Shell API
typescriptimport { open } from '@tauri-apps/api/shell'; // Open external link await open('https://example.com'); // Execute command const { stdout } = await Command.create('ls', ['-l']).execute();
Dialog API
typescriptimport { open, save } from '@tauri-apps/api/dialog'; // Open file selector const selected = await open({ multiple: true, filters: [{ name: 'Images', extensions: ['png', 'jpg', 'jpeg'] }] }); // Save file dialog const filePath = await save({ defaultPath: 'document.txt', filters: [{ name: 'Text', extensions: ['txt'] }] });
Window API
typescriptimport { appWindow } from '@tauri-apps/api/window'; // Get window information const label = appWindow.label; const scaleFactor = await appWindow.scaleFactor(); // Window control await appWindow.minimize(); await appWindow.maximize(); await appWindow.close(); // Listen to window events const unlisten = await appWindow.onResized(({ payload }) => { console.log('Window resized:', payload); });
Notification API
typescriptimport { sendNotification } from '@tauri-apps/api/notification'; sendNotification({ title: 'Notification', body: 'This is a notification' });
Clipboard API
typescriptimport { readText, writeText } from '@tauri-apps/api/clipboard'; // Read clipboard const text = await readText(); // Write to clipboard await writeText('Hello World');
Global Shortcut API
typescriptimport { register, unregisterAll } from '@tauri-apps/api/globalShortcut'; // Register shortcut await register('CommandOrControl+Shift+1', () => { console.log('Shortcut pressed'); }); // Unregister all shortcuts await unregisterAll();
Permission Configuration
Declare required permissions in tauri.conf.json:
json{ "tauri": { "allowlist": { "fs": { "all": true }, "shell": { "all": true }, "dialog": { "all": true }, "window": { "all": true }, "notification": { "all": true }, "clipboard": { "all": true }, "globalShortcut": { "all": true } } } }