$npx -y skills add SpillwaveSolutions/automating-mac-apps-plugin --skill automating-chromeAutomates Google Chrome and Chromium-based browsers via JXA with AppleScript dictionary discovery. Use when asked to "automate Chrome tabs", "control browser with JXA", "Chrome AppleScript automation", "Chromium browser scripting", or "browser tab management". Covers windows, tab
| 1 | # Automating Chrome / Chromium Browsers (JXA-first, AppleScript discovery) |
| 2 | |
| 3 | ## Technology Status |
| 4 | |
| 5 | JXA/AppleScript browser automation is legacy. JavaScript injection is disabled by default in modern Chrome. Modern alternatives: Selenium/ChromeDriver, Puppeteer, PyXA. |
| 6 | |
| 7 | **Related Skills**: `web-browser-automation`, `automating-mac-apps` |
| 8 | |
| 9 | **PyXA Installation:** See `automating-mac-apps` skill (PyXA Installation section). |
| 10 | |
| 11 | ## Contents |
| 12 | - [Scope](#scope) |
| 13 | - [Core framing](#core-framing) |
| 14 | - [Workflow (default)](#workflow-default) |
| 15 | - [Quick Examples](#quick-examples) |
| 16 | - [Modern Alternatives](#modern-alternatives) |
| 17 | - [What to load](#what-to-load) |
| 18 | |
| 19 | ## Scope |
| 20 | - Primary target: Google Chrome (bundle ID: com.google.Chrome). |
| 21 | - Chromium-based variants (Brave: com.brave.Browser, Edge: com.microsoft.edgemac, Arc: company.thebrowser.Browser) often expose similar dictionaries but may differ by bundle name and permissions. |
| 22 | - Always verify dictionary availability in Script Editor for target browser before automation. |
| 23 | |
| 24 | **Security Note**: JavaScript injection via AppleScript is disabled by default in Chrome. Enable via: View → Developer → Allow JavaScript from Apple Events (not recommended for production use). |
| 25 | |
| 26 | ## Core framing |
| 27 | - AppleScript dictionaries define the automation surface. |
| 28 | - JXA provides logic and data handling. |
| 29 | - `execute()` runs JavaScript in page context but doesn't reliably return values—use tunneling patterns (save to clipboard/localStorage) for data extraction instead. |
| 30 | |
| 31 | **⚠️ Security Warning**: The tunneling approach (clipboard/localStorage) can expose sensitive data. Use modern APIs for production automation. |
| 32 | - **Tunneling Patterns Explained:** Since `execute()` doesn't return JavaScript results directly, work around this by having your injected script save data to accessible locations like the system clipboard (`navigator.clipboard.writeText()`) or localStorage (`localStorage.setItem()`). Then retrieve the data in your JXA script using system commands or by reading back from localStorage via another `execute()` call. |
| 33 | |
| 34 | ## Workflow (default) |
| 35 | 1) Discover dictionary terms in Script Editor (Chrome or target browser). |
| 36 | 2) Prototype minimal AppleScript commands in target browser. |
| 37 | 3) Port to JXA and add defensive checks: |
| 38 | - Wrap operations in try/catch blocks |
| 39 | - Check browser process status: `chrome.running()` |
| 40 | - Verify window/tab indices exist before access |
| 41 | - Handle permission dialogs programmatically when possible |
| 42 | 4) Use batch URL reads and reverse-order deletes for tab operations. |
| 43 | 5) Use tunneling patterns for DOM data extraction. |
| 44 | 6) Validate results: Log tab counts, URLs, or extracted data to confirm operations succeeded. |
| 45 | |
| 46 | ## Quick Examples |
| 47 | |
| 48 | **Open new tab and navigate:** |
| 49 | ```javascript |
| 50 | const chrome = Application('Google Chrome'); |
| 51 | chrome.windows[0].tabs.push(chrome.Tab()); |
| 52 | chrome.windows[0].tabs[chrome.windows[0].tabs.length - 1].url = 'https://example.com'; |
| 53 | ``` |
| 54 | |
| 55 | **Execute JavaScript in current tab:** |
| 56 | ```javascript |
| 57 | const result = chrome.execute({javascript: 'document.title'}); |
| 58 | // Note: execute() runs JS but doesn't reliably return values |
| 59 | ``` |
| 60 | |
| 61 | **Batch close tabs (reverse order):** |
| 62 | ```javascript |
| 63 | const tabs = chrome.windows[0].tabs; |
| 64 | for (let i = tabs.length - 1; i >= 0; i--) { |
| 65 | if (tabs[i].url().includes('unwanted')) { |
| 66 | tabs[i].close(); |
| 67 | } |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | **Extract page data via tunneling:** |
| 72 | ```javascript |
| 73 | // Inject script to save title to localStorage |
| 74 | chrome.execute({javascript: 'localStorage.setItem("pageTitle", document.title)'}); |
| 75 | // Retrieve via another execute call |
| 76 | chrome.execute({javascript: 'console.log(localStorage.getItem("pageTitle"))'}); |
| 77 | ``` |
| 78 | |
| 79 | **Check browser permissions:** |
| 80 | ```javascript |
| 81 | try { |
| 82 | const chrome = Application('Google Chrome'); |
| 83 | chrome.windows[0].tabs[0].url(); // Test access |
| 84 | console.log('Permissions OK'); |
| 85 | } catch (error) { |
| 86 | console.log('Permission error:', error.message); |
| 87 | } |
| 88 | ``` |
| 89 | |
| 90 | ## Modern Alternatives |
| 91 | |
| 92 | For production Chrome automation, see the `web-browser-automation` skill for comprehensive guides covering: |
| 93 | |
| 94 | - **PyXA**: macOS-native Chrome automation with full integration |
| 95 | - **Selenium**: Cross-platform automation with automatic ChromeDriver management |
| 96 | - **Puppeteer**: Node.js automation with bundled Chrome |
| 97 | - **Multi-browser workflows**: Chrome, Edge, Brave, and Arc coordination |
| 98 | |
| 99 | **Quick PyXA Example** (see web-browser-automation skill for details): |
| 100 | ```python |
| 101 | import PyXA |
| 102 | |
| 103 | # Launch Chrome and navigate |
| 104 | chrome = PyXA.Application("Google Chrome") |
| 105 | chrome.activate() |
| 106 | chrome.open_location("https://example.com") |
| 107 | |
| 108 | # Get current tab info |
| 109 | current_tab = chrome.current_tab() |
| 110 | print(f"Page title: {cu |