Re: TextEdit won't open a plain text file after writing to it with write cmd


Ilya Shebalin
 

Here's slightly modified the original 1st script:
#########The script No.1. I got the coercion error number -1700 ("couldn't coerce the_text to "class salc"")
property theText : ""
property theFile : missing value
property theSubject : ""
tell application "Mail"


set TheNotes to (get selection)
repeat with aNote in TheNotes
if subject of aNote contains ":" then set CorrectedSubject to my FindandReplace(":", space, subject of aNote)
set {theText, theSubject} to {content of aNote, CorrectedSubject}


end repeat
end tell
tell application "Finder"
set theName to theSubject & ".txt"
set FullFilePath to ((path to desktop as text) & theName)
if not (file FullFilePath exists) then
set theDoc to make new document file with properties {name:theName, displayed name:theName} at folder "Desktop" of home
tell application "TextEdit"
activate
set MyDoc to make new document with properties {text:theText, name:theName} at end of documents
close MyDoc saving in (FullFilePath as alias)
quit
end tell
else
set theFile to (FullFilePath as alias)
set theText to theText
set theDialog to display dialog "The document already exists. " & return & "Would you like to update it?" buttons {"No", "Yes"} default button 2 cancel button 1
if button returned of result is "No" then
error number -128
else
try
my writeToFile(theText, theFile)
on error number -49
close access theFile
end try
my writeToFile(theText, theFile)
end if
end if
end tell

on writeToFile(theText, theFile)
set FileID to open for access theFile with write permission
set eof FileID to 0
write theText to FileID as "class utf8"
close access FileID
end writeToFile

on FindandReplace(SearchItem, ReplacementItem, theString)
get theString
set AppleScript's text item delimiters to SearchItem
set TempText to text items of theString
set AppleScript's text item delimiters to ReplacementItem
set theString to TempText as text
end FindandReplace



#################The script No.2. A rough version of the 1st one, for the sake of keeping bare bone structure only. Runs flawlessly.

property theText : ""
property theFile : missing value
tell application "Finder"


set theName to "103" --can be changed to any name
set theExtension to "txt" --can be changed to any extension
set FullFilePath to (((path to desktop) as text) & theName & "." & theExtension)
if not (file FullFilePath exists) then
set theText to "This is test of writing text to a file and closing access to it."
set theDoc to make new document file with properties {name:theName & "." & theExtension, displayed name:theName} at folder "Desktop" of home
tell application "TextEdit"
activate


set myDoc to make new document with properties {text:theText, name:theName & "." & theExtension} at end of documents
close myDoc saving in (FullFilePath as alias)

quit

end tell
else
set theFile to FullFilePath as alias
set theText to "This is another text"


set theDialog to display dialog "The document already exists. " & return & "Write another text to it?" buttons {"No", "Yes"} default answer "" default button 2 cancel button 1
if button returned of result is "No" then
error number -128
else
set theText to text returned of theDialog
my writeUTF16(theText, theFile)
end if
end if
end tell

on writeUTF16(theText, theFile)
set FileID to open for access theFile with write permission
set theEnd to get eof FileID
write theText to FileID as Unicode text starting at theEnd
close access FileID
end writeUTF16





16.06.2018, в 9:51, Shane Stanley написал(а):

On 16 Jun 2018, at 3:40 pm, Ilya Shebalin <iljashebalin2@...> wrote:

Here's my script (copy pasted as plain text) and another following that one.

I'm no longer sure what your question is. Neither use the suggested code. And the second one is problematic if the file already exists and is longer than the text you're writing.

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>






Join applescript@apple-dev.groups.io to automatically receive all group messages.