improve function
[dotfiles.git] / bin / emacs_toggle.applescript
1
2 ## this scirpt works together with Quicksilver
3 ## copy/link this file to ~/Library/Application Support/Quicksilver/Action
4 ## and set a toggle event on Quicksilver preference
5 #
6 ## http://brettterpstra.com/2011/01/22/quick-tip-applescript-application-toggle/ 
7 #
8 #
9 #Version 1.0 toggle emacs front
10
11 set appName to "Emacs"
12 set startIt to false
13 tell application "System Events"
14     if not (exists process appName) then
15         set startIt to true
16     else if frontmost of process appName then
17         set visible of process appName to false
18     else
19         set frontmost of process appName to true
20     end if
21 end tell
22 if startIt then
23     tell application appName to activate
24 end if
25
26 ## version 2.0 - using emacs daemon and client
27 ##set appName to "Emacs"
28 ##set startIt to false
29 ##tell application "System Events"
30 ##    try
31 ##        -- we look for <= 2 because Emacs --daemon seems to always have an entry in visibile-frame-list even if there isn't
32 ##        set frameVisible to do shell script "/usr/local/bin/emacsclient -e '(<= 2 (length (visible-frame-list)))'"
33 ##        if frameVisible is not "t" then
34 ##            -- there is a not a visible frame, launch one
35 ##            do shell script "/usr/local/bin/emacsclient -c -n"
36 ##            set startIt to true
37 ##        else if frontmost of process "Emacs" then
38 ##            set visible of process "Emacs" to false
39 ##        else
40 ##            set frontmost of process "Emacs" to true
41 ##        end if
42 ##    on error
43 ##        -- daemon is not running, start the daemon and open a frame           
44 ##        do shell script "/usr/local/bin/emacs --daemon"
45 ##        do shell script "/usr/local/bin/emacsclient -c -n"
46 ##        set startIt to true
47 ##    end try
48 ##end tell
49 ##
50 ##-- bring the visible frame to the front
51 ##if startIt then
52 ##    tell application "Emacs" to activate
53 ##end if