Encoding of broken characters in macscripter.net

Hi all…

After communication to Nigel about the encoding of specific characters on macscripter.net.
I was doing some research on the topic.

And macscripter.net is not alone of strange character or broken encoding in UTF-8.

Before FluxBB or php use CP1252 or MS-ANSI WINDOWS-1252 to decode character.
When FluxBB change to UTF-8 the conversion of some character got broken.

But…

Apple include command-line tool iconv to be able to encode characters from one format to other.

-- Nigels current application reference character
set inputString to "⌘"

set theCommand to "iconv -f UTF-8 -t CP1252"
set theResult to do shell script "echo " & quoted form of inputString & "| " & theCommand

And maybe Apple have function to do it with ASOC. But the above AS Script will give you some
clue if you need to covert ‘some’ characters in AS from macscripter.net to UTF-8.

Regards.

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

my convertFromCP1252:"⌘"

on convertFromCP1252:aString
	set aString to current application's NSString's stringWithString:aString
	set theData to aString's dataUsingEncoding:12 -- NSWindowsCP1252StringEncoding
	return (current application's NSString's alloc()'s initWithData:theData encoding:4) as text -- NSUTF8StringEncoding
end convertFromCP1252:

Wonderful Shane…

A good start for a ASOC Script that take input of file… or take the text from Script Editor that has broken characters and convert it to correct ones. Many scripts on macscripter.net before 2010 but also before 2016 has them.

Good work Shane… thanks.

I made a fast Script that take the front document of Script Editor and encode the input
and make a new document. And I’m sure it could me adapted to work also for Script Debugger.

The Script use Script Menu…

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

on run
	tell application "Script Editor"
		tell front document to set theText to its contents
	end tell
	
	set convText to my convertFromCP1252:theText
	
	tell application "Script Editor"
		make new document with properties {text:convText}
	end tell
end run

on convertFromCP1252:aString
	set aString to current application's NSString's stringWithString:aString
	set theData to aString's dataUsingEncoding:12 -- NSWindowsCP1252StringEncoding
	return (current application's NSString's alloc()'s initWithData:theData encoding:4) as text -- NSUTF8StringEncoding
end convertFromCP1252:

Thanks for the scripts, Fredrik71 and Shane. I’ve made this topic “sticky” so that it always appears near the top of the listing.

Here is a Script that do the same thing but here we could select any text from Safari
to be converted to right characters.

Test site
https://macscripter.net/viewtopic.php?pid=138776#p138776

Selected text on the post above.

Run the Script below.

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

on run
	tell application "Safari"
		activate
		set theText to (do JavaScript "getSelection().toString()" in document 1)
	end tell
	
	set convText to my convertFromCP1252:theText
	
	tell application "TextEdit"
		make new document with properties {text:convText}
	end tell
end run

on convertFromCP1252:aString
	set aString to current application's NSString's stringWithString:aString
	set theData to aString's dataUsingEncoding:12 -- NSWindowsCP1252StringEncoding
	return (current application's NSString's alloc()'s initWithData:theData encoding:4) as text -- NSUTF8StringEncoding
end convertFromCP1252: