利用 Shortcuts App 實現 Finder 一鍵複製檔案內容

最近彼得潘在 Finder 操作檔案時想要一鍵複製程式檔的內容,不想再辛苦地打開檔案再全選複製。

可惜研究一下後,發現目前 Finder 只能複製檔案和複製檔案路徑。

點選 Copy 會複製整個檔案
按住 option 鍵,點選 Copy xxx as Pathname 會複製檔案路徑

稍微快一點的方法是先按空白鍵叫出 Quick Look,然後再全選複製,不過彼得潘有點懶惰,想要再快一點,以下我們透過自動化工具 Shortcuts App 實現 Finder 一鍵複製檔案內容。

新增 Quick Actions

啟動 Shortcuts App,點選 Quick Actions,然後點選右上的 +。

設定名稱

輸入 Copy File Content。

在右邊的搜尋框輸入關鍵字加入相關動作

比方一開始我們要先取得 Finder 選取的檔案,輸入 get sele 後雙擊 Get Selected Files in Finder。

請依序加入以下三個動作,Get Selected Files in Finder、Get Text from Input、Copy to Clipboard。

設定 Finder 的 Quick Action

我們想在 Finder 操作 Copy File Content,因此請先點選右上的 i 切換到 Shortcut Details,勾選 Use as Quick Action 下方的 Finder。

從 Finder 複製檔案內容

現在我們可以從 Finder 複製檔案內容了 ! 比方從下圖 ContentView.swift 的右鍵選單點選 Quick Actions > Copy File Content。

在檔案明細的下方也會出現 Copy File Content。

在 TextEdit 按下 cmd + v 即可貼上剛剛複製的內容。

設定快速鍵

我們也可以設定 Copy File Content 的快速鍵,從 Shortcut 的 Details 分頁點選 Add Keyboard Shortcut。

在此彼得潘輸入 cmd + ctrl + c。

一鍵複製檔案內容

現在我們在 Finder 上點選檔案後,按下 cmd + ctrl + c 即可複製檔案內容。

從 Terminal 複製檔案內容

若是習慣從 Terminal 操作,結合 cat & pbcopy 即可複製檔案內容。以下指令將複製 ContentView.swift 的內容。

cat ContentView.swift | pbcopy

以下指令將讓 ContentView2.swift 的內容變成 ContentView.swift 的內容,若是 ContentView2.swift 原本不存在,則會同時新增檔案。

cat ContentView.swift > ContentView2.swift

--

--

彼得潘的 iOS App Neverland
彼得潘的 Swift iOS App 開發問題解答集

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com