⚡ Script Kit开发速查表
版本unknown 更新日志2025-11-18 GitHubjohnlindquist/kit
340px

📦 安装与启动 >>

  • npm install -g @johnlindquist/kit | 全局安装Script Kit
  • kit | 启动Script Kit界面
  • ~/.kit | 默认脚本存储目录

基础脚本结构

import "@johnlindquist/kit";

let name = await arg("请输入名字");
await showToast(`你好,${name}!`);
Script Kit提供TypeScript支持和丰富的API

📦 包管理API >>

  • await npm("package") | 安装并导入包
  • appInstallMultiple(pkgs) | 批量安装包
  • installMissingPackage(pkg) | 条件安装
  • npmInstall(pkg) | 仅安装不导入

包管理示例

import "@johnlindquist/kit";

// 安装并导入
let lodash = await npm("lodash")
let axios = await npm("axios")

// 批量安装
await appInstallMultiple(["axios", "lodash", "dayjs"])

// 条件安装(仅当缺失时)
await installMissingPackage("lodash")

// 仅安装不导入
await npmInstall("package-name")

// 作用域包
let kit = await npm("@johnlindquist/kit")
let babel = await npm("@babel/core")
自动处理包的安装和依赖管理

💬 用户交互API >>

  • await arg(prompt) | 文本输入框
  • await select(options) | 选择列表
  • await confirm(message) | 确认对话框
  • await multi-select(options) | 多选列表

交互示例

import "@johnlindquist/kit";

// 文本输入
let name = await arg("请输入你的名字:")

// 单选
let choice = await select(["选项1", "选项2", "选项3"])

// 确认
let confirmed = await confirm("确定继续吗?")

// 多选
let tools = await multi-select(["git", "npm", "docker", "k8s"])

// 带默认值
let email = await arg("邮箱:", {defaultValue: "user@example.com"})
丰富的用户交互组件提升脚本体验

🔔 系统通知与界面 >>

  • await showToast(message) | 桌面通知
  • await notify(title, body) | 系统通知
  • await div(html) | HTML内容显示
  • await md(content) | Markdown显示

通知示例

import "@johnlindquist/kit";

// 简单通知
await showToast("任务完成!")

// 系统通知
await notify("任务状态", "构建任务已完成,耗时2分钟")

// HTML内容
await div("

HTML标题

段落内容

") // Markdown内容 await md(` # 标题 - 列表项1 - 列表项2 **粗体文本** `) // 带进度条 let progress = 0 let interval = setInterval(async () => { progress += 10 await showToast(`进度:${progress}%`) if (progress >= 100) clearInterval(interval) }, 1000)
多种展示方式满足不同通知需求

📁 文件系统操作 >>

  • await readFile(path) | 读取文件
  • await writeFile(path, content) | 写入文件
  • await getFiles(pattern) | 文件搜索
  • await pathExists(path) | 路径检查

文件操作示例

import "@johnlindquist/kit";

// 读取文件
let content = await readFile("config.json")
let data = JSON.parse(content)

// 写入文件
await writeFile("output.txt", "Hello World!")

// 搜索文件
let jsFiles = await getFiles("*.js", { recursive: true })
let allFiles = await getFiles("**/*", { includeDirectories: true })

// 路径操作
let configPath = path.join(home(), ".config", "app.json")
let exists = await pathExists(configPath)

// 读取JSON
let packageJson = await readJson("package.json")
await writeJson("package-backup.json", packageJson)
完整的文件系统操作支持

📋 剪贴板操作 >>

  • await clipboard.read() | 读取剪贴板
  • await clipboard.write(text) | 写入剪贴板
  • await clipboard.readText() | 读文本内容
  • await clipboard.readImage() | 读图像内容

剪贴板示例

import "@johnlindquist/kit";

// 读取剪贴板
let clipboardContent = await clipboard.read()
await showToast(`剪贴板内容:${clipboardContent}`)

// 写入剪贴板
await clipboard.write("Hello from Script Kit!")

// 处理复制内容
let text = await arg("选择要复制的文本:")
await clipboard.write(text)
await showToast("内容已复制到剪贴板")

// 剪贴板格式转换
let content = await clipboard.read()
await clipboard.write(content.toUpperCase())

// JSON内容处理
let jsonData = await clipboard.read()
try {
  let parsed = JSON.parse(jsonData)
  await clipboard.write(JSON.stringify(parsed, null, 2))
  await showToast("JSON格式化完成")
} catch {
  await showToast("无效的JSON格式")
}
剪贴板操作实现文本和数据的快速交换

⚡ 环境管理与系统 >>

  • env.VAR_NAME | 访问环境变量
  • home() | 获取用户主目录
  • cwd() | 获取当前工作目录
  • exec(command) | 执行系统命令

环境操作示例

import "@johnlindquist/kit";

// 环境变量访问
let apiKey = env.API_KEY || "default-key"
let nodeEnv = env.NODE_ENV || "development"

// 设置环境变量
env.NODE_ENV = "production"
env.DEBUG = "true"

// 目录操作
let homeDir = home()
let cwdPath = cwd()
let projectPath = path.join(homeDir, "projects")

// 执行系统命令
let gitStatus = await exec("git status")
await showToast(div(`
${gitStatus}
`)) // 带参数的命令 let version = await exec("node --version") await showToast(`Node.js版本:${version.trim()}`) // 开发环境检查 let configs = { git: path.join(home(), ".gitconfig"), ssh: path.join(home(), ".ssh"), npm: path.join(home(), ".npmrc") } let missing = [] for (let [name, configPath] of Object.entries(configs)) { if (!await pathExists(configPath)) { missing.push(name) } } if (missing.length > 0) { await showToast(`缺少配置:${missing.join(", ")}`) }
环境管理和系统集成为脚本提供强大能力

🏗️ 项目脚手架 >>

  • await degit(template) | 克隆模板
  • await readJson(path) | 读取JSON文件
  • await writeJson(path, data) | 写入JSON文件
  • await copy(source, target) | 复制文件

项目创建示例

import "@johnlindquist/kit";

async function createProject(projectName, template) {
  console.log(`创建 ${projectName} 从 ${template}...`)

  try {
    // 克隆模板
    await degit(template).clone(`./${projectName}`)

    // 更新 package.json
    let pkgPath = path.join(projectName, 'package.json')
    let pkg = await readJson(pkgPath)
    pkg.name = projectName
    pkg.version = "1.0.0"
    pkg.author = "Your Name"
    await writeJson(pkgPath, pkg, { spaces: 2 })

    // 安装依赖
    await exec(`cd ${projectName} && npm install`)

    // 初始化Git
    await exec(`cd ${projectName} && git init`)

    console.log(`✅ 项目 ${projectName} 创建成功!`)
    await showToast(`项目${projectName}已创建`)
  } catch (error) {
    console.error('创建失败:', error)
    await showToast(`创建失败:${error.message}`)
  }
}

// 使用示例
let projectName = await arg("项目名称:")
let template = await select([
  "vite/vanilla",
  "vite/vue-ts",
  "vite/react-ts",
  "create-next-app"
])

await createProject(projectName, template)
脚手架功能简化项目初始化流程