imporve apple script toggle application
[dotfiles.git] / scripts / emacs_toggle.applescript
index d3bbe3b..f034a4e 100644 (file)
@@ -1,10 +1,27 @@
+#tell application "System Events"
+#    tell application process "Emacs"
+#        if visible then
+#            set visible to false
+#        else
+#            set visible to true
+#            tell application "Emacs" to activate
+#        end if
+#    end tell
+#end tell
+#
+## http://brettterpstra.com/2011/01/22/quick-tip-applescript-application-toggle/ 
+set appName to "Emacs"
+set startIt to false
 tell application "System Events"
-    tell application process "Emacs"
-        if visible then
-            set visible to false
-        else
-            set visible to true
-            tell application "Emacs" to activate
-        end if
-    end tell
+    if not (exists process appName) then
+        set startIt to true
+    else if frontmost of process appName then
+        set visible of process appName to false
+    else
+        set frontmost of process appName to true
+    end if
 end tell
+if startIt then
+    tell application appName to activate
+end if
+