diff --git a/emacs-client-mac.scpt b/emacs-client-mac.scpt index b0d74b7..77f7c7f 100644 --- a/emacs-client-mac.scpt +++ b/emacs-client-mac.scpt @@ -1,17 +1,37 @@ -tell application "Terminal" - try - -- we look for <= 2 because Emacs --daemon seems to always have an entry in visibile-frame-list even if there isn't - set frameVisible to do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -e '(<= 2 (length (visible-frame-list)))'" - if frameVisible is not "t" then - -- there is a not a visible frame, launch one - do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n" - end if - on error - -- daemon is not running, start the daemon and open a frame - do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon" - do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n" - end try -end tell +on run + runclient("") +end run --- bring the visible frame to the front -tell application "Emacs" to activate \ No newline at end of file +on open argv + set filename to quoted form of POSIX path of item 1 of argv + runclient(filename) +end open + +on runclient(filename) + tell application "System Events" to set isRunning to (name of processes) contains "Terminal" + + tell application "Terminal" + try + set frameVisible to do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -e '(<= 2 (length (visible-frame-list)))'" + if frameVisible is not "t" then + -- there is a not a visible frame, launch one + do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n " & filename + else if filename is not "" then + -- there is a viible frame, just open a file in exiting one + do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n " & filename + end if + on error + -- daemon is not running, start the daemon and open a frame + do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon" + do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n " & filename + end try + end tell + + -- bring the visible frame to the front + tell application "Emacs" to activate + + -- close terminal + if not isRunning then + tell application "Terminal" to quit + end if +end runclient \ No newline at end of file