How to create a new TextEdit-window in plain Text by AppleScript without saving it?

68 views Asked by At

Is there a more perfect solution for that:

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

for example by using properties:

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
3

There are 3 answers

0
Mark Setchell On

You can read the TextEdit defaults like this:

defaults read com.apple.TextEdit  

Output

{
    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;
}

The parameter RichText = 0 means it will create new documents in "Plain Text" format.


You can change the default to "Rich Text" with:

defaults delete com.apple.TextEdit RichText

You can change the default to "Plain Text" with:

defaults write com.apple.TextEdit RichText -int 0
1
Ted Wrigley On

A little GUI scripting works…

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
0
Tom On

Thanks for your hints guys. I finished my project in old school like this:

set Tempclipdata to the clipboard

… my script and then the final: set the clipboard to DatatextT do shell script "pbpaste | pbcopy" -- to get it unformatted

tell application "System Events" to tell process "TextEdit"
    delay 0.2
    keystroke "a" using {command down}
    keystroke "v" using {command down}
end tell

delay 1 set the clipboard to Tempclipdata

but sometimes the pasteboard handler is corrupted and TextEdit needs a softstart. The solution to set an reset the defaults of TextEdit is much more professional, but as I would have to do so approx 40 times each day, when I use my script, I believe my old school solution is the one I prefer until there will be added a properties routine within TextEdit.

Thanks a lot: Thomas