如何通过 AppleScript 以纯文本形式创建新的 TextEdit 窗口而不保存它?

问题描述 投票:0回答:2

有没有更完美的解决方案:

tell application "TextEdit"
set Text1 to "my text and so on…"

activate

set theDoc to make new document

tell application "System Events" to tell process "TextEdit"
    keystroke "t" using {shift down, command down}
end tell

delay 1

tell theDoc
    set text of theDoc to Text1
end tell
end tell

例如通过使用属性:

set Text1 to "my text and so on…"
tell application "TextEdit"activate
set theDoc to make new document with properties {text:Text1, Content:text / plain} --doesnt work
tell theDoc -alternative way
    set text of theDoc as plain text
end tell
end tell
text window applescript document textedit
2个回答
0
投票

您可以像这样阅读 TextEdit 默认值:

defaults read com.apple.TextEdit  

输出

{
    NSNavLastRootDirectory = "~";
    NSNavPanelExpandedSizeForOpenMode = "{800, 448}";
    NSNavPanelExpandedSizeForSaveMode = "{800, 448}";
    "NSWindow Frame NSNavPanelAutosaveName" = "560 473 800 388 0 0 1920 1055 ";
    PlainTextEncoding = 4;
    PlainTextEncodingForWrite = 4;
    RichText = 0;
    TextReplacement = 0;
}

参数

RichText = 0
表示它将以“纯文本”格式创建新文档。


您可以将默认值更改为“富文本”

defaults delete com.apple.TextEdit RichText

您可以将默认值更改为“纯文本”

defaults write com.apple.TextEdit RichText -int 0

0
投票

一点 GUI 脚本就可以了……

set Text1 to "my text and so on…"
tell application "TextEdit" to activate
set theDoc to make new document with properties {text:Text1}
tell application "System Events"
    tell process "TextEdit"'s menu bar 1's menu "Format"
        if menu item "Make Plain Text" exists then
            click menu item "Make Plain Text"
        end if
    end tell
end tell
© www.soinside.com 2019 - 2024. All rights reserved.