使用 Applescript 激活 Emacs 功能的最有效方法 [英] Most efficient method of activating an Emacs function with an applescript
问题描述
是否有更好的方法来使用 AppleScript 激活 Emacs 中的函数,而不是打开 minibuffer 来启动该函数?需要一个延迟才能将我的手指从控制键上移开以使用键盘快捷键启动 applescript(以便 Emacs 不会将其注册为命令),并且在键入函数名称后还有另一个延迟进入小缓冲区.理想情况下,我想告诉 Emacs 在后台运行特定功能而无需打开迷你缓冲区.下面是一个简单的applescript来激活Emacs,用M-x
打开minibuffer,输入函数名(dock
),然后按回车键.无论 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.
仅供参考:对于像我这样的新手程序员,这里有一份关键代码备忘单:https://apple.stackexchange.com/questions/36943/how-do-i-automate-a-key-press-in-applescript/36947#36947?newreg=9adf36816f4245d69f0900ec09588057
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:一种更简短的方法是在 Emacs 中设置一个附加到函数 (global-set-key (kbd "
-- 例如,F6 激活功能,然后使用 key code 97
作为 F6 键.然而,学习如何在不打开迷你缓冲区的情况下通过名称(使用 Applescript)指定运行 Emacs 函数仍然会很好.
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
或
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:修订草案(基于@Francesco 的回答)——它要求 (server-start)
位于 Emacs 启动文件中(例如,<代码>init.el).如果 Emacs GUI 已经在运行,那么运行 emacsclient
之前没有停顿;否则,启动 Emacs GUI(暂停 1 秒让服务器加载),然后运行 emacsclient
.
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
推荐答案
我不知道applescript,但是你不能用emacsclient
这样做?
I don't know about applescript, but can't you use emacsclient
to do this?
在现有 Emacs 实例 (Mxserver-start
RET) 中启动服务器后,您可以运行以下内容:
Once you have started the server in your existing Emacs instance (M-xserver-start
RET), you can run something like :
emacsclient -e "(call-interactively 'dock)"
将任意 lisp 代码评估为 Emacs.
to evaluate arbitrary lisp code into Emacs.
这篇关于使用 Applescript 激活 Emacs 功能的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!