How to read special characters (letters) with AppleScript? (Like this.)
I’ve got this error message when i replace char ž (letter z with caron) with z:
how can AppleScript read latin letter z, c and s with caron?
How to read special characters (letters) with AppleScript? (Like this.)
I’ve got this error message when i replace char ž (letter z with caron) with z:
how can AppleScript read latin letter z, c and s with caron?
You have to use a format like “Unicode text” or “«class utf8»” for non US-ASCII characters.
Depends on the encoding of the file you want to read.
The more detailes you give us - the better we can help!
Some code would be appreciated!
how can i use unicode in AppleScript?
i would like to use this script (http://bbs.applescript.net/viewtopic.php?id=13509) for changing filenames and foldernames from latin letter c, z and s with caron to c, z and s.
In the script you find these lines: (search for them)
“set source_folder to source_folder as string”
“set the new_item_name to the text_item_list as string”
remove them with:
“set source_folder to source_folder as Unicode text”
“set the new_item_name to the text_item_list as Unicode text”
or if you are working in Tiger:
just remove the “as string” statement ->“Unicode text” is the standard text format since Tiger!
Note: Script not tested!
hm when I change filename from z to latin letter z with caron is ok… but i don’t need that… I would like to change filename from latin letter z with caron to z… and that doesn’t work.
This is a problem:
display dialog "ž"
when I run this script … then AppleScript display me char à and not a latin letter z with carot.
The letter “ž” changed to “Ô in your editor window too, didn’t it?
That’s because the Editor only supports code in US-ASCII.
Type “ž” in the dialog of theis script and the next dialog will display it properly:
set thechar to (text returned of (display dialog "" default answer "")) as Unicode text
display dialog thechar
Applescript can handle non-US-ASCII character as “ž”!
But you have to feed the script with your special characters by either:
set thechar to (text returned of (display dialog "" default answer "")) as Unicode text
or
by reading the chars out of a file
typing them in Script Editor doesn’t work!
This might get close to what you want, sebastijan. It works recursively, so that the names of files and/or folders are also changed in nested folders:
property convType : {}
property folderNames : false
property fileNames : false
property plainList : "CSZcsz"'s characters
property listCount : count plainList
property caronList : (do shell script "perl -e 'print \"\\x{010C}\\x{0160}\\x{017D}\\x{010D}\\x{0161}\\x{017E}\"'")'s characters
to convertName(n)
repeat with i from 1 to listCount
set text item delimiters to my plainList's item i
set n to n's text items
set text item delimiters to my caronList's item i
tell n to set n to beginning & ({""} & rest)
end repeat
n
end convertName
to convertFolder(f)
tell application "Finder"
if folderNames then set folder f's name to my convertName(folder f's name)
if fileNames then repeat with i in (get f's files)
set x to i's name extension
set i's name to my convertName(text 1 thru -((count x) + 1) of (get i's name)) & x
end repeat
repeat with i in (get f's folders)
my convertFolder(i as alias)
end repeat
end tell
end convertFolder
set f to choose folder with prompt "Choose a folder for name conversion:"
tell (choose from list {"Files only", "Folders only", "Files and Folders"} with prompt "Choose type of name conversion:" default items convType)
if it is false then error number -128
set convType to item 1
end tell
considering case
set folderNames to "Folders" is in convType
set fileNames to "Files" is in convType
convertFolder(f)
end considering
I should have read your message more carefully. I’ve tried the conversion from a character with caron to one without - and you’re right. It doesn’t seem to work properly at all. Looks a bit buggy to me…
If I find a fix or a workaround, I’ll get back to you.
Apologies for the delay. I’m now pretty sure the behaviour described above is down to a bug.
I’ll file a bug report with Apple - but in the meantime, the following workaround should clean up file and/or folder names (by stripping any caron characters from them):
property modType : {}
property caronChar : ASCII character 255
to cleanName(n)
set text item delimiters to caronChar
set n to n's text items
set text item delimiters to {""}
tell n to beginning & ({""} & rest)
end cleanName
to convertItems from f with modFldr and modFile
local modFldr, modFile
tell application "Finder"
if modFldr then
set n to folder f's name as string
if caronChar is in n then set folder f's name to my cleanName(n)
end if
if modFile then repeat with i in (get f's files)
set n to i's name as string
if caronChar is in n then set i's name to my cleanName(n)
end repeat
repeat with i in (get f's folders)
convertItems of me from i as alias given modFldr:modFldr, modFile:modFile
end repeat
end tell
end convertItems
set f to choose folder with prompt "Choose a folder for name conversion:"
tell (choose from list {"Files only", "Folders only", "Files and Folders"} with prompt ¬
"Choose type of name conversion:" default items modType)
if it is false then error number -128
set modType to item 1
end tell
considering case
convertItems from f given modFldr:"Folders" is in modType, modFile:"Files" is in modType
end considering