Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Copy Code Styled

English · 繁體中文

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.

Copied with Light style Copied with Dark style


English

▶ 切換到繁體中文

Features

  • 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.

Download (no build required)

  1. Grab CopyCodeStyled-x.y.z.zip from the latest release, unzip, and move CopyCodeStyled.app to /Applications.
  2. 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.
  3. Continue with step 2 of Install below (enable the extension, restart Xcode).

Install (from source)

  1. 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.
  2. Enable it in System Settings ▸ General ▸ Login Items & Extensions ▸ Xcode Source Editor, or from the terminal:
    pluginkit -e use -i com.swiftruru.CopyCodeStyled.CopyCodeExtension
  3. Restart Xcode. Select code, then Editor ▸ Copy Code Styled ▸ Copy as Light Style / Copy as Dark Style.
  4. 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":

Quick Actions showing Copy as Light Style / Copy as Dark Style

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.

Host app with setup steps and live preview

Project layout

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

Debugging

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.

Customizing

  • Font: StyledCodeRenderer.defaultFontName; size: StyledCodeRenderer.defaultFontSize (24 pt).
  • Colors: CodeTheme.xcodeLight / CodeTheme.xcodeDark.
  • Keep original indentation: pass dedent: false to CodeTextCleaner.clean(_:tabWidth:dedent:).

Notes

  • Built with Xcode 27, targets macOS 14+.
  • XcodeKit.framework is 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.

繁體中文

▶ Switch to English

在 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、型別名稱、函式呼叫、宣告、前置處理指令。
  • 支援 SwiftC 家族(C / C++ / Objective-C)的語法上色;其他語言退回只上色註解、字串、數字。
  • 剪貼簿同時放入三種格式:public.rtf(Keynote / Pages / 備忘錄)、public.html(Notion / 瀏覽器)、純文字。
  • 只帶文字顏色、不帶背景色,貼上後不會出現一塊塊的白底或黑底,直接融入投影片或文件的背景。
  • 預設字型 SF Mono 24 pt(未安裝 SF Mono 時改用 Menlo)。
  • 自動展開 tab、去除共同縮排、移除前後空白行;沒有選取時複製整個檔案。

下載安裝(免編譯)

  1. 最新 Release 下載 CopyCodeStyled-x.y.z.zip,解壓縮後把 CopyCodeStyled.app 放到 /Applications
  2. 開啟一次 app。這份是開發者憑證簽章、未經 notarize,第一次請按右鍵 ▸ 打開,或到 系統設定 ▸ 隱私權與安全性 按「強制打開」。
  3. 接著做下方「從原始碼安裝」的第 2 步起(勾選 extension、重啟 Xcode)。

從原始碼安裝

  1. 用 Xcode 開啟 CopyCodeStyled.xcodeproj,Scheme 選 CopyCodeStyled,Build & Run 一次(或 Archive 後把 app 放到 /Applications)。macOS 會從這個 app bundle 註冊 extension;app 移動位置後請再執行一次。
  2. 系統設定 ▸ 一般 ▸ 登入項目與延伸功能 ▸ Xcode Source Editor 勾選 Copy Code Styled,或在終端機執行:
    pluginkit -e use -i com.swiftruru.CopyCodeStyled.CopyCodeExtension
  3. 重新啟動 Xcode,選取程式碼後使用 Editor ▸ Copy Code Styled ▸ Copy as Light Style / Copy as Dark Style
  4. 想要快捷鍵:Xcode ▸ Settings ▸ Key Bindings 搜尋「Copy as」。

也可以在 Xcode 的 Quick Actions(⇧⌘A)輸入「copy co」直接執行:

Quick Actions 顯示 Copy as Light Style / Copy as Dark Style

Host app 會顯示同樣的安裝步驟與即時預覽,可貼入任何 Swift 程式碼、調整字級並手動複製。

Host app:安裝步驟與即時預覽

專案結構

路徑 說明
CopyCodeStyled/ macOS host app(安裝說明、即時預覽、手動複製)
CopyCodeExtension/ Source Editor Extension(SourceEditorCommand.swiftInfo.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:)dedentfalse

備註

  • 以 Xcode 27 建置,支援 macOS 14 以上。
  • XcodeKit.framework 已嵌入 .appex(Embed Frameworks phase);缺少它 extension process 無法啟動,Xcode 選單就不會出現指令。
  • 兩個 target 都用開發者 Team 簽章(Automatic signing);ad-hoc 簽章的版本 Xcode 不會載入。

▲ Back to top / 回到頂部

About

Xcode Source Editor Extension: copy selected code as Light/Dark syntax-colored rich text (RTF + HTML) for Keynote, Pages, Notion…

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages