Skip to content

Instantly share code, notes, and snippets.

@mahmoudimus
Last active January 17, 2025 18:24
Show Gist options
  • Save mahmoudimus/6354b85c8793c1044ed9e3472b8b1485 to your computer and use it in GitHub Desktop.
Save mahmoudimus/6354b85c8793c1044ed9e3472b8b1485 to your computer and use it in GitHub Desktop.
An AppleScript that allows Automator to open a file via emacsclient
-- Function to check if a file exists and is executable
on fileExistsAndExecutable(thePath)
try
do shell script "test -x " & quoted form of thePath
return true
on error
return false
end try
end fileExistsAndExecutable
on run {input, parameters}
-- this was a shell script before that ran `/usr/local/bin/emacsclient --no-wait -c -a emacs "$@" >/dev/null 2>&1 &`
-- Define paths to emacsclient and emacs
set emacsClientPath to "/opt/homebrew/bin/emacsclient"
set emacsFallbackPath to "/Applications/Emacs.app/Contents/MacOS/Emacs"
-- Determine which emacsclient to use
if fileExistsAndExecutable(emacsClientPath) then
set selectedEmacsClient to emacsClientPath
else if fileExistsAndExecutable(emacsFallbackPath) then
set selectedEmacsClient to emacsFallbackPath
else
display dialog "emacsclient not found at " & emacsClientPath & " or " & emacsFallbackPath buttons {"OK"} default button "OK"
return
end if
-- Check if there are <= 2 visible frames
try
set frameVisible to do shell script quoted form of selectedEmacsClient & " -e '(<= 2 (length (visible-frame-list)))'"
on error errMsg
display dialog "Error checking visible frames:
" & errMsg buttons {"OK"} default button "OK"
return
end try
if frameVisible is not "t" then
-- Launch a new Emacs frame
try
do shell script quoted form of selectedEmacsClient & " -c -n >/dev/null 2>&1 &"
on error errMsg
display dialog "Error launching Emacs frame:
" & errMsg buttons {"OK"} default button "OK"
return
end try
end if
-- Iterate through each file passed to the application
repeat with aFile in input
set filePath to POSIX path of aFile
-- Debug: Log the file being processed
-- log "Attempting to open file: " & filePath
try
-- Construct the emacsclient command with debug information
set emacsCommand to quoted form of selectedEmacsClient & " --no-wait -a emacs " & quoted form of filePath
-- Debug: Log the command being executed
-- log "Executing command: " & emacsCommand
-- Optionally, display the command in a dialog for immediate feedback (can be commented out in production)
-- display dialog "Executing command: " & emacsCommand buttons {"OK"} default button "OK"
-- Open the file with emacsclient using the specified command
do shell script emacsCommand
-- Debug: Confirm successful execution
-- log "Successfully executed emacsclient for file: " & filePath
on error errMsg
-- Debug: Log the error message
-- log "Error opening file: " & filePath & " - " & errMsg
-- Display a dialog with the error information
display dialog "Error opening file: " & filePath & return & errMsg buttons {"OK"} default button "OK"
end try
end repeat
-- Bring Emacs to the foreground
try
-- Check if Emacs application exists
tell application "System Events"
if exists application process "Emacs" then
tell application "Emacs" to activate
else
do shell script "open -a Emacs"
delay 1 -- Wait a moment for Emacs to launch
tell application "Emacs" to activate
end if
end tell
on error errMsg
display dialog "Error bringing Emacs to foreground:
" & errMsg buttons {"OK"} default button "OK"
end try
return input
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment