欢迎光临散文网 会员登陆 & 注册

macOS下的emacs启动脚本,加速文件打开(基于AppleScript实现)

2020-05-20 18:36 作者:杜衡图南  | 我要投稿

一、 介绍与程序原理

在emacs中,我们可以使用“emacs --daemon”命令来启动emacs的守护程序,以提高以后打开emacs的速度。但是在命令行中执行上述命名会使命令行处于占用模式,不方便后续操作,因此需要使用脚本后台创建一个守护程序。

创建守护程序以后,再打开文件就可以使用“emacsclient -c -n + 文件名” 命令在新的frame中打开文件,同时也可以使用“emacsclient -c -n”创建一个空白的frame。

然而emacs安装以后默认创建的应用程序使用的emacs命令启动,而并非使用emacsclient启动,在macOS系统中,可以使用“automator(自动操作)”工具,方便的创建脚本,实现上述功能。

另外,如果使用zotero配合org-mode来做笔记,会发现默认情况下在zotero添加的以org为后缀名的附件,并不能双击启动,因此,需要创建下方的客户端启动脚本,然后配合Mac系统下的“预设应用程序”工具,将org后缀名全部通过自己创建的客户端启动脚本启动,就可以实现双击打开org附件的功能。


二、创建守护脚本

  1. 在macOS的启动器中启动自动操作工具(如图1)

图1 自动操作工具

2. 在打开的窗口中选择“应用程序”(如图2)

图2 选择应用程序

3. 搜索“AppleScript”并将它拖到主界面。

图3 创建一个AppleScript

4. 删除框中全部内容,并写入如下内容:

tell application "Terminal"

do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon >/dev/null 2>&1 &"

end tell

图4 输入脚本代码


5. 保存脚本

执行“文件-存储”,将脚本保存到“应用程序”文件夹下。这里的文件名可以自定义。后面就用这个文件名启动emacs守护程序。


三、创建客户端启动脚本

重复以上 1-3 步,在第四步中换成如下脚本代码

on run argv
set temp to item 1 of argv as string
if temp is equal to "" then
set real_Path to " " as string
display notification with title "Open Emacs"
else
set POSIX_Path to item 1 of argv as string
set real_Path to POSIX path of POSIX_Path as string
display notification real_Path with title "Open in Emacs..."
end if

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
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n " & real_Path
end if
on error
do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon >/dev/null 2>&1 &"
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n " & real_Path
end try
end tell
tell application "Emacs" to activate
end run

完成后执行上方第5步,命名并保存。启动程序创建完毕。


 四、小结

为了进一步提高emacs效率,我们可以在“系统设置-用户与群组-登陆项”中将守护脚本添加进来。这样每次启动电脑后,将自动启动emacs服务程序。

本文对于带参数传入的AppleScript脚本的编写,有参照价值。并且在脚本编程中应用了POSIX知识。


五、 参考资料

  1. Aaron5, 2012. OS X下的Emacs Deamon模式设置 - Nil - ITeye博客[EB/OL](2012–11–13)[2020–05–20]. https://www.iteye.com/blog/clojure-1724368.

  2. Apple, 2016. Introduction to AppleScript Language Guide[EB/OL](2016–01–25)[2020–05–20]. https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html.


macOS下的emacs启动脚本,加速文件打开(基于AppleScript实现)的评论 (共 条)

分享到微博请遵守国家法律