Topics

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

Ilya Shebalin
 

Hello,
I wrote a script that would act as an Automator service plug-in in Mail.app when placed to "Run AppleScript" action. It basically would extract the contents of a selected message along with its subject and create a plain text file with the subject as its name and the msg contents becoming the file's contents. IIf the file exists and all the statements beyond "else" clause are executed then TextEdit gives a warning of a wrong encoding ("The document "theFile.txt" could not be opened. Text encoding Unicode (UTF-8) isn’t applicable.") and refuses to open the file. The script is as follows:


property theText: ""
property theSubject:""
property theFile: missing value

tell application "Mail"
set theMessages to (get selection)
set theSubject to ""

repeat with aMessage in theMessages
set {theSubject, theText} to {subject of aMessage, content of aMessage}
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: theSubject } 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 file  FullFilePath 
quit
end tell
else
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 returned of result is "Cancel" then
error number -128
else
set theFile to  (FullFilePath as alias)
try
my writeToFile(theText, theFile)
on error number -49
close access theFile
my  writeToFile(theText, theFile)
end try
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 

.Is this an issue when you cannot open a file in an app that wasn't the one you created said file in?
OS X Lion 10.7.5

Shane Stanley
 

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

IIf the file exists and all the statements beyond "else" clause are executed then TextEdit gives a warning of a wrong encoding ("The document "theFile.txt" could not be opened. Text encoding Unicode (UTF-8) isn’t applicable.") and refuses to open the file.
You need to save the file as UTF-8 -- at the moment you're saving it in MacRoman. Change this:

write theText to FileID
to:

write theText to FileID as «class utf8»


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

Ilya Shebalin
 

Hi, Shane
I did that too, I tried every encoding that write cmd provide as a value of its as parameter. When I use "class utf8" I get an error. 

On Sat, Jun 16, 2018 at 2:25 AM Shane Stanley <sstanley@...> wrote:
On 16 Jun 2018, at 4:30 am, Ilya Shebalin <iljashebalin2@...> wrote:
>
> IIf the file exists and all the statements beyond "else" clause are executed then TextEdit gives a warning of a wrong encoding ("The document "theFile.txt" could not be opened. Text encoding Unicode (UTF-8) isn’t applicable.") and refuses to open the file.

You need to save the file as UTF-8 -- at the moment you're saving it in MacRoman. Change this:

> write theText to  FileID

to:

write theText to FileID as «class utf8»


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





Shane Stanley
 

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

When I use "class utf8" I get an error.
Are you enclosing it in « and » characters?

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

Ilya Shebalin
 

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.


On Sat, Jun 16, 2018 at 4:08 AM Shane Stanley <sstanley@...> wrote:
On 16 Jun 2018, at 11:05 am, Ilya Shebalin <iljashebalin2@...> wrote:
>
> When I use "class utf8" I get an error.

Are you enclosing it in « and » characters?

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





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>

Shane Stanley
 

On 16 Jun 2018, at 2:23 pm, Alastair Leith <qc.student.au@...> wrote:

Is it possible to maintain the rich text formatting in the text file same as email?
No.

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

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>






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>

Ilya Shebalin
 

I'm not sure I can interpret your response correctly. The second script is a tad modified version of the 1st one. On the very 1st run it creates a new text doc, on every consecutive one it offers 2 options: dismiss writing or write another text by appending it to the target file. I just ran it successfully in that it creates a file and if it's updated with writing new text it allows me to open it in TextEditor. The 1st script works too except TextEdit won't open the file. To run the second script on different file all you have to do is to change the Name variable assigning to it another word as a name.
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>




Shane Stanley
 

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

I'm not sure I can interpret your response correctly.
My apologies -- I misread your code.

I'm still not sure what the problem was with «class utf8».

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

Ilya Shebalin
 

It seems you don't own me apology, Shane. My 1st script was butchered at some point missing some lines previously present perhaps because of numerous copy pastes between ScriptEditor and Mail messages in reply to this thread. A hidden issue manifested when AppleScript traditionally failed on ":" character (it bothers me, seriously; they could implement some solutions to AS in that regard. Annoying). I'm working on it right now.
16.06.2018, в 15:46, Shane Stanley написал(а):

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

I'm not sure I can interpret your response correctly.
My apologies -- I misread your code.

I'm still not sure what the problem was with «class utf8».

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




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>






Shane Stanley
 

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

write theText to FileID as "class utf8"

You're still using quote marks instead of chevrons -- they are not the same things.

write theText to FileID as «class utf8»

Can you see the difference?



Ilya Shebalin
 

I see the difference. I just have to figure out how to make quotes into as chevrons in precompiled AS code because my default locale is not en_EN. Maybe that's the cause of the error. 
17.06.2018, в 9:52, Shane Stanley написал(а):

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

write theText to FileID as "class utf8"

You're still using quote marks instead of chevrons -- they are not the same things.

write theText to FileID as «class utf8»

Can you see the difference?




Shane Stanley
 

On 18 Jun 2018, at 2:39 am, Ilya Shebalin <iljashebalin2@...> wrote:

I just have to figure out how to make quotes into as chevrons in precompiled AS code because my default locale is not en_EN. Maybe that's the cause of the error.
Go here and download a copy of Script Debugger: <https://latenightsw.com/sd7/download/>. Open one of your working scripts, then go to the View Menu and choose Show Raw (Chevron) Syntax. That should show you what you need.


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

Ilya Shebalin
 

Hello again, Shane
That's how the as "class utf8" compiles in ScriptDebugger:
«class as  »:"class utf8"

18.06.2018, в 2:50, Shane Stanley написал(а):

On 18 Jun 2018, at 2:39 am, Ilya Shebalin <iljashebalin2@...> wrote:

I just have to figure out how to make quotes into as chevrons in precompiled AS code because my default locale is not en_EN. Maybe that's the cause of the error.

Go here and download a copy of Script Debugger: <https://latenightsw.com/sd7/download/>. Open one of your working scripts, then go to the View Menu and choose Show Raw (Chevron) Syntax. That should show you what you need.


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






Shane Stanley
 

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

That's how the as "class utf8" compiles in ScriptDebugger:
Type this into a Script Debugger window:

current date

Compile, then go to the View Menu and choose Show Raw (Chevron) Syntax, and compile again. What do you see now?

Also, what language is your computer set to?

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

Ilya Shebalin
 

current date compiles into «event miscued» in SD and my Mac is set to Russian as default language.
18.06.2018, в 8:33, Shane Stanley написал(а):

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

That's how the as "class utf8" compiles in ScriptDebugger:

Type this into a Script Debugger window:

current date

Compile, then go to the View Menu and choose Show Raw (Chevron) Syntax, and compile again. What do you see now?

Also, what language is your computer set to?

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






Ilya Shebalin
 

actually should've been «event misccurd» (auto-correction in action stood in the way)
18.06.2018, в 8:33, Shane Stanley написал(а):

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

That's how the as "class utf8" compiles in ScriptDebugger:

Type this into a Script Debugger window:

current date

Compile, then go to the View Menu and choose Show Raw (Chevron) Syntax, and compile again. What do you see now?

Also, what language is your computer set to?

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