Skip to content

Instantly share code, notes, and snippets.

@anhkhoakz
Last active June 9, 2025 02:01
Show Gist options
  • Select an option

  • Save anhkhoakz/84c46a8e9328112d769e45804584fe5b to your computer and use it in GitHub Desktop.

Select an option

Save anhkhoakz/84c46a8e9328112d769e45804584fe5b to your computer and use it in GitHub Desktop.

Revisions

  1. anhkhoakz revised this gist Jun 9, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GhosttyAlfred.applescript
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ property WINDOW_TIMEOUT : 3
    property SHELL_LOAD_DELAY : 0.2
    property SWITCH_DELAY : 0.2
    property LOG_FILE : "/tmp/alfred_ghostty/debug.log"
    property ENABLE_LOGGING : true
    property ENABLE_LOGGING : false

    on isGhosttyRunning()
    tell application "System Events" to return (application process "Ghostty" exists)
  2. anhkhoakz revised this gist Jun 4, 2025. No changes.
  3. anhkhoakz revised this gist Jun 4, 2025. 1 changed file with 28 additions and 41 deletions.
    69 changes: 28 additions & 41 deletions GhosttyAlfred.applescript
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,24 @@
    -- AlfredGhostty Script v1.4.0
    -- Latest version: https://github.com/zeitlings/alfred-ghostty-script

    -- Configuration constants
    property NEW_TAB : "t" -- Open new tab
    property NEW_WINDOW : "n" -- Open new window
    property NEW_SPLIT : "d" -- Open new split
    property QUICK_TERMINAL : "qt" -- Open quick terminal
    property OPEN_MODE : NEW_TAB -- Default open mode
    property RUN_COMMAND : true -- Auto-run pasted commands
    property REUSE_TAB : false -- Reuse existing tab if true
    property WINDOW_TIMEOUT : 3 -- Seconds to wait for window
    property SHELL_LOAD_DELAY : 0.2 -- Delay for shell to load
    property SWITCH_DELAY : 0.2 -- Delay for window switching
    property LOG_FILE : "/tmp/alfred_ghostty/debug.log" -- Debug log path
    property ENABLE_LOGGING : true -- Toggle debug logging

    -- Check if Ghostty is running
    -- Latest version: https://paste.sr.ht/~anhkhoakz?search=GhosttyAlfred.applescript
    -- Forked from https://github.com/zeitlings/alfred-ghostty-script

    property NEW_TAB : "t"
    property NEW_WINDOW : "n"
    property NEW_SPLIT : "d"
    property QUICK_TERMINAL : "qt"
    property OPEN_MODE : NEW_TAB
    property RUN_COMMAND : true
    property REUSE_TAB : false
    property WINDOW_TIMEOUT : 3
    property SHELL_LOAD_DELAY : 0.2
    property SWITCH_DELAY : 0.2
    property LOG_FILE : "/tmp/alfred_ghostty/debug.log"
    property ENABLE_LOGGING : true

    on isGhosttyRunning()
    tell application "System Events" to return (application process "Ghostty" exists)
    end isGhosttyRunning

    -- Activate Ghostty
    on activateGhostty()
    try
    tell application "Ghostty" to activate
    @@ -31,15 +29,13 @@ on activateGhostty()
    end try
    end activateGhostty

    -- Check if Ghostty has open windows
    on hasGhosttyWindows()
    if not isGhosttyRunning() then return false
    tell application "System Events"
    return (count of windows of process "Ghostty") > 0
    end tell
    end hasGhosttyWindows

    -- Wait for a Ghostty window to appear
    on waitForGhosttyWindow(timeoutSeconds)
    set endTime to (current date) + timeoutSeconds
    repeat while (current date) < endTime
    @@ -49,19 +45,16 @@ on waitForGhosttyWindow(timeoutSeconds)
    return false
    end waitForGhosttyWindow

    -- Log messages to debug file
    on logError(prefix, message)
    if not ENABLE_LOGGING then return
    try
    set logMessage to "[" & (current date) & "] " & prefix & ": " & message & "\n"
    do shell script "mkdir -p /tmp/alfred_ghostty"
    write logMessage to file LOG_FILE starting at eof
    on error errMsg
    -- Silent fail to avoid log-related crashes
    end try
    end logError

    -- Clean up temporary files
    on cleanupTempFile(filePath)
    try
    do shell script "rm -f " & quoted form of filePath
    @@ -70,29 +63,26 @@ on cleanupTempFile(filePath)
    end try
    end cleanupTempFile

    -- Send command to Ghostty
    on sendCommandToGhostty(commandText, isNewlyActivated)
    if commandText is "" then
    logError("sendCommandToGhostty", "Empty command received")
    return false
    end if

    if not isNewlyActivated then delay SWITCH_DELAY
    set hadWindows to hasGhosttyWindows()
    createNewGhosttyContext(isNewlyActivated, hadWindows)

    -- Wait for shell if new session or no prior windows

    if isNewlyActivated or not REUSE_TAB or (REUSE_TAB and not hadWindows) then
    delay SHELL_LOAD_DELAY
    end if

    if not waitForGhosttyWindow(1) then
    logError("sendCommandToGhostty", "No Ghostty window found")
    display dialog "Failed to verify Ghostty window exists" buttons {"OK"} default button "OK"
    return false
    end if

    -- Direct input instead of clipboard

    tell application "System Events"
    tell process "Ghostty"
    repeat with char in characters of commandText
    @@ -106,39 +96,37 @@ on sendCommandToGhostty(commandText, isNewlyActivated)
    return true
    end sendCommandToGhostty

    -- Create new tab, window, or split based on mode
    on createNewGhosttyContext(isNewlyActivated, hadWindows)
    if isNewlyActivated then return

    set needsNewWindow to not hadWindows
    set overrideReuse to (REUSE_TAB and not hadWindows)

    tell application "System Events"
    if needsNewWindow or overrideReuse then
    keystroke "n" using command down -- New window
    keystroke "n" using command down
    return
    end if
    if not REUSE_TAB then
    if OPEN_MODE is NEW_SPLIT and hadWindows then
    keystroke "d" using command down -- New split
    keystroke "d" using command down
    else
    keystroke OPEN_MODE using command down -- New tab or window
    keystroke OPEN_MODE using command down
    end if
    end if
    end tell
    end createNewGhosttyContext

    -- Send command to Quick Terminal
    on sendToQuickTerminal(commandText, needsActivation)
    if commandText is "" then
    logError("sendToQuickTerminal", "Empty command received")
    return false
    end if

    if needsActivation then
    if not activateGhostty() then return false
    end if

    tell application "System Events"
    tell process "Ghostty"
    set viewMenu to menu 1 of menu bar item "View" of menu bar 1
    @@ -159,14 +147,13 @@ on sendToQuickTerminal(commandText, needsActivation)
    return true
    end sendToQuickTerminal

    -- Main Alfred entry point
    on alfred_script(query)
    if query is missing value or query is "" then
    logError("alfred_script", "No query provided")
    display dialog "No command provided" buttons {"OK"} default button "OK"
    return
    end if

    if OPEN_MODE is QUICK_TERMINAL then
    sendToQuickTerminal(query, not isGhosttyRunning())
    else
  4. anhkhoakz created this gist Jun 4, 2025.
    185 changes: 185 additions & 0 deletions GhosttyAlfred.applescript
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,185 @@
    -- AlfredGhostty Script v1.4.0
    -- Latest version: https://github.com/zeitlings/alfred-ghostty-script

    -- Configuration constants
    property NEW_TAB : "t" -- Open new tab
    property NEW_WINDOW : "n" -- Open new window
    property NEW_SPLIT : "d" -- Open new split
    property QUICK_TERMINAL : "qt" -- Open quick terminal
    property OPEN_MODE : NEW_TAB -- Default open mode
    property RUN_COMMAND : true -- Auto-run pasted commands
    property REUSE_TAB : false -- Reuse existing tab if true
    property WINDOW_TIMEOUT : 3 -- Seconds to wait for window
    property SHELL_LOAD_DELAY : 0.2 -- Delay for shell to load
    property SWITCH_DELAY : 0.2 -- Delay for window switching
    property LOG_FILE : "/tmp/alfred_ghostty/debug.log" -- Debug log path
    property ENABLE_LOGGING : true -- Toggle debug logging

    -- Check if Ghostty is running
    on isGhosttyRunning()
    tell application "System Events" to return (application process "Ghostty" exists)
    end isGhosttyRunning

    -- Activate Ghostty
    on activateGhostty()
    try
    tell application "Ghostty" to activate
    return true
    on error errMsg
    logError("activateGhostty", "Failed to activate Ghostty: " & errMsg)
    return false
    end try
    end activateGhostty

    -- Check if Ghostty has open windows
    on hasGhosttyWindows()
    if not isGhosttyRunning() then return false
    tell application "System Events"
    return (count of windows of process "Ghostty") > 0
    end tell
    end hasGhosttyWindows

    -- Wait for a Ghostty window to appear
    on waitForGhosttyWindow(timeoutSeconds)
    set endTime to (current date) + timeoutSeconds
    repeat while (current date) < endTime
    if hasGhosttyWindows() then return true
    delay 0.05
    end repeat
    return false
    end waitForGhosttyWindow

    -- Log messages to debug file
    on logError(prefix, message)
    if not ENABLE_LOGGING then return
    try
    set logMessage to "[" & (current date) & "] " & prefix & ": " & message & "\n"
    do shell script "mkdir -p /tmp/alfred_ghostty"
    write logMessage to file LOG_FILE starting at eof
    on error errMsg
    -- Silent fail to avoid log-related crashes
    end try
    end logError

    -- Clean up temporary files
    on cleanupTempFile(filePath)
    try
    do shell script "rm -f " & quoted form of filePath
    on error errMsg
    logError("cleanupTempFile", "Failed to remove " & filePath & ": " & errMsg)
    end try
    end cleanupTempFile

    -- Send command to Ghostty
    on sendCommandToGhostty(commandText, isNewlyActivated)
    if commandText is "" then
    logError("sendCommandToGhostty", "Empty command received")
    return false
    end if

    if not isNewlyActivated then delay SWITCH_DELAY
    set hadWindows to hasGhosttyWindows()
    createNewGhosttyContext(isNewlyActivated, hadWindows)

    -- Wait for shell if new session or no prior windows
    if isNewlyActivated or not REUSE_TAB or (REUSE_TAB and not hadWindows) then
    delay SHELL_LOAD_DELAY
    end if

    if not waitForGhosttyWindow(1) then
    logError("sendCommandToGhostty", "No Ghostty window found")
    display dialog "Failed to verify Ghostty window exists" buttons {"OK"} default button "OK"
    return false
    end if

    -- Direct input instead of clipboard
    tell application "System Events"
    tell process "Ghostty"
    repeat with char in characters of commandText
    keystroke char
    end repeat
    if RUN_COMMAND then
    keystroke return
    end if
    end tell
    end tell
    return true
    end sendCommandToGhostty

    -- Create new tab, window, or split based on mode
    on createNewGhosttyContext(isNewlyActivated, hadWindows)
    if isNewlyActivated then return

    set needsNewWindow to not hadWindows
    set overrideReuse to (REUSE_TAB and not hadWindows)

    tell application "System Events"
    if needsNewWindow or overrideReuse then
    keystroke "n" using command down -- New window
    return
    end if
    if not REUSE_TAB then
    if OPEN_MODE is NEW_SPLIT and hadWindows then
    keystroke "d" using command down -- New split
    else
    keystroke OPEN_MODE using command down -- New tab or window
    end if
    end if
    end tell
    end createNewGhosttyContext

    -- Send command to Quick Terminal
    on sendToQuickTerminal(commandText, needsActivation)
    if commandText is "" then
    logError("sendToQuickTerminal", "Empty command received")
    return false
    end if

    if needsActivation then
    if not activateGhostty() then return false
    end if

    tell application "System Events"
    tell process "Ghostty"
    set viewMenu to menu 1 of menu bar item "View" of menu bar 1
    if exists menu item "Quick Terminal" of viewMenu then
    click menu item "Quick Terminal" of viewMenu
    else
    logError("sendToQuickTerminal", "Quick Terminal menu item not found")
    return false
    end if
    repeat with char in characters of commandText
    keystroke char
    end repeat
    if RUN_COMMAND then
    keystroke return
    end if
    end tell
    end tell
    return true
    end sendToQuickTerminal

    -- Main Alfred entry point
    on alfred_script(query)
    if query is missing value or query is "" then
    logError("alfred_script", "No query provided")
    display dialog "No command provided" buttons {"OK"} default button "OK"
    return
    end if

    if OPEN_MODE is QUICK_TERMINAL then
    sendToQuickTerminal(query, not isGhosttyRunning())
    else
    set isNewlyActivated to not isGhosttyRunning()
    if not activateGhostty() then
    display dialog "Failed to activate Ghostty" buttons {"OK"} default button "OK"
    return
    end if
    if isNewlyActivated and not waitForGhosttyWindow(WINDOW_TIMEOUT) then
    logError("alfred_script", "Failed to create initial window")
    display dialog "Failed to create initial Ghostty window" buttons {"OK"} default button "OK"
    return
    end if
    sendCommandToGhostty(query, isNewlyActivated)
    end if
    end alfred_script