Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

将 ID 属性添加到目录中的 Org 模式文件

Posted on 2024-12-02

标志.webp

这有点深奥,但我还是把它写在这里。

我曾多次在使用Denote和Org-roam进行 Org 模式笔记之间切换。它们大多是兼容的,所以这并不算太麻烦。

我需要做的一件事是确保所有 .org 文件在顶部都包含 :ID: 属性,以便 Org-roam 将它们包含在其数据库中。 ID 属性如下所示:

 :PROPERTIES: :ID: 4def7046-3ef8-4436-a079-09362bf66aff :END:

我的许多 Denote 文件已经包含此内容,因为我喜欢将文件和图像拖放到笔记中,并且 ID 属性使此功能可以正常工作。但可能有 300 个文件没有 ID,我不想为每个文件手动添加属性。

我开始编写一个 shell 脚本来执行此操作,但对一些语法感到困惑,因此我搜索了错误并找到了修复方法。然后我在sed命令等方面遇到了麻烦。

有时我会忘记 ChatGPT 的存在。有时我会忽略它,因为我想自己学习东西。但有时我只是想要答案,尽管法学硕士可能存在问题,但不可否认它们的有用性。

经过几次提示、几次迭代,我在大约 10 分钟内就得到了我需要的东西。

 #!/bin/bash # Directory containing the Org-mode files ORG_DIR = " ${ 1 :- . } " # Defaults to current directory if not provided   
# Loop through all .org files in the specified directory for file in " $ORG_DIR " /*.org; do # Skip if no Org-mode files are found [ -e " $file " ] || continue   
# Check if the :ID: property exists in the first 5 lines if ! head -n 5 " $file " | grep -q "^:ID:" ; then # Generate a unique ID (UUID) id = $( uuidgen )   
# Create the :PROPERTIES: block with the :ID: properties_block = ":PROPERTIES:\n:ID: $id \n:END:"   
# Insert the :PROPERTIES: block at the very top of the file gsed -i "1i $properties_block " " $file "   
 echo "Added :ID: $id inside a :PROPERTIES: block to $file " else echo ":ID: already exists in the first 5 lines of $file " fi done

我需要做的唯一更改是用gsed替换sed ,因为 macOS 版本的 sed 缺少开关并引发错误。

当我在那里时,我向 ChatGPT 询问了 Emacs lisp 中的脚本版本,以防万一。我让它编写了在 Dired 缓冲区中使用的函数,因为那是我最可能需要它的地方。这是函数…

 (defun add-id-to-org-files-in-dired () "Add :ID: property inside a :PROPERTIES: block at the top of all Org files in the current Dired buffer." (interactive) ;;(require 'uuidgen) ;; Ensure `uuidgen` is available (dired-map-dired-file-lines (lambda (file) (when (and (string-suffix-p ".org" file t ) (file-exists-p file)) (with-temp-buffer (insert-file-contents file) ;; Check the first 5 lines for an existing :ID: property (unless (save-excursion (goto-char (point-min)) (search-forward ":ID:" (line-end-position 5 ) t )) ;; Insert the :PROPERTIES: block at the top of the file (goto-char (point-min)) (insert ":PROPERTIES:\n:ID: " (org-id-uuid) "\n:END:\n\n" ) (write-region (point-min) (point-max) file)) (message "Added :ID: to %s" file))))))

完成了,完成了。可能有更好、更干净的方法来做到这一点,但我的问题已经解决,这就是我想要的。

原文: https://baty.net/2024/11/adding-an-id-property-to-org-mode-files-in-directory/

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Colin Percival
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Dustin Curtis
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Liz Danzico
  • Lou Plummer
  • Luke Wroblewski
  • Matt Baer
  • Matt Stoller
  • Matthias Endler
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Rohit Patel
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • Understanding AI
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme