在 MacOS 上创建新的 rtf 文件即服务

Create new rtf file as a service on MacOS

提问人:PeterPefi 提问时间:10/29/2023 更新时间:10/29/2023 访问量:19

问:

我经常使用此服务在当前打开的文件夹中创建一个新的文本文件:

tell application "Finder"
    set txt to make new file at (the target of the front window) as alias
    select txt
end tell

但是,如果我能创建一个 RTF,那会更有用。然而,在谷歌搜索和 GPT 之后,我无法让它工作。有人为自己写过类似的服务吗?

macOS 服务 RTF

评论


答:

1赞 Lasse 10/29/2023 #1

它不是超级优雅,但您可以将空 RTF 文档的数据写入新创建的文件中:

tell application "Finder" to set txt to (make new file at Finder window 1) as alias
try
    set fileHandle to open for access (POSIX path of txt) with write permission
    write "{\\rtf1\\ansi\\ansicpg1252\\cocoartf2757
\\cocoatextscaling0\\cocoaplatform0{\\fonttbl}
{\\colortbl;\\red255\\green255\\blue255;}
{\\*\\expandedcolortbl;;}
\\paperw11900\\paperh16840\\margl1440\\margr1440\\vieww11520\\viewh8400\\viewkind0
}" to fileHandle
    close access fileHandle
on error err number num
    display dialog err & " number " & num buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
    try
        close access fileHandle
    end try
    return
end try

tell application "Finder" to select txt

如果您在 TextEdit 中打开它,这将导致 RTF 正常运行。