Getting crazy with Applescript Documentation: UTF8 x Unicode

Hi,

I am trying to deal with a POSIX PATH that contains foreigner characters, like those in Western Europe languages. Something like

“/Users/Bob/Mon Fólio €”

as you noticed there are two problems with the last part of the path…

  1. an accented letter ó
  2. an euro sign…

Of course these are Unicode Text.

If I open the terminal and type cd to the path above, terminal will show it like

cd /Users/Bob/Mon F\303\263lio \342\202\254

ó will be coded like \303\263
and the euro sign as \342\202\254

Can someone tell me how to convert the POSIX PATH (accented letters, normal letters, spaces, symbols, anything that is allowed in a directory name) to the equivalent “slash” code???

I know that this have something to do with UTF8, cause terminal just accepts this encoding, but how on earth can I convert that? I have no idea…

I have tryed to put a command like

set x to POSIX PATH of MyPath as «class utf8»

but Applescript spit on me…

Can someone help me please???
I am desperate! :slight_smile:

regards

This works here:

set x to (choose folder) --> containing any characters
do shell script "ls " & quoted form of POSIX path of x

Works fine with the 4-byte Unicode char you posted previously (in my machine) :wink:

About the display in the Terminal window hex-against-decimal, seems that it’s not decimal representation, but octal :stuck_out_tongue:

:smiley:

YESSS JJ,
You are a master!!!
I had to do just a little modification,

set x to (choose folder) → containing any characters
do shell script "ls " & quoted form of POSIX path of x as Unicode Text

That’s working fine, but now I have another problem a few lines down… :slight_smile:

I am trying to write to a file on a directory with those strange chars… so I have the lines…

set fconfig to ((this_directory as XXXX) & “.config”)
set nref to open for access file fconfig with write permission
write (name&code) to file fconfig
close access nref

where XXXX I have tested with: string, unicode text and alias… none worked.
The error is in this first line…

I have typed also the invert syntax, like in

set fconfig to file “.config” of this_directory

did not worked also!!!

How to create a new file on a unicode directory??? I am getting crazy… bl bl blu blu…

thanks! :slight_smile:

Yes, Unicode support in AS is not still a full reality… This is the workaround:

set f to POSIX file ((POSIX path of (choose folder)) & "config")

set nref to open for access f with write permission
write "foo" to nref
close access nref

Hi JJ, thanks again!!!
This works perfectly unless I use this on the following context…

suppose I am developing a folder action to process files dropped to one directory, so, in the first line of this action I will have to have

on adding folder items to this_folder after receiving all_items
set f to POSIX file ((POSIX path of (this_folder)) & “config”)
set nref to open for access f with write permission
write “foo” to nref
close access nref

again, the second line gives me an error!!! grrrrrrr…

I have tried the second line also as

set f to POSIX file ((POSIX path of (name of this_folder)) & “config”)
and
set f to POSIX file ((POSIX path of (this_folder as alias)) & “config”)

but nothing seems to work… I am stuck on this line…

I have tested the first part of the command, i.e.,

POSIX path of (this_folder) and it returns the string correctly,
when I add the second part, i.e,
POSIX file of (…
I have the error…

POSIX file of IS NOT CONVERTING THE PATH correctly.

I know I am bothering you too much, but, if in your wisdom as the master of the universe, if you can help me…
Thankkkkkkkkkkkkkksssssssssssssssssssss!!!

p.s. - note: can POSIX file make a reference to a file that do not exists? remember that the file config is about to be created…

Yes.

It works fine here… Hmmm… Are you sure your Folder Actions are working OK? What are your results with this code?

on adding folder items to this_folder after receiving all_items
	display dialog "going!"
	try
		set f to POSIX file ((POSIX path of this_folder) & "config")
		set nref to open for access f with write permission
		write "foo" to nref
		close access nref
	on error msg
		display dialog msg with icon stop
	end try
end adding folder items to

Hi, Your code works perfectly, but not mine… :smiley:

Yes, the action works for any directory, except for those with strange unicode chars…

For example: I created a directory named
𦛚𦛨𦖭𦉪𦆲훙횬ꌸꍇ㎈
on the desktop…
choosed chars at random, from the UNICODE table on character palette.

Then, I used this code,

this is my code

on adding folder items to this_folder after receiving all_items
set delim to “´” as unicode text
try
set fconfig to (POSIX file of (POSIX path of this_directory & “.config”))
set nref to open for access file fconfig with write permission
write ((name & delim & assunto & delim & corpo & delim & destinatario & delim) as Unicode text) to file fconfig
close access nref
on error msg
display dialog msg with icon stop
end try
end adding folder items to

the error message I see is:
Can’t get «class psxf» of /Users/mike/desktop/???훙횬??㎈/.config

as you see, some of the chars were not converted correctly…
:o

Try this:

on adding folder items to this_folder after receiving all_items
	set delim to "´" as Unicode text
	try
		set fconfig to POSIX file ((POSIX path of this_folder) & ".config")
		set nref to open for access fconfig with write permission
		write ((name & delim & assunto & delim & corpo & delim & destinatario & delim) as Unicode text) to nref
		close access nref
	on error msg
		display dialog msg with icon stop
	end try
end adding folder items to

The main error was using “POSIX file of” instead of “POSIX file”.
Also, there was a little typo (you were using “this_directory” instead of “this_folder”). And it’s better if you use the stream you opened for write permission (“nref”). And “fconfig” will hold a file specification, so you don’t need stat later “open for access file fconfig”, but simply “open for access fconfig”. :wink:

→ untested, btw, but should work now

I have corrected the two errors: POSIX file of to simply POSIX file
and OPEN FOR ACCESS FILE FCONFIG to simply OPEN FOR ACCESS FCONFIG
the typo you mentioned was present just on the version I put here on the forum. It was my mistake copying parts of the code…

Unfortunately, after made the suggestions you pointed, in my view, correctly, the script stop working for all directories, not just for those with strange chars… :rolleyes:

If you can please test it on your system, just create a folder called 𦛚𦛨𦖭𦉪𦆲훙횬ꌸꍇ㎈
on your desktop and add the action to it…

Simply do not works. It’s unbelieavable!!! Everything is fine now. It must be a bug.
The script works perfectly for directories not using strange chars…
if you noticed, that error message I mentioned, that is,
Can’t get «class psxf» of /Users/mike/desktop/???훙횬??㎈/.config

shows interrogation marks on the unrecognizable chars… at least those chars not recognized by the script… strangely, from the chars I selected, just 3 were recognized… that’s strange… the more logical would be no char recognized…

very strange…

Is that any way of substituting the line
set fconfig to POSIX file ((POSIX path of this_folder) & “.config”)
for something like
set fconfig to (this_folder as unicode text) & “.config”

or

file “.config” of this_folder?

the problem with this last one I think is that the file must exist when the command is executed and that will not be the case the first time the script run…

… blu blu blub lblblb gllll glrrrr

Definitivelly, this works here:

on adding folder items to this_folder after receiving all_items
	try
		set f to POSIX file ((POSIX path of this_folder) & ".config")
		set nref to open for access f with write permission
		write "foo" to nref
		close access nref
	on error msg
		display dialog msg with icon stop
	end try
end adding folder items to

:confused:

Nope. Not working for me.
Works for almost 100% of cases, but not for all cases.
There’s a bug on this command.
The only way to make it work is to use the following syntax (I discovered after hours of pain and suffering…)


tell application "Finder" to make new file at this_directory with properties {name:"config"}
set nref to open for access file "config" of this_directory with write permission
write (("bla bla bla") as Unicode text) to nref
close access nref

this works for any chinese, indian, cherokee and even klingon char!!!
Anyway thank you JJ!!!
:smiley: