How to decode strings with backslash escaped unicode characters?

Some shell commands return unicode characters as a string like “a\u0308” for “a¨” which is “ä” in file names. How to transform this escaped strings to real unicode text in AppleScriptObjC?

Can you give an example? They should generally be converted to Unicode by do shell script.

Here’s an example

tell application "Finder"
	set theFolder to make folder in (path to desktop) with properties {name:"Ümläut"}
	do shell script "defaults write local.prefs.test someString " & (name of theFolder)
	delete theFolder
   	get do shell script "defaults read local.prefs.test someString"
end tell

I didn’t realise defaults was an offender. You can apply a Unicode transform, like this:

use AppleScript version "2.5" -- 10.11 or later
use framework "Foundation"
use scripting additions

set someString to do shell script "defaults read local.prefs.test someString"
set someString to ((current application's NSString's stringWithString:someString)'s stringByApplyingTransform:"Hex-Any" |reverse|:false) as text

(Although in the case of defaults it would be quicker and neater to just use NSUserDefaults instead.)

1 Like

Thanks alot, that’s a cool one-liner. :wink:

Do you remember where you first came across "Hex-Any" ? Looking in the Apple developer documentation where it lists NSStringTransform constants, I couldn’t see the equivalent constant listed. Do you have another source for things that Apple don’t document, or am I just looking on the wrong page ?

Apple’s docs say:

The constants defined by the NSStringTransform type offer a subset of the functionality provided by the underlying ICU transform functionality. To apply an ICU transform defined in the ICU User Guide that doesn’t have a corresponding NSStringTransform constant, create an instance of NSMutableString and call the applyTransform:reverse:range:updatedRange: method instead.

This is incorrect. NSString.h says:

You can pass one of the predefined transforms below (NSStringTransformLatinToKatakana, etc), or any valid ICU transform ID as defined in the ICU User Guide.

You can find the ICU guide describing the defined transforms here:

http://userguide.icu-project.org/transforms/general

1 Like