Tuesday, August 4, 2020

#1 2020-07-28 06:32:38 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 319

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.

Applescript:

-- 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.

Last edited by Fredrik71 (2020-07-28 06:35:27 am)


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.


Filed under: UTF-8, CP1252

Offline

 

#2 2020-07-28 07:18:28 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6433

Re: Encoding of broken characters in macscripter.net

Fredrik71 wrote:

And maybe Apple have function to do it with ASOC.



Applescript:

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:


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-07-28 08:08:36 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 319

Re: Encoding of broken characters in macscripter.net

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.


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.

Offline

 

#4 2020-07-28 08:56:43 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 319

Re: Encoding of broken characters in macscripter.net

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...

Applescript:

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:

Last edited by Fredrik71 (2020-07-28 08:58:22 am)


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.

Offline

 

#5 2020-07-28 01:50:38 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5256

Re: Encoding of broken characters in macscripter.net

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


NG

Offline

 

#6 2020-07-29 02:36:40 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 319

Re: Encoding of broken characters in macscripter.net

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?p … 76#p138776

Selected text on the post above.

Run the Script below.

Applescript:

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:


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)