An Xcode Source Editor Extension that copies the selected code as syntax-colored rich text in Xcode's Light or Dark theme — ready to paste into Keynote, Pages, Notes, Mail, Notion and any rich-text editor.
- Two commands in Xcode's Editor menu: Copy Code Styled ▸ Copy as Light Style / Copy as Dark Style (also reachable via Quick Actions, ⇧⌘A).
- Colors follow Xcode's built-in Default (Light) and Default (Dark) themes: keywords, strings, numbers, comments, attributes, type names, function calls, declarations, preprocessor directives.
- Highlights Swift and the C family (C / C++ / Objective-C); other languages fall back to comments/strings/numbers.
- The pasteboard receives three flavors at once:
public.rtf(Keynote / Pages / Notes),public.html(Notion / browsers), and plain text. - Text colors only, no background — the pasted code sits cleanly on whatever your slide or document background is.
- Default font: SF Mono 24 pt (falls back to Menlo when SF Mono is not installed).
- Tabs are expanded, common leading indentation is removed, trailing blank lines are trimmed. With no selection the whole file is copied.
- Grab CopyCodeStyled-x.y.z.zip from the latest release, unzip, and move
CopyCodeStyled.appto/Applications. - Open the app once. It is signed with a development certificate but not notarized, so on first launch use right-click ▸ Open, or allow it in System Settings ▸ Privacy & Security ▸ Open Anyway.
- Continue with step 2 of Install below (enable the extension, restart Xcode).
- Open
CopyCodeStyled.xcodeproj, pick the CopyCodeStyled scheme and Build & Run once (or Archive and copy the app to/Applications). macOS registers the extension from the app bundle — run the app again if you move it. - Enable it in System Settings ▸ General ▸ Login Items & Extensions ▸ Xcode Source Editor, or from the terminal:
pluginkit -e use -i com.swiftruru.CopyCodeStyled.CopyCodeExtension
- Restart Xcode. Select code, then Editor ▸ Copy Code Styled ▸ Copy as Light Style / Copy as Dark Style.
- Optional: bind shortcuts in Xcode ▸ Settings ▸ Key Bindings (search for "Copy as").
The commands also show up in Xcode's Quick Actions (⇧⌘A) — just type "copy co":
The host app shows the same setup steps and a live preview where you can paste any Swift code, tweak the size, and copy manually.
| Path | Description |
|---|---|
CopyCodeStyled/ |
macOS host app (setup instructions, live preview, manual copy) |
CopyCodeExtension/ |
The Source Editor Extension (SourceEditorCommand.swift, Info.plist declares the two commands) |
Shared/CodeTheme.swift |
Light / Dark palettes matching Xcode's default themes |
Shared/CodeTokenizer.swift |
Lightweight lexer for Swift and C / C++ / Objective-C, generic fallback |
Shared/StyledCodeRenderer.swift |
Builds NSAttributedString / RTF / HTML and writes the pasteboard; tab expansion and de-indent |
Select the CopyCodeExtension scheme and Run. Xcode asks which app to launch — choose Xcode. A second (gray-icon) Xcode instance starts with the extension loaded, so breakpoints work.
- Font:
StyledCodeRenderer.defaultFontName; size:StyledCodeRenderer.defaultFontSize(24 pt). - Colors:
CodeTheme.xcodeLight/CodeTheme.xcodeDark. - Keep original indentation: pass
dedent: falsetoCodeTextCleaner.clean(_:tabWidth:dedent:).
- Built with Xcode 27, targets macOS 14+.
XcodeKit.frameworkis embedded in the.appex(Embed Frameworks phase) — without it the extension process cannot launch and the commands never appear.- Both targets are signed with your development team (Automatic signing). Ad-hoc signed builds are not loaded by Xcode.
在 Xcode 裡選取程式碼,用 Xcode 預設 Light / Dark 配色複製成 rich text,直接貼到 Keynote、Pages、備忘錄、Mail、Notion 或任何支援 rich text 的編輯器。
- Xcode Editor 選單新增兩個指令:Copy Code Styled ▸ Copy as Light Style / Copy as Dark Style(也可透過 Quick Actions ⇧⌘A 搜尋執行)。
- 配色對應 Xcode 內建的 Default (Light) 與 Default (Dark) 主題:關鍵字、字串、數字、註解、attribute、型別名稱、函式呼叫、宣告、前置處理指令。
- 支援 Swift 與 C 家族(C / C++ / Objective-C)的語法上色;其他語言退回只上色註解、字串、數字。
- 剪貼簿同時放入三種格式:
public.rtf(Keynote / Pages / 備忘錄)、public.html(Notion / 瀏覽器)、純文字。 - 只帶文字顏色、不帶背景色,貼上後不會出現一塊塊的白底或黑底,直接融入投影片或文件的背景。
- 預設字型 SF Mono 24 pt(未安裝 SF Mono 時改用 Menlo)。
- 自動展開 tab、去除共同縮排、移除前後空白行;沒有選取時複製整個檔案。
- 到 最新 Release 下載 CopyCodeStyled-x.y.z.zip,解壓縮後把
CopyCodeStyled.app放到/Applications。 - 開啟一次 app。這份是開發者憑證簽章、未經 notarize,第一次請按右鍵 ▸ 打開,或到 系統設定 ▸ 隱私權與安全性 按「強制打開」。
- 接著做下方「從原始碼安裝」的第 2 步起(勾選 extension、重啟 Xcode)。
- 用 Xcode 開啟
CopyCodeStyled.xcodeproj,Scheme 選 CopyCodeStyled,Build & Run 一次(或 Archive 後把 app 放到/Applications)。macOS 會從這個 app bundle 註冊 extension;app 移動位置後請再執行一次。 - 到 系統設定 ▸ 一般 ▸ 登入項目與延伸功能 ▸ Xcode Source Editor 勾選 Copy Code Styled,或在終端機執行:
pluginkit -e use -i com.swiftruru.CopyCodeStyled.CopyCodeExtension
- 重新啟動 Xcode,選取程式碼後使用 Editor ▸ Copy Code Styled ▸ Copy as Light Style / Copy as Dark Style。
- 想要快捷鍵:Xcode ▸ Settings ▸ Key Bindings 搜尋「Copy as」。
也可以在 Xcode 的 Quick Actions(⇧⌘A)輸入「copy co」直接執行:
Host app 會顯示同樣的安裝步驟與即時預覽,可貼入任何 Swift 程式碼、調整字級並手動複製。
| 路徑 | 說明 |
|---|---|
CopyCodeStyled/ |
macOS host app(安裝說明、即時預覽、手動複製) |
CopyCodeExtension/ |
Source Editor Extension(SourceEditorCommand.swift、Info.plist 定義兩個指令) |
Shared/CodeTheme.swift |
Light / Dark 配色(對應 Xcode 預設主題) |
Shared/CodeTokenizer.swift |
輕量 lexer:Swift、C / C++ / Objective-C,其餘語言退回通用模式 |
Shared/StyledCodeRenderer.swift |
產生 NSAttributedString / RTF / HTML 並寫入剪貼簿;含 tab 展開、去除共同縮排 |
Scheme 選 CopyCodeExtension 後 Run,Xcode 會詢問要啟動哪個 app → 選 Xcode,會另外開一個(灰色圖示的)Xcode 實例並載入 extension,可下中斷點。
- 字型:
StyledCodeRenderer.defaultFontName;字級:StyledCodeRenderer.defaultFontSize(預設 24 pt)。 - 配色:
CodeTheme.xcodeLight/CodeTheme.xcodeDark。 - 不想去除共同縮排:
CodeTextCleaner.clean(_:tabWidth:dedent:)的dedent傳false。
- 以 Xcode 27 建置,支援 macOS 14 以上。
XcodeKit.framework已嵌入.appex(Embed Frameworks phase);缺少它 extension process 無法啟動,Xcode 選單就不會出現指令。- 兩個 target 都用開發者 Team 簽章(Automatic signing);ad-hoc 簽章的版本 Xcode 不會載入。



