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


Ilya Shebalin
 

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


property theText : ""
property theFile : missing value
property theSubject : ""
tell application "Mail"


set TheNotes to (get selection)
repeat with aNote in TheNotes
set {theText, theSubject} to {content of aNote, subject of aNote}
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


end tell
else
set theFile to (FullFilePath as alias)
set theText to theText
display dialog "The file already exists." & return & return & "Do you want to update it?" buttons {"Cancel", "Update"} default button  1 cancel button 2
if button rif button returned of result is "Cancel" 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
close access FileID
end writeToFile

===========================================

I wrote another test script. Interestingly enough it let's me open with TextEdit just fine. Here it is:


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


set theName to "102"
set theExtension to "txt"
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 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
#save myDoc in file FullFilePath
close myDoc saving in (FullFilePath as alias)


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, в 4:24, Shane Stanley написал(а):

On 16 Jun 2018, at 11:12 am, Ilya Shebalin <iljashebalin2@...> wrote:

Ooops...Through your question I think I'm onto smth. Yes I enclose it in quotes - just as given in the AppleScript guide I'm reading.

The « and » characters are quotes in some languages, but they have a special meaning in AppleScript. You have to enter them exactly.

It would help trouble-shoot if you copied and pasted your code here directly.

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






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