-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Ghostty: Fix "Open with Ghostty" to open selected Finder item #26554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
raycastbot
merged 4 commits into
raycast:main
from
lederniermagicien:fix/ghostty-open-selected-finder-item
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| const { defineConfig } = require("eslint/config"); | ||
| const raycastConfig = require("@raycast/eslint-config"); | ||
|
|
||
| module.exports = defineConfig([ | ||
| ...raycastConfig, | ||
| ]); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,146 @@ | ||
| import { getPreferenceValues } from "@raycast/api"; | ||
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { | ||
| closeMainWindow, | ||
| getPreferenceValues, | ||
| getSelectedFinderItems, | ||
| getFrontmostApplication, | ||
| popToRoot, | ||
| showToast, | ||
| Toast, | ||
| } from "@raycast/api"; | ||
|
|
||
| import { runGhosttyCommand } from "./utils/command"; | ||
| import { openGhosttyTabAtFinderLocation, openGhosttyWindowAtFinderLocation } from "./utils/scripts"; | ||
| import { runAppleScript } from "./utils/applescript"; | ||
|
|
||
| export default async function Command() { | ||
| /** | ||
| * Get selected items from Path Finder via AppleScript. | ||
| */ | ||
| async function getSelectedPathFinderItems(): Promise<string[]> { | ||
| const result = await runAppleScript(` | ||
| tell application "Path Finder" | ||
| set thePaths to {} | ||
| repeat with pfItem in (get selection) | ||
| set the end of thePaths to POSIX path of pfItem | ||
| end repeat | ||
| set AppleScript's text item delimiters to linefeed | ||
| return thePaths as text | ||
| end tell | ||
| `); | ||
| return result | ||
| .split("\n") | ||
| .map((p) => p.trim()) | ||
| .filter(Boolean); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve each selected path to a directory. | ||
| * Directories pass through; files resolve to their parent. | ||
| * Deduplicates results. | ||
| */ | ||
| async function resolveDirectories(items: { path: string }[]): Promise<string[]> { | ||
| const results = await Promise.all( | ||
| items.map(async (item) => { | ||
| const info = await fs.stat(item.path); | ||
| return info.isDirectory() ? item.path : path.dirname(item.path); | ||
| }), | ||
| ); | ||
| return [...new Set(results)]; | ||
| } | ||
|
|
||
| /** | ||
| * Fallback: if nothing is selected, open the current Finder window's directory. | ||
| * Returns false if Finder isn't frontmost or has no open window. | ||
| */ | ||
| async function fallbackToFinderWindow(): Promise<boolean> { | ||
| const app = await getFrontmostApplication(); | ||
| if (app.name !== "Finder") return false; | ||
|
|
||
| const currentDirectory = await runAppleScript( | ||
| `tell application "Finder" to get POSIX path of (target of front window as alias)`, | ||
| ); | ||
| if (!currentDirectory) return false; | ||
|
|
||
| await openGhosttyAt(currentDirectory); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Open Ghostty at the given directory using the native AppleScript API. | ||
| * Opens as a new window or tab based on the user's preference. | ||
| * Sets the tab/window title to the directory name. | ||
| */ | ||
| async function openGhosttyAt(directory: string): Promise<void> { | ||
| const { openWithGhosttyMode } = getPreferenceValues<Preferences.OpenWithGhostty>(); | ||
|
|
||
| const script = openWithGhosttyMode === "tab" ? openGhosttyTabAtFinderLocation : openGhosttyWindowAtFinderLocation; | ||
| await runGhosttyCommand(script); | ||
| // AppleScript escapes double-quotes by doubling them: " → "" | ||
| const dirLiteral = `"${directory.replace(/"/g, '""')}"`; | ||
| const directoryName = path.basename(directory); | ||
| const nameLiteral = `"${directoryName.replace(/"/g, '""')}"`; | ||
|
|
||
| const openCommand = | ||
| openWithGhosttyMode === "tab" | ||
| ? `if (count of windows) is 0 then | ||
| set win to new window with configuration cfg | ||
| else | ||
| set win to front window | ||
| set newTab to new tab in win with configuration cfg | ||
| select tab newTab | ||
| end if` | ||
| : `set win to new window with configuration cfg`; | ||
|
|
||
| await runAppleScript(` | ||
| tell application "Ghostty" | ||
| activate | ||
| set cfg to new surface configuration | ||
| set initial working directory of cfg to ${dirLiteral} | ||
| ${openCommand} | ||
| set term to focused terminal of selected tab of win | ||
| try | ||
| perform action ("set_tab_title:" & ${nameLiteral}) on term | ||
| perform action ("set_window_title:" & ${nameLiteral}) on term | ||
| end try | ||
| input text "clear" to term | ||
| send key "enter" to term | ||
| focus term | ||
| activate window win | ||
| end tell | ||
| `); | ||
| } | ||
|
|
||
| export default async function Command() { | ||
| try { | ||
| let selectedItems: { path: string }[] = []; | ||
| const app = await getFrontmostApplication(); | ||
|
|
||
| if (app.name === "Finder") { | ||
| selectedItems = await getSelectedFinderItems(); | ||
| } else if (app.name === "Path Finder") { | ||
| const paths = await getSelectedPathFinderItems(); | ||
| selectedItems = paths.map((p) => ({ path: p })); | ||
| } | ||
|
|
||
| if (selectedItems.length > 0) { | ||
| const directories = await resolveDirectories(selectedItems); | ||
| for (const dir of directories) { | ||
| await openGhosttyAt(dir); | ||
| } | ||
| } else { | ||
| const ranFallback = await fallbackToFinderWindow(); | ||
| if (!ranFallback) { | ||
| await showToast({ | ||
| style: Toast.Style.Failure, | ||
| title: "No Finder item selected", | ||
| message: "Select a file or folder in Finder or Path Finder to open in Ghostty.", | ||
| }); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| await showToast({ | ||
| style: Toast.Style.Failure, | ||
| title: "Cannot open in Ghostty", | ||
| message: String(error), | ||
| }); | ||
| } | ||
| await closeMainWindow(); | ||
| await popToRoot(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.