Skip to content

perf(arc): replace tab iteration loop with AppleScript whose filter#26969

Draft
hua86430 wants to merge 3 commits intoraycast:mainfrom
hua86430:fix/arc-enhance-tab-lookup-performance
Draft

perf(arc): replace tab iteration loop with AppleScript whose filter#26969
hua86430 wants to merge 3 commits intoraycast:mainfrom
hua86430:fix/arc-enhance-tab-lookup-performance

Conversation

@hua86430
Copy link
Copy Markdown

@hua86430 hua86430 commented Apr 6, 2026

Description

Replace manual repeat loop in runAppleScriptActionOnTab with AppleScript's native whose clause for direct tab lookup.

Before (O(n) loop)

set tabIndex to 1
repeat with aTab in every tab of first window
  if id of aTab is "tabId" then
    tell tab tabIndex of window 1 to action
    return tabIndex
  end if
  set tabIndex to tabIndex + 1
end repeat

After (direct lookup)

tell first window
  try
    tell (first tab whose id is "tabId") to action
  end try
end tell

This eliminates O(n) sequential tab scanning, improving responsiveness especially when many tabs are open. The try/end try block gracefully handles cases where the tab no longer exists.

Checklist

Replace manual `repeat` loop that iterates through every tab to find a matching ID
with AppleScript's native `whose` clause for direct lookup.

This eliminates O(n) sequential tab scanning, improving responsiveness
especially when many tabs are open.
@raycastbot raycastbot added extension fix / improvement Label for PRs with extension's fix improvements extension: arc Issues related to the arc extension AI Extension platform: macOS labels Apr 6, 2026
@raycastbot
Copy link
Copy Markdown
Collaborator

raycastbot commented Apr 6, 2026

Thank you for your first contribution! 🎉

🔔 @thomaspaulmann @zach-fuller @ankitchouhan1020 @pernielsentikaer @danpalmer @rgrunberg @tleo19 @loris @bdsqqq @xilopaint @jayeshbhole @hinzed1127 @omar-salama @underscoregeb @ridemountainpig @nebbles @alanxmay @gdsmith @EasyKey5 @Pavelas @mchoun you might want to have a look.

You can use this guide to learn how to check out the Pull Request locally in order to test it.

📋 Quick checkout commands
BRANCH="fix/arc-enhance-tab-lookup-performance"
FORK_URL="https://github.com/hua86430/extensions.git"
EXTENSION_NAME="arc"
REPO_NAME="extensions"

git clone -n --depth=1 --filter=tree:0 -b $BRANCH $FORK_URL
cd $REPO_NAME
git sparse-checkout set --no-cone "extensions/$EXTENSION_NAME"
git checkout
cd "extensions/$EXTENSION_NAME"
npm install && npm run dev

We're currently experiencing a high volume of incoming requests. As a result, the initial review may take up to 10-15 business days.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 6, 2026

Greptile Summary

This PR replaces the O(n) repeat loop in runAppleScriptActionOnTab with AppleScript's native whose clause for direct tab lookup — a reasonable performance improvement for users with many open tabs.

  • The whose clause (first tab whose id is "${tabId}") throws an AppleScript error when no matching tab exists, changing the previous silent no-op to an uncaught JavaScript exception in callers like selectTab, closeTab, and reloadTab that have no error handling.
  • The new if (count of windows) is 0 then make new window guard is redundant — ensureArcIsRunning() (called immediately above) already performs the same check.
  • CHANGELOG.md was not updated as required.

Confidence Score: 3/5

Merging as-is changes error-handling behavior — a missing tab now throws where it previously returned silently.

One P1 finding: the whose clause changes a silent no-op into an uncaught exception for callers that don't handle the error case. A missing CHANGELOG update and a redundant guard are additional concerns.

extensions/arc/src/arc.ts — specifically the runAppleScriptActionOnTab function and its callers

Important Files Changed

Filename Overview
extensions/arc/src/arc.ts Replaces O(n) loop with AppleScript whose clause; introduces a behavior change where a missing tab now throws instead of silently no-ops, and adds a redundant window guard already handled by ensureArcIsRunning()
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/arc/src/arc.ts
Line: 121-123

Comment:
**Redundant window guard after `ensureArcIsRunning()`**

The `ensureArcIsRunning()` call on line 118 already guarantees at least one window exists (see lines 17–19 of the same file where the same `if (count of windows) is 0 then make new window` guard runs). This block is therefore unreachable and can be removed.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: extensions/arc/src/arc.ts
Line: 125

Comment:
**`whose` clause throws when no tab matches**

If no tab with `tabId` exists in the first window (e.g., the tab was closed between when the list was fetched and this action runs), `first tab whose id is "..."` raises an AppleScript error. The old loop silently returned nothing in that case. `runAppleScript` will surface this as a thrown JavaScript error, and callers (`selectTab`, `closeTab`, `reloadTab`) have no try/catch, changing the observable behaviour from a silent no-op to an uncaught exception.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "docs(arc): add changelog entry for tab l..." | Re-trigger Greptile

Comment thread extensions/arc/src/arc.ts Outdated
Comment thread extensions/arc/src/arc.ts Outdated
…ssing tab

- Remove `if (count of windows) is 0` check since `ensureArcIsRunning()` already guarantees a window exists
- Wrap `whose` clause in try/end try to gracefully handle cases where the tab no longer exists
Copy link
Copy Markdown
Collaborator

@pernielsentikaer pernielsentikaer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hua86430 👋

Would you consider moving activate inside the try block so Arc doesn't activate when the tab wasn't found (preserving old behavior)


I converted this PR into a draft until it's ready for the review, please press the button Ready for review when it's ready and we'll have a look 😊

@pernielsentikaer pernielsentikaer marked this pull request as draft April 10, 2026 11:12
@pernielsentikaer pernielsentikaer self-assigned this Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Extension extension: arc Issues related to the arc extension extension fix / improvement Label for PRs with extension's fix improvements platform: macOS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants