
macOS Shortcuts (tiến thân là Workflow do Apple mua năm 2017) là công cụ automation mạnh nhất tích hợp sẵn trên macOS từ phiên bản Monterey. Không chỉ dành cho power user, Shortcuts giúp developer tự động hóa workflow lặp lại: build project, deploy, quản lý clipboard, resize ảnh, convert video, trigger CI/CD, thậm chí điều khiển smart home. Bài này hướng dẫn từ cơ bản đến advanced: tạo shortcut chạy shell script, AppleScript, JavaScript for Automation (JXA), tích hợp với Raycast, Alfred, và chia sẻ qua iCloud.
Tại sao developer nên dùng Shortcuts
Khác với Automator (legacy, chỉ hỗ trợ workflow tuyến tính), Shortcuts hỗ trợ: biến (variable), điều kiện if/else, lặp (repeat), menu chọn (choose from menu), chạy script (Shell, AppleScript, JavaScript, Python), gọi API (Get Contents of URL), và chia sẻ shortcut file .shortcuts hoặc public link. Tích hợp sâu với hệ thống: Services menu, Quick Actions (Finder/Touch Bar), Menu Bar, Siri, Back Tap (iPhone), Focus Mode.

Shortcuts cơ bản cho developer
Chạy Shell Script ngay trong Shortcuts
Action Run Shell Script cho phép execute bash/zsh/fish trực tiếp. Input nhận từ action trước (text, file, clipboard), output trả về text/stdout. Ví dụ shortcut “Build & Test”:
# Build project
cd ~/Projects/my-app
npm run build 2>&1
# Run tests
npm test 2>&1
# Notify kết quả
osascript -e 'display notification "Build & Test hoàn tất" with title "Shortcuts"'
Đặt shortcut này vào Menu Bar hoặc gán keyboard shortcut (System Settings → Keyboard → Keyboard Shortcuts → Services) để chạy ngay từ bất kỳ app nào.
AppleScript và JavaScript for Automation (JXA)
Action Run AppleScript và Run JavaScript for Automation điều khiển app macOS native. JXA mạnh hơn: syntax ES6, Promise/async-await, truy cập Objective-C bridge. Ví dụ lấy URL từ Safari tab hiện tại:
// JXA
const safari = Application('Safari');
const url = safari.windows[0].currentTab.url();
url;
Kết hợp với action Get Contents of URL để fetch API, parse JSON, hiển thị kết quả — không cần viết native macOS app.
Advanced: Biến, Dictionary, và List
Shortcuts hỗ trợ các kiểu dữ liệu: Text, Number, Boolean, Date, File, Dictionary (key-value), List (array). Action Set Variable, Get Dictionary Value, Get Item from List cho phép xử lý dữ liệu phức tạp. Ví dụ shortcut “Deploy to Staging”:
- Dictionary config: {server: “staging.example.com”, path: “/var/www/app”, branch: “develop”}
- Get Dictionary Value → lấy từng trường
- Run Shell Script: rsync, ssh deploy command
- Get Contents of URL: gọi webhook Slack/Discord notify

Tích hợp Raycast, Alfred, và Hammerspoon
Raycast Extensions API cho phép gọi Shortcuts từ Raycast command. Cài extension “Shortcuts” từ Raycast Store, gán alias cho shortcut quan trọng (vd: “deploy” → Deploy to Staging). Alfred Powerpack có workflow “Shortcuts” tương tự. Hammerspoon (Lua) có thể trigger shortcut qua CLI:
-- Hammerspoon init.lua
hs.hotkey.bind({"cmd", "shift"}, "D", function()
hs.execute("shortcuts run 'Deploy to Staging'")
end)
Kết hợp: Shortcuts làm heavy lifting (script, API call), Raycast/Alfred làm launcher UI, Hammerspoon làm global hotkey.
Shortcuts hữu ích cho developer (sẵn sàng import)
| Shortcut | Chức năng | Actions chính |
|---|---|---|
| Build & Notify | Build project, run test, send notification | Run Shell Script, Show Notification |
| Resize & Optimize Images | Resize ảnh, convert WebP, strip metadata | Get File, Run Shell Script (sips, cwebp), Save File |
| Convert Video to GIF | ffmpeg convert mp4 → gif tối ưu size | Get File, Run Shell Script (ffmpeg), Save File |
| Clipboard History | Lưu clipboard text, tìm kiếm, paste lại | Get Clipboard, Append to File, Choose from List |
| Jira Ticket Branch | Tạo git branch từ Jira ticket URL | Get Clipboard, Get Text Between, Run Shell Script (git checkout -b) |
| API Test & Format JSON | Gọi API, format JSON, copy kết quả | Ask for Input, Get Contents of URL, Get Dictionary Value, Copy to Clipboard |
| Screenshot → OCR → Copy | Chụp màn hình, OCR text, copy vào clipboard | Take Screenshot, Run Shell Script (tesseract), Copy to Clipboard |
Chia sẻ và đồng bộ Shortcuts
Shortcuts lưu trong iCloud Drive (~/Library/Mobile Documents/com~apple~Shortcuts). Import/export file .shortcuts (dạng JSON nén). Chia sẻ công khai: Share → Copy iCloud Link → gửi cho team. Team có thể import, sửa, re-share. Dùng Git track file .shortcuts (giải nén bằng unzip -p file.shortcuts shortcuts.json) để version control.

Mẹo nâng cao
- Input từ Services menu: Chọn text trong Xcode → Services → Shortcut → xử lý text đó (format, encode, generate code)
- Folder Action: Gán shortcut cho folder (Right-click → Services → Folder Action Setup) — tự động xử lý file drop vào (resize ảnh, convert video, upload S3)
- Focus Mode trigger: Shortcuts → Automation → When Focus “Work” turns on → Run “Setup Dev Environment” (mở terminal, IDE, Docker, Spotify)
- Back Tap (iPhone) → Mac: Shortcuts trên iPhone chạy SSH command lên Mac qua Termius/Prompt — điều khiển Mac từ xa không cần VNC
- Menu Bar app: Dùng “Menu Bar” action type (Shortcuts 2.0+) tạo app menu bar thuần túy từ shortcut, không cần Swift/Objective-C
Kết luận
macOS Shortcuts là automation layer nằm giữa GUI (Automator) và code (shell script, Hammerspoon). Developer tận dụng được: chạy script không cần mở Terminal, điều khiển app native qua JXA, tích hợp API, trigger từ Raycast/Alfred/hotkey, đồng bộ iCloud across Mac/iPhone/iPad. Thay vì viết wrapper app cho mọi task nhỏ, hãy gói vào Shortcuts — share cho team, import 1 click, chạy từ Menu Bar hay Siri. Bắt đầu từ 1 shortcut đơn giản (build + notify), dần mở rộng thành toolkit automation cá nhân.
Nguồn: Apple Shortcuts User Guide, MacStories Shortcuts Archive
