用applescript激活Emacs函数的最有效的方法 [英] Most efficient method of activating an Emacs function with an applescript

查看:199
本文介绍了用applescript激活Emacs函数的最有效的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方法来激活Emacs中的函数,而不是打开minibuffer来启动函数?需要一个延迟才能使我的手指离开控制键,以启动带有键盘快捷键的应用程序(以便Emacs不将其注册为命令),并且在输入函数名称后还有另一个延迟进入迷你缓冲区理想情况下,我想告诉Emacs,不要打开minibuffer 来运行特定功能。以下是一个简单的应用程序来激活Emacs,并使用 Mx 打开minibuffer,并键入函数的名称( dock ),然后按返回键。 Emacs目前是开放的(隐藏的或在前面的)或关闭的方法。



FYI:对于像我这样的新手程序员,这里是一个备忘录关键代码: https :/ / p>




编辑:更简单的方法是在Emacs中设置一个附加到函数(global-set-key(kbd< f6>)(lambda()(interactive)(dired/ Applications)))例如,F6来激活该功能,然后使用键代码97 作为F6键。但是,学习如何通过名称(使用applescript)来指定运行Emacs函数,而不打开minibuffer仍然是很好的。






 告诉应用程序系统事件
告诉应用程序/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/Emacs 激活
延迟.3
键代码53#escape
键代码7#x
键代码2#d
键代码31#o
键代码8#c
键代码40#k
延迟.1
键代码36#返回
结束告诉

 告诉应用程序系统事件
告诉应用程序/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/Emacs激活
延迟.3
键代码97#F6
结束告诉






strong> EDIT :修订的草案(基于@Francesco的答案) - 它要求在Emacs启动f内部(server-start) ile(例如, init.el )。 IF Emacs GUI已经运行,然后运行 emacsclient ELSE,启动Emacs GUI(暂停1秒服务器加载),然后运行 emacsclient

  on_running(appName)
告诉应用程序系统事件到(进程名称)包含appName
end is_running
set EmacsRunning to is_running(Emacs)
如果EmacsRunning然后
告诉应用程序/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/Emacs激活
做shell脚本/用户/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/bin/emacsclient -e'(dired \/ Applications\)'
else
tell应用程序/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/Emacs激活
延迟1
做shell脚本/Users/HOME/.0 .data / .0.emacs / Emacs.app / Contents / MacOS / bin / emacsclient -e'(dired \/ Applications\)'
end if


解决方案

我不知道applescript,但不能使用 emacsclient 这样做?



在现有的Emacs实例中启动服务器( Mx server-start RET ),您可以执行以下操作:

  emacsclient -e(call-interactiveively'dock)

将任意的lisp代码评估为Emacs。


Is there a better method to activate a function in Emacs with an applescript instead of opening the minibuffer to launch the function? There is a delay that is needed to take my finger off of the control key to launch the applescript with a keyboard shortcut (so that Emacs doesn't register it as a command), and there is another delay after typing the name of the function into the minibuffer. Ideally, I'd like to tell Emacs under-the-hood to run a particular function without opening the minibuffer. The following is a simple applescript to activate Emacs, and open the minibuffer with M-x, and type the name of the function (dock), and press the return key. The method should work whether Emacs is presently open (hidden or in the front) or closed.

FYI:  For the novice programmers like myself, here is a cheat-sheet for key codes:  https://apple.stackexchange.com/questions/36943/how-do-i-automate-a-key-press-in-applescript/36947#36947?newreg=9adf36816f4245d69f0900ec09588057


EDIT:  A shorter method would be to set up a keyboard shortcut within Emacs that is attached to a function (global-set-key (kbd "<f6>") (lambda () (interactive) (dired "/Applications"))) -- e.g., F6 to activate the function, and then use key code 97 for the F6 key. However, it would still be nice to learn how to specify running an Emacs function by name (with an applescript) without opening the minibuffer.


tell application "System Events"
  tell application "/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/Emacs" to activate
  delay .3
  key code 53 # escape
  key code 7 # x
  key code 2 # d
  key code 31 # o
  key code 8 # c
  key code 40 # k
  delay .1
  key code 36 # return
end tell

or

tell application "System Events"
  tell application "/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/Emacs" to activate
  delay .3
  key code 97 # F6
end tell


EDIT:  Revised draft (based on the answer by @Francesco) -- it requires that (server-start) be inside the Emacs startup file (e.g., init.el). IF Emacs GUI is already running, then run emacsclient without a pause prior thereto; ELSE, launch Emacs GUI (pause 1 second for the server to load) and then run emacsclient.

on is_running(appName)
  tell application "System Events" to (name of processes) contains appName
end is_running
set EmacsRunning to is_running("Emacs")
if EmacsRunning then
  tell application "/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/Emacs" to activate
  do shell script "/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/bin/emacsclient -e '(dired \"/Applications\")'"
else
  tell application "/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/Emacs" to activate
  delay 1
  do shell script "/Users/HOME/.0.data/.0.emacs/Emacs.app/Contents/MacOS/bin/emacsclient -e '(dired \"/Applications\")'"
end if

解决方案

I don't know about applescript, but can't you use emacsclient to do this?

Once you have started the server in your existing Emacs instance (M-xserver-startRET), you can run something like :

emacsclient -e "(call-interactively 'dock)"

to evaluate arbitrary lisp code into Emacs.

这篇关于用applescript激活Emacs函数的最有效的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆