【技巧分享+问题已解决】 macOS 快捷指令翻译段落

siri-shortcuts.el 使用的是 URL Scheme 方案,不如终端下的 shortcuts 好用,所以想用临时文件来传递数据,以实现用快捷指令里的翻译功能翻译句子。

刚才测试了一下 macOS 的 shortcuts 发现传入文件路径的写法是绝对路径。

如何把 buffer 中的部分内容输出到临时文件,并把临时文件的绝对路径传递给命令行工具?

有没有创建临时文件并把绝对路径传递给命令行的可参考案例啊?

谢谢各位。

shortcuts run "Translate File" -i "~/test.txt" -o "~/test-t.txt"
Error: The file “test.txt” couldn’t be opened because there is no such file.

# 只能写成以下形式,但是找不到它 `-o` 输出的文件

shortcuts run "Translate File" -i $HOME/test.txt   
shortcuts run "Translate File" -i "$HOME/test.txt"   
shortcuts run "Translate File" -i "test.txt" -o "test-t.txt"
       

当前进度

DONE 基本功能已完成

DONE 禁止临时 buffer 的弹出

DONE 自动跳转到段落末尾

安装使用

需安装快捷指令:

从 iCloud 安装

https://www.icloud.com/shortcuts/2b329031a5a34e62b914afee915b388b

或下载文件后用快捷指令.app导入

不放心的可以按图自己手搓,并将快捷指令命名为 Translate File

Emacs 设置

;; Siri Shortcuts: Translate
;; {{{
(add-to-list 'display-buffer-alist
  (cons "\\*Async Shell Command\\*.*" (cons #'display-buffer-no-window nil)))
(defun my/siri-translate ()
  (interactive)
  (let ((tempfile
         (make-temp-file "siri-translate-" nil ".txt") ; temp file
	 ))
    (write-region (format "%s" (thing-at-point 'paragraph)) nil tempfile)
    (end-of-paragraph-text)
    (shell-command (format "shortcuts run \"Translate File\" -i %s &" tempfile))
    )
  (shell-command "open -b org.gnu.Emacs")
  )
(keymap-global-set "C-c t" #'my/siri-translate)
;; }}}

视频演示

参考资料

目标是把这段代码改写一下,去掉插件依赖。

(require 'siri-shortcuts)
(defun my/siri-translate ()
  (interactive)
  (siri-shortcuts-browse-url 'run-shortcut "EmacsTranslate" "text" (thing-at-point 'paragraph t))
  )
(keymap-global-set "C-c t" #'my/siri-translate)

未完成/有 bug

(thing-at-point 'sentence) 保存到临时文件,并把绝对路径传递给 shell-command

(defun my/siri-translate ()
  (interactive)
  (shell-command-to-string  (format "shortcuts run \"Translate File\" -i %s"
                                    (make-temp-file
                                     "siri-translate-" nil ".txt"
                                     '(insert  (thing-at-point 'sentence t))
                                     )
                                    ))
  (do-applescript "tell application id \"org.gnu.Emacs\" to activate")
  )
(keymap-global-set "C-c t" #'my/siri-translate)

make-temp-file

make-temp-file is a byte-compiled Lisp function in ‘files.el’.

(make-temp-file PREFIX &optional DIR-FLAG SUFFIX TEXT)

Create a temporary file.
The returned file name (created by appending some random characters at the end
of PREFIX, and expanding against ‘temporary-file-directory’ if necessary),
is guaranteed to point to a newly created file.
You can then use ‘write-region’ to write new data into the file.

If DIR-FLAG is non-nil, create a new empty directory instead of a file.

If SUFFIX is non-nil, add that at the end of the file name.

If TEXT is a string, insert it into the new file; DIR-FLAG should be nil.
Otherwise the file will be empty.

  Other relevant functions are documented in the file group.
  Probably introduced at or before Emacs version 21.1.

我水平太差了……

水平差可以先不要用shell-command ,我现在执行命令行程序为了 看到执行过程,都还是先单独打开一个buffer在里面执行

(defun my/shell-test ()
  (interactive)
  (shell "*shell-shortcuts*")
  (insert "输入要执行的命令")
  (comint-send-input);;回车
  (insert "输入要执行的命令2")
  (comint-send-input);;回车
)

我的过程其实只有一步,就是 shell-command。

刚闹明白 make-temp-file,但是不知道怎么传给 shell-command

(make-temp-file "siri-translate" nil ".txt"
		'(insert  (thing-at-point 'sentence t))
                  )

Bonus

搭配 OCR 一起食用风味更佳

注意这句话的光标已经移动到temp file的buffer里了,里边空空如也,thing-at-point 会返回nil。

进入temp 之前先获取内容试试?以下我随便打的,只是个思路,没有任何测试:

(let ((thing (thing-at-point 'sentence t))
  (make-temp-file "siri-translate" nil ".txt"
   (insert thing)))

:laughing: 你来晚了,我已经换思路搞定了。

虽然还是一知半解,不过功能已经基本完善了。

更新一个中译英快捷指令,就不单独发帖了。

https://www.icloud.com/shortcuts/f9bee8cad02c4c62a419c74066e28a84

(defun my/siri-translate2english ()
  (interactive)
  (let
      ((tempfile
        (make-temp-file "siri-translate-" nil ".txt")
        ))
    (write-region
     (format "%s" (thing-at-point 'paragraph))
     nil
     tempfile)
    (end-of-paragraph-text) ; jump to end of paragraph
    (shell-command (format "shortcuts run \"Translate File 2 English\" -i %s &" tempfile))
    )
  (shell-command "open -b org.gnu.Emacs")
  )

(keymap-global-set "C-c e" #'my/siri-translate2english)