How to create a UTF-8 encoded text file using do shell script?

How to create a UTF-8 encoded text file using do shell script? As I understand it, the command should start with do shell script. And what to write next?

Answered by DTS Engineer in 791660022

These might come in handy:

-- fetch a file's utf8 content
on FetchFileData(filepath)
	set {isopen, filedata} to {false, ""}
	try
		set fileID to (open for access (filepath as alias) without write permission)
		set isopen to true
		set filedata to read fileID from 0 to ((get eof fileID) - 1) as «class utf8»
		close access fileID
		set isopen to false
	on error message number code
		display dialog "error (" & code & ") reading file: " & message
		if isopen then close access fileID
		error message number code
	end try
	filedata
end FetchFileData

-- save a string as utf8 text
on StoreFileData(filepath, filedata)
	set isopen to false
	try
		set fileID to (open for access file filepath with write permission)
		set isopen to true
		write filedata to fileID as «class utf8»
		close access fileID
		set isopen to false
	on error message number code
		display dialog "error (" & code & ") saving file: " & message
		if isopen then close access fileID
		error message number code
	end try
end StoreFileData

-- note the « and » characters are produced by typing option-\ and option-shift-\ respectively.

The direct object of a do shell script event is the text of the shell script you want to execute. You don’t need to do anything special to get UTF-8. If you pass it a string, AppleScript will do the coercion for you. For example:

do shell script "echo 'Hello Cruel World!' ; date"

returns:

Hello Cruel World!
Tue Aug  8 09:49:10 BST 2023

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

eskimo, if there are quotes in the text ", then an error occurs. There can be any characters in my text.

AppleScript and sh each have their own quoting story. For specific cases you can generally tiptoe around the landmines. For example, in the script above I used double quotes for ApplesScript and single quotes for sh. And you can solve this:

if there are quotes in the text ", then an error occurs

by escaping the quotes:

do shell script "echo 'this is some \"quoted\" text'"

However, doing this in the general case is quite challenging. You wrote

There can be any characters in my text.

Why? Is this a file system path? Or something else?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Is this a file system path? Or something else?

It's just text that needs to be written inside a text file. The text can be anything, even hieroglyphs. A common everyday task: to write something to a text file. What surprises you here, I can't understand. Have you never written anything to a file in your life?

The text can be anything, even hieroglyphs.

OK.

It's just text that needs to be written inside a text file.

Ah, I see. The thing that confused me here is your reference to do shell script. If you want to create a text file from AppleScript, you typically don’t start there. Rather, use write:

set myText to "Hello•Cruel•World!"
write myText to POSIX file "/Users/quinn/tmp.txt"

Note that I’m using bullets in this example so that you can check that the text encoding is correct.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The thing that confused me here is your reference to do shell script.

I just don't understand AppleScript and don't understand what it's called. I write a script using Google - what I find, I write, I don't understand what is called in AppleScript and why it is needed.

write does not change the content of the text? Other commands, for example, printf, echo delete apostrophes '.

I just don't understand AppleScript

I don’t think it’s viable to teach yourself AppleScript incrementally by asking questions here on DevForums. I recommend that you find yourself a an AppleScript tutorial and work through that. AFAIK Apple doesn’t publish such a thing — there’s the AppleScript Language Guide, which I highly recommend but is not structured as a tutorial — but there are plenty of other options out there on the ’net.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

And what about this question:

does the recording not change the content of the text? Other commands, for example, printf, echo delete apostrophes '.

These might come in handy:

-- fetch a file's utf8 content
on FetchFileData(filepath)
	set {isopen, filedata} to {false, ""}
	try
		set fileID to (open for access (filepath as alias) without write permission)
		set isopen to true
		set filedata to read fileID from 0 to ((get eof fileID) - 1) as «class utf8»
		close access fileID
		set isopen to false
	on error message number code
		display dialog "error (" & code & ") reading file: " & message
		if isopen then close access fileID
		error message number code
	end try
	filedata
end FetchFileData

-- save a string as utf8 text
on StoreFileData(filepath, filedata)
	set isopen to false
	try
		set fileID to (open for access file filepath with write permission)
		set isopen to true
		write filedata to fileID as «class utf8»
		close access fileID
		set isopen to false
	on error message number code
		display dialog "error (" & code & ") saving file: " & message
		if isopen then close access fileID
		error message number code
	end try
end StoreFileData

-- note the « and » characters are produced by typing option-\ and option-shift-\ respectively.

How to create a UTF-8 encoded text file using do shell script?
 
 
Q