Re: TextEdit won't open a plain text file after writing to it with write cmd
Re: TextEdit won't open a plain text file after writing to it with write cmd
- Subject: Re: TextEdit won't open a plain text file after writing to it with write cmd
- From: Nigel Garvey <email@hidden>
- Date: Sat, 16 Jun 2018 10:45:05 +0100
ILJA SHEBALIN wrote on Fri, 15 Jun 2018 21:30:08 +0300:
> 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:
Hi Ilja.
Your script does have one or two problems and oddities. The TextEdit
save didn't work for me because the subject of e-mail I happened to
select began with "Re: " and you can't use colons in file or folder
names. The Finder changed the colon to a slash when creating the file
(in High Sierra), but the tests for the file's existence and the
destination for the save were based on the path created by the script,
which seemed to include a subfolder named "Re". But I don't see the
point of using the Finder to create the file and TextEdit to save to it
when the same thing could be achieved with the File Read/Write commands
already in the script.
Your 'display dialog' section sets the "Update" button as the cancel
button and issues an explicit cancel error if the "Cancel" button's
clicked! Result: the script stops either way.
The 'end repeat' should logically go at the end of the action so that
all selected messages are handled.
I use an e-mail client other than Mail, but the rest of the code below
works for me:
tell application "Mail" to set theMessages to (get selection)
repeat with aMessage in theMessages
tell application "Mail" to set {theSubject, theText} to {subject of
aMessage, content of aMessage}
if (theSubject contains ":") then set theSubject to
replaceSubstring(theSubject, ":", "/")
set theName to theSubject & ".txt"
set FullFilePath to (path to desktop as text) & theName
tell application "Finder"
if (file FullFilePath exists) then display dialog "The file \"" & theName
& "\" already exists." & return & return & "Do you want to update it?" buttons
{"Cancel", "Update"} default button 1 cancel button 1
-- The script stops here if the Cancel button's clicked.
end tell
writeToFile(theText, FullFilePath)
end repeat
on writeToFile(theText, HFSPath)
-- 'HFSPath as «class furl»' would be generally safer than 'file HFSPath'
here if it works in Lion too.
set FileID to (open for access file HFSPath with write permission)
try
set eof FileID to 0
write theText as «class utf8» to FileID
end try
close access FileID
end writeToFile
on replaceSubstring(theText, searchString, replaceString)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchString
set textItems to theText's text items
set AppleScript's text item delimiters to replaceString
set theText to textItems as text
set AppleScript's text item delimiters to astid
return theText
end replaceSubstring
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden