1 / 22
🤖

Part 04 Unit 03

AI 產生腳本

用自然語言描述需求

讓 AI 幫你寫腳本

使用鍵盤 ← → 或點擊按鈕翻頁

2 / 22

學習目標

完成本單元後,你將學會:

  • 使用 Antigravity AI 面板
  • 撰寫有效的腳本生成提示
  • 驗證和修正 AI 生成的腳本
  • 迭代優化腳本功能

💡 不會寫程式?讓 AI 代勞!

3 / 22

AI 腳本生成流程

💭

描述需求

🤖

AI 生成

🔍

檢查測試

優化改進

💡 AI 不是一次就完美,但可以快速迭代改進

4 / 22

步驟 1:開啟 AI 面板

在 Antigravity 中開啟 AI 助手

Ctrl + Shift + A

AI 面板會在右側開啟,你可以:

  • 輸入自然語言問題
  • 請求生成程式碼
  • 解釋程式碼功能
  • 修復程式錯誤
5 / 22

步驟 2:輸入第一個提示

📝 你的提示

請幫我寫一個 PowerShell 腳本,可以將資料夾中所有的 .md 檔案轉換成 .docx 格式。使用 pandoc 指令。

提示技巧:

  • 說明使用的工具(PowerShell、pandoc)
  • 描述輸入和輸出(.md → .docx)
  • 指定範圍(資料夾中所有檔案)
6 / 22

步驟 3:AI 生成腳本

🤖 AI 回應:

# convert-md-to-docx.ps1 # 將資料夾中所有 .md 檔案轉換成 .docx $files = Get-ChildItem -Filter "*.md" foreach ($file in $files) { $outputFile = $file.BaseName + ".docx" pandoc $file.Name -o $outputFile Write-Host "已轉換: $($file.Name) -> $outputFile" } Write-Host "轉換完成!共處理 $($files.Count) 個檔案"

💡 AI 生成的腳本通常可以直接使用!

7 / 22

步驟 4:儲存 AI 生成的腳本

方法一:複製貼上

  1. 選取 AI 生成的程式碼
  2. Ctrl + C 複製
  3. 新增檔案,貼上儲存

方法二:讓 AI 直接建立

請把這個腳本儲存為 convert-md-to-docx.ps1

💡 Antigravity 的 AI 可以直接操作檔案系統

8 / 22

步驟 5:測試腳本

在終端機中執行測試

# 進入有 .md 檔案的資料夾 cd D:\測試資料夾 # 執行腳本 .\convert-md-to-docx.ps1

⚠️ 如果出錯怎麼辦?

把錯誤訊息貼給 AI,讓它幫你修正!

9 / 22

步驟 6:請求 AI 改進腳本

📝 改進提示

請改進這個腳本:
1. 加入錯誤處理
2. 顯示進度百分比
3. 如果沒有找到檔案就顯示警告

💡 不需要重新描述,AI 記得對話內容

10 / 22

AI 改進後的腳本

# convert-md-to-docx.ps1 - 改進版 $files = Get-ChildItem -Filter "*.md" -ErrorAction SilentlyContinue if ($files.Count -eq 0) { Write-Host "⚠️ 警告:找不到任何 .md 檔案" -ForegroundColor Yellow exit } $total = $files.Count $current = 0 $success = 0 foreach ($file in $files) { $current++ $percent = [math]::Round(($current / $total) * 100) Write-Progress -Activity "轉換中..." -Status "$percent%" -PercentComplete $percent try { $output = $file.BaseName + ".docx" pandoc $file.Name -o $output Write-Host "✅ $($file.Name)" -ForegroundColor Green $success++ } catch { Write-Host "❌ $($file.Name): $($_.Exception.Message)" -ForegroundColor Red } } Write-Host "`n📊 完成!成功: $success / 總計: $total"
11 / 22

實用提示範例:批次重命名

📝 你的提示

寫一個 PowerShell 腳本,把資料夾中所有檔案的名稱加上今天的日期前綴。
例如:report.docx → 20250101_report.docx
# AI 生成的腳本 $date = Get-Date -Format "yyyyMMdd" $files = Get-ChildItem -File foreach ($file in $files) { $newName = "${date}_$($file.Name)" Rename-Item $file.FullName -NewName $newName Write-Host "重命名: $($file.Name) → $newName" }
12 / 22

實用提示範例:監控資料夾

📝 你的提示

寫一個腳本,監控指定資料夾。當有新的 .md 檔案出現時,自動使用 pandoc 轉成 .docx

🤖 AI 會生成使用 FileSystemWatcher 的腳本,可以:

  • 持續監控資料夾
  • 偵測新檔案建立事件
  • 自動執行轉換
13 / 22

實用提示範例:整理檔案

📝 你的提示

寫一個腳本,把資料夾中的檔案按照副檔名分類到不同資料夾。
例如:.docx 放到 Word 資料夾,.pdf 放到 PDF 資料夾
# AI 生成的腳本 $files = Get-ChildItem -File foreach ($file in $files) { $ext = $file.Extension.TrimStart('.') $folder = "$ext-files" if (!(Test-Path $folder)) { New-Item -ItemType Directory -Name $folder | Out-Null } Move-Item $file.FullName -Destination $folder Write-Host "移動: $($file.Name) → $folder/" }
14 / 22

撰寫有效提示的技巧

✅ 好的提示包含:

  • 明確的目標 - 腳本要做什麼
  • 技術規格 - 使用什麼語言/工具
  • 輸入輸出 - 處理什麼檔案
  • 特殊需求 - 錯誤處理、進度顯示等

❌ 避免模糊的描述:

  • 「寫一個處理檔案的腳本」(太模糊)
  • 「幫我自動化」(沒有具體說明)
15 / 22

萬用提示模板

📝 模板

請用 [語言/工具] 寫一個腳本:

功能:[描述主要功能]
輸入:[什麼檔案/資料夾]
輸出:[期望的結果]
額外需求:[錯誤處理/進度顯示/參數等]

💡 使用模板可以確保提供足夠的資訊

16 / 22

讓 AI 幫你除錯

執行腳本時出現這個錯誤:
pandoc: Cannot find file 'test.md'
🤖 AI
這個錯誤表示 pandoc 找不到檔案。可能的原因:
1. 檔案名稱有空格需要用引號包住
2. 不在正確的工作目錄

讓我修正腳本...

💡 把錯誤訊息完整貼給 AI,它能快速找到問題

17 / 22

迭代改進腳本

持續對話優化

腳本很好用!可以再加一個功能嗎?
轉換完成後自動開啟輸出資料夾
🤖 AI
當然!在腳本最後加上:
Start-Process explorer $OutputFolder

💡 AI 記得整個對話,可以持續改進

18 / 22

進階用法:解釋現有腳本

📝 你的提示

請解釋這段腳本在做什麼:
gci *.md | % { pandoc $_.Name -o ($_.BaseName + ".docx") }

🤖 AI 解釋:

  • gci *.md - 取得所有 .md 檔案
  • | - 管道傳遞給下一個指令
  • % - ForEach-Object 的別名
  • $_.Name - 當前檔案名稱
  • $_.BaseName - 不含副檔名
19 / 22

安全注意事項

⚠️ 執行 AI 生成的腳本前:

  • 仔細閱讀腳本內容
  • 理解每一行指令的作用
  • 先在測試資料夾執行
  • 避免執行刪除或修改重要檔案的腳本

✅ 安全的做法:

  • 使用 -WhatIf 模擬執行
  • 保留原始檔案備份
  • 不確定就問 AI 解釋
20 / 22

動手練習

練習任務

使用 AI 生成一個腳本,符合以下需求:

  1. 將 .md 檔案轉成 .html
  2. 輸出到 html-output 資料夾
  3. 顯示轉換進度
  4. 完成後顯示統計

參考提示

請用 PowerShell 寫一個腳本,將所有 .md 檔案用 pandoc 轉成 .html,輸出到 html-output 資料夾,並顯示進度和完成統計。

21 / 22

練習參考答案

🔒 輸入密碼查看答案

# md-to-html.ps1 $outputFolder = "html-output" if (!(Test-Path $outputFolder)) { mkdir $outputFolder | Out-Null } $files = Get-ChildItem *.md $total = $files.Count; $current = 0; $success = 0 foreach ($file in $files) { $current++ Write-Host "[$current/$total] $($file.Name)" -NoNewline try { pandoc $file.Name -o "$outputFolder\$($file.BaseName).html" --standalone Write-Host " ✅" -ForegroundColor Green $success++ } catch { Write-Host " ❌" -ForegroundColor Red } } Write-Host "`n📊 完成!成功: $success / 總計: $total"
22 / 22

本單元總結

你學會了:

  • 使用 Antigravity AI 生成腳本
  • 撰寫有效的提示
  • 測試和除錯 AI 生成的程式
  • 迭代改進腳本功能

AI 是你的程式助手!

不會寫程式也能自動化工作

🎉 Part 04 完成!下一個:進階格式轉換