Archive for the 'NSMutableString' Category

2016/11/06 XMLをrecordにv2

XMLをrecordに変換するAppleScriptです。

以前にAppleScript-Users ML上で流れていたXML→record変換のAppleScriptでしたが、動作確認を行ってもうまく動かず、そのまま放置状態になっていました。

見直してみたところ、「NSMutableDictionary’s dictionary()」というカラのmutable dictionaryを作成する部分が、うまくAppleScriptの処理系に認識されていなかったようでした。少し書き直してみました。

AppleScript名:XMLをrecordにv2
–2015 Shane Stanley & Alex Zavatone
– Modified 2016-11-06 by Takaaki Naganoya
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/4306

property dictStack : missing value – stack to hold array of dictionaries
property textInProgress : “” – string to collect text as it is found
property anError : missing value – if we get an error, store it here

set xmlString to “< ?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>

Saga
Nor
én
Malm
ö
Martin
Rohde
K
øbenhavn

set xmlRes to my makeRecordWithXML:xmlString
–> {|character|:{firstName:{|contents|:”Saga”}, lastName:{|contents|:”Norén”}, city:{|contents|:”Malmö“}, partner:{firstName:{|contents|:”Martin”}, lastName:{|contents|:”Rohde”}, city:{|contents|:”København”}, attributes:{approach:”dogged”}}}}

on makeRecordWithXML:xmlString
  – set up properties
  
set my dictStack to current application’s NSMutableArray’s array() – empty mutable array
  
set anEmpty to current application’s NSMutableDictionary’s |dictionary|()
  (
my dictStack)’s addObject:anEmpty – add empty mutable dictionary
  
set my textInProgress to current application’s NSMutableString’s |string|() – empty mutable string
  
  
– convert XML from string to data
  
set anNSString to current application’s NSString’s stringWithString:xmlString
  
set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
  
– initialize an XML parser with the data
  
set theNSXMLParser to current application’s NSXMLParser’s alloc()’s initWithData:theData
  
  
– set this script to be the parser’s delegate
  
theNSXMLParser’s setDelegate:me
  
  
– tell it to parse the XML
  
set theResult to theNSXMLParser’s parse()
  
if theResult then – went OK, get first item on stack
    return ((my dictStack)’s firstObject()) as record
  else – error, so return error
    error (my anError’s localizedDescription() as text)
  end if
end makeRecordWithXML:

– this is an XML parser delegate method. Called when new element found
on parser:anNSXMLParser didStartElement:elementName namespaceURI:aString qualifiedName:qName attributes:aRecord
  – store reference to last item on the stack
  
set parentDict to my dictStack’s lastObject()
  
  
– make new child
  
set childDict to current application’s NSMutableDictionary’s |dictionary|()
  
  
– if there are attributes, add them as a record with key “attributes”
  
if aRecord’s |count|() > 0 then
    childDict’s setValue:aRecord forKey:“attributes”
  end if
  
  
– see if there’s already an item for this key
  
set existingValue to parentDict’s objectForKey:elementName
  
  
if existingValue is not missing value then
    – there is, so if it’s an array, store it…
    
if (existingValue’s isKindOfClass:(current application’s NSMutableArray)) as boolean then
      set theArray to existingValue
    else
      – otherwise create an array and add it
      
set theArray to current application’s NSMutableArray’s arrayWithObject:existingValue
      
parentDict’s setObject:theArray forKey:elementName
    end if
    
    
– then add the new dictionary to the array
    
theArray’s addObject:childDict
  else
    – add new dictionary directly to the parent
    
parentDict’s setObject:childDict forKey:elementName
  end if
  
  
– also add the new dictionary to the end of the stack
  (
my dictStack)’s addObject:childDict
end parser:didStartElement:namespaceURI:qualifiedName:attributes:

– this is an XML parser delegate method. Called at the end of an element
on parser:anNSXMLParser didEndElement:elementName namespaceURI:aString qualifiedName:qName
  – if any text has been stored, add it as a record with key “contents”
  
if my textInProgress’s |length|() > 0 then
    set dictInProgress to my dictStack’s lastObject()
    
dictInProgress’s setObject:textInProgress forKey:“contents”
    
    
– reset textInProgress property for next element
    
set my textInProgress to current application’s NSMutableString’s |string|()
  end if
  
  
– remove last item from the stack
  
my dictStack’s removeLastObject()
end parser:didEndElement:namespaceURI:qualifiedName:

– this is an XML parser delegate method. Called when string is found. May be called repeatedly
on parser:anNSXMLParser foundCharacters:aString
  – only append string if it’s not solely made of space characters (which should be, but aren’t, caught by another delegate method)
  
if (aString’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceAndNewlineCharacterSet()))’s |length|() > 0 then
    (my textInProgress)’s appendString:aString
  end if
end parser:foundCharacters:

– this is an XML parser delegate method. Called when there’s an error
on parser:anNSXMLParser parseErrorOccurred:anNSError
  set my anError to anNSError
end parser:parseErrorOccurred:

★Click Here to Open This Script 

2016/01/07 OSAScriptController+OSAScriptViewでAppleScriptを実行

Cocoaの機能を用いて、OSAScriptController+OSAScriptViewの組み合わせでAppleScriptのテキストを実行するAppleScriptです。

osascriptexe.png

AppleScriptの動的生成(テキストを組み立ててコンパイル(構文確認)してAppleScriptにして)+実行については、

 (1)AppleScriptのコマンドrun scriptで実行するもの
 (2)shellのosascriptコマンドで実行するもの
 (3)OSAScriptのinitWithSource: compileAndReturnError:メソッドで実行するもの

ぐらいが定番でしょうか。文法的に無茶なことを実現(recordの動的生成など)するために、昔から使われてきたブラックテクノロジー(セキュリティ的に穴になりそうな無茶な処理)です。

#  recordの動的生成については、OS X 10.10から通常のAppleScriptでもCocoaの機能が普通に呼び出せるようになったので、NSDictionaryを作成してrecord生成できます。正規表現なども普通に使えるようになったので、かつての「常識」が根底から変わってきています

で、なんでOSAScriptController+OSAScriptViewとかいう「自分流Script Editorを作る部品」を引っ張り出してきたかといえば、

 AppleScriptを直接PDF化したい(書式つきで)

ような場合に、OSAScriptViewからリッチテキストを取得して、RTF、HTML、PDFなどに変換するような処理ができると考えたからです。まっとうに考えれば、Script EditorをAppleScriptでコントロールして書き出すあたりになりますが、それだと並列処理できませんので。

並列処理実行できるように、このような部品を使ってみた次第です。

AppleScript名:OSAScriptViewでAppleScriptを実行
– Created 2016-01-07 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “OSAKit”
use framework “AppKit”

set srcStr to current application’s NSMutableString’s stringWithString:“display dialog \”OSAScript TEST\”"

set osaCon to current application’s OSAScriptController’s alloc()’s init()
–>  (OSAScriptController) <OSAScriptController: 0×7ff7240ae190>

set osaView to current application’s OSAScriptView’s alloc()’s init()

osaCon’s setScriptView:osaView
osaView’s setString:srcStr
–osaCon’s compileScript:(missing value)
osaCon’s runScript:(missing value)

–set aRT to osaView’s

★Click Here to Open This Script 

2015/11/26 動的にテキストビューを表示

動的にNSTextViewを作成してログ表示するAppleScriptです。

textview1.png

Script Editor上でControl+Command+Rで、ASObjC Explorer 4上では「Run in Foreground」をチェックして実行する必要があります。

AppleScript名:ASOCでテキストビューを表示
– Created 2015-11-25 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”

set aWidth to 400
set aHeight to 600
set aColor to current application’s NSColor’s colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0
set aTitle to “テキストビューのじっけん/TextView Test”
set outText to “ぴよまるソフトウェア / Piyomaru Software “

set aScroll to current application’s NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
set aView to current application’s NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))

set aStr to current application’s NSMutableString’s alloc()’s init()

aView’s setRichText:true
aView’s useAllLigatures:true
aView’s setTextColor:(current application’s NSColor’s cyanColor())

aView’s setBackgroundColor:aColor
aScroll’s setDocumentView:aView
aView’s enclosingScrollView()’s setHasVerticalScroller:true

set aWin to makeWinWithView(aScroll, aWidth, aHeight, aTitle, 0.8)
aWin’s makeKeyAndOrderFront:(missing value)
aWin’s makeFirstResponder:aView

repeat with i from 1 to 100
  set bStr to (current application’s NSMutableString’s stringWithString:(outText & (i as string) & return))
  
set aStr to (bStr’s stringByAppendingString:aStr)
  (
aView’s setString:aStr)
end repeat

my closeWin:aWin

–make Window for Input
on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV)
  set aScreen to current application’s NSScreen’s mainScreen()
  
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
  
set aBacking to current application’s NSTitledWindowMask –NSBorderlessWindowMask
  
set aDefer to current application’s NSBackingStoreBuffered
  
  
– Window
  
set aWin to current application’s NSWindow’s alloc()
  (
aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
  
aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor())
  
  
aWin’s setTitle:aTitle
  
aWin’s setDelegate:me
  
aWin’s setDisplaysWhenScreenProfileChanges:true
  
aWin’s setHasShadow:true
  
aWin’s setIgnoresMouseEvents:false
  
aWin’s setLevel:(current application’s NSNormalWindowLevel)
  
aWin’s setOpaque:false
  
aWin’s setAlphaValue:alphaV –append
  
aWin’s setReleasedWhenClosed:true
  
aWin’s |center|()
  
aWin’s makeKeyAndOrderFront:(me)
  
  
– Set Custom View
  
aWin’s setContentView:aView
  
  
return aWin
  
end makeWinWithView

–close win
on closeWin:aWindow
  delay 5
  
repeat with n from 10 to 1 by -1
    (aWindow’s setAlphaValue:n / 10)
    
delay 0.02
  end repeat
  
aWindow’s |close|()
end closeWin:

★Click Here to Open This Script 

2015/09/30 Unicodeの文字をNormalizeする

Cocoaの機能を用いて、NSStringの文字列をNormalizeするAppleScriptです。

たまたま仕事で、PDFの内容(テキスト)をparseしてデータを取り出して文字コードの範囲チェックをしていたところ・・・特定の文字のコード判定が思ったようにできず・・・調べてみたら、濁点つきのひらがな&カタカナがひっかかっていることが判明。これはNormalizeしてから処理するしかないだろーと考え、やり方を一通り調べてみました。

処理した結果がどうなっているかを調べるために、hexdumpのルーチンをつけています。

Normalizeしてからコード判定してみたら、問題なくできました。

AppleScript名:Unicodeの文字をNormalizeする
– Created 2015-09-30 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

–Reference:
–http://akisute.com/2010/05/utf-8-normalize.html
–http://nomenclator.la.coocan.jp/unicode/normalization.htm

set a to “がぎぐげご”
set aStr to current application’s NSString’s stringWithString:a
log hexDumpString(aStr)
–> {”E3″, “81″, “8C”, “E3″, “81″, “8E”, “E3″, “81″, “90″, “E3″, “81″, “92″, “E3″, “81″, “94″}

–NFD
set aNFD to aStr’s decomposedStringWithCanonicalMapping()
–>  (NSString) “がぎぐげご”
log hexDumpString(aNFD)
–> {”E3″, “81″, “8B”, “E3″, “82″, “99″, “E3″, “81″, “8D”, “E3″, “82″, “99″, “E3″, “81″, “8F”, “E3″, “82″, “99″, “E3″, “81″, “91″, “E3″, “82″, “99″, “E3″, “81″, “93″, “E3″, “82″, “99″}

–NFKD
set aNFKD to aStr’s decomposedStringWithCompatibilityMapping()
–>  (NSString) “がぎぐげご”
log hexDumpString(aNFKD)
–> {”E3″, “81″, “8B”, “E3″, “82″, “99″, “E3″, “81″, “8D”, “E3″, “82″, “99″, “E3″, “81″, “8F”, “E3″, “82″, “99″, “E3″, “81″, “91″, “E3″, “82″, “99″, “E3″, “81″, “93″, “E3″, “82″, “99″}

–NFC
set aNFC to aStr’s precomposedStringWithCanonicalMapping()
–>  (NSString) “がぎぐげご”
log hexDumpString(aNFC)
–> {”E3″, “81″, “8C”, “E3″, “81″, “8E”, “E3″, “81″, “90″, “E3″, “81″, “92″, “E3″, “81″, “94″}

–NFKC
set aNFKC to aStr’s precomposedStringWithCompatibilityMapping()
–>  (NSString) “がぎぐげご”
log hexDumpString(aNFKC)
–> {”E3″, “81″, “8C”, “E3″, “81″, “8E”, “E3″, “81″, “90″, “E3″, “81″, “92″, “E3″, “81″, “94″}

–NSStringをhexdumpする
on hexDumpString(theNSString)
  set theNSData to theNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set theString to (theNSData’s |description|()’s uppercaseString())
  
  
–Remove “< " ">” characters in head and tail
  
set tLength to (theString’s |length|()) - 2
  
set aRange to current application’s NSMakeRange(1, tLength)
  
set theString2 to theString’s substringWithRange:aRange
  
  
–Replace Space Characters
  
set aString to current application’s NSString’s stringWithString:theString2
  
set bString to aString’s stringByReplacingOccurrencesOfString:” “ withString:“”
  
  
set aResList to splitString(bString, 2)
  
–> {”E3″, “81″, “82″, “E3″, “81″, “84″, “E3″, “81″, “86″, “E3″, “81″, “88″, “E3″, “81″, “8A”}
  
  
return aResList
  
end hexDumpString

–Split NSString in specified aNum characters
on splitString(aText, aNum)
  
  
set aStr to current application’s NSString’s stringWithString:aText
  
if aStr’s |length|() aNum then return aText
  
  
set anArray to current application’s NSMutableArray’s new()
  
set mStr to current application’s NSMutableString’s stringWithString:aStr
  
  
set aRange to current application’s NSMakeRange(0, aNum)
  
  
repeat while (mStr’s |length|()) > 0
    if (mStr’s |length|()) < aNum then
      anArray’s addObject:(current application’s NSString’s stringWithString:mStr)
      
mStr’s deleteCharactersInRange:(current application’s NSMakeRange(0, mStr’s |length|()))
    else
      anArray’s addObject:(mStr’s substringWithRange:aRange)
      
mStr’s deleteCharactersInRange:aRange
    end if
  end repeat
  
  
return (current application’s NSArray’s arrayWithArray:anArray) as list
  
end splitString

★Click Here to Open This Script 

AppleScript名:Unicodeの文字を各方法でNormalizeして文字数をカウント
– Created 2015-10-02 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–Reference:
–http://akisute.com/2010/05/utf-8-normalize.html
–http://nomenclator.la.coocan.jp/unicode/normalization.htm

set aRes to normalizeStrAndRetEachLength("がぎぐげご")
–> {origLen:5, NFD:10, NFKD:10, NFC:5, NFKC:5}
set bRes to normalizeStrAndRetEachLength("ぱぴぷぺぽ")
–> {origLen:5, NFD:10, NFKD:10, NFC:5, NFKC:5}
set cRes to normalizeStrAndRetEachLength("㌀")
–> {origLen:1, NFD:1, NFKD:5, NFC:1, NFKC:4}
set dRes to normalizeStrAndRetEachLength("㍿")
–> {origLen:1, NFD:1, NFKD:4, NFC:1, NFKC:4}
set eRes to normalizeStrAndRetEachLength("Å")
–> {origLen:1, NFD:2, NFKD:2, NFC:1, NFKC:1}
set fRes to normalizeStrAndRetEachLength("…")
–> {origLen:1, NFD:1, NFKD:3, NFC:1, NFKC:3}
set gRes to normalizeStrAndRetEachLength("")
–> {origLen:1, NFD:1, NFKD:3, NFC:1, NFKC:3}
set hRes to normalizeStrAndRetEachLength("")
–> {origLen:1, NFD:1, NFKD:2, NFC:1, NFKC:2}
set iRes to normalizeStrAndRetEachLength("")
–> {origLen:1, NFD:1, NFKD:3, NFC:1, NFKC:3}

on normalizeStrAndRetEachLength(a)
  set aStr to current application’s NSString’s stringWithString:a
  
set origLenNum to aStr’s |length|()
  
set aNFD to aStr’s decomposedStringWithCanonicalMapping()’s |length|() –NFD
  
set aNFKD to aStr’s decomposedStringWithCompatibilityMapping()’s |length|() –NFKD
  
set aNFC to aStr’s precomposedStringWithCanonicalMapping()’s |length|() –NFC
  
set aNFKC to aStr’s precomposedStringWithCompatibilityMapping()’s |length|() –NFKC
  
return {origLen:origLenNum, NFD:aNFD, NFKD:aNFKD, NFC:aNFC, NFKC:aNFKC}
end normalizeStrAndRetEachLength

★Click Here to Open This Script 

2015/09/02 ASOCで文字を逆順に

Cocoaの機能を用いて、テキストを逆順に(”Apple”→”elppA”)するAppleScriptです。

Pure AppleScriptで書くと2〜4行ぐらいの内容で、Pure AppleScriptで実行した方が高速です。

追記:Shane Stanleyから「そのままだと日本語のキャラクターとかに対応できないよ」という指摘があり、Shaneによる修正版(v2)を掲載。日本語ネイティブスピーカーとして申し訳ないです(汗)。

reverse_resized.png

Cocoaの機能を使う判断基準の参考にしたかったので、uuidを生成して連結したテキストを処理させてみると、

10回連結:ASOC版→0.061秒、Pure AS版:0.063秒

と、わずかにASOC版が高速という結果が出て、さらに1万回連結した巨大なテキストを処理させてみると、ASOC版がPure AppleScript版の2〜3倍ぐらいのスピードで処理できました(文字列連結・・・つまり、テスト用データの作成部分で差が出ているような気もしないではない)。

ほかに、ひらがなとカタカナの区別をちゃんとしたいとか、「函廚函屮螢奪肇襦廚同一視されるのは嫌だといった場合にはCocoaの機能を呼び出すことになるでしょう。

AppleScript名:ASOCで文字を逆順に v2
– Created 2015-09-01 by Takaaki Naganoya
– Modified 2015-09-01 by Shane Stanley –Consider CJK Characters & Emoji
–http://stackoverflow.com/questions/6720191/reverse-nsstring-text
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set aUUID to (current application’s NSUUID’s UUID()’s UUIDString()) as text
–>  ”46EF17B7-CB3E-4DD9-BA8A-013D3B30A80A”

set aUUID to 😀😐 & aUUID & 😀😐
–>  ”😀😐46EF17B7-CB3E-4DD9-BA8A-013D3B30A80A😀😐

set revUUID to reversedStr(aUUID) as text
–>  ”😐😀A08A03B3D310-A8AB-9DD4-E3BC-7B71FE64😐😀

on reversedStr(paramStr as text)
  set aStr to current application’s NSString’s stringWithString:paramStr
  
set strLength to aStr’s |length|()
  
set revStr to current application’s NSMutableString’s stringWithCapacity:strLength
  
set charIndex to strLength - 1
  
repeat while charIndex > -1
    set subStrRange to aStr’s rangeOfComposedCharacterSequenceAtIndex:charIndex
    
revStr’s appendString:(aStr’s substringWithRange:subStrRange)
    
set charIndex to (location of subStrRange) - 1
  end repeat
  
  
return revStr
end reversedStr

★Click Here to Open This Script 

日本語文字列の入ったNSString/NSMutableStringから1文字取り出す処理の追試を行っておきました。なるほど。

AppleScript名:ASOCで文字列から一部分を取り出す
– Created 2015-09-02 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set aStr to current application’s NSString’s stringWithString:“ぴよまるソフトウェア”
–>  (NSString) “ぴよまる”
set s1 to aStr’s characterAtIndex:0
–>  «data ushr7430»–NG

set subStrRange to aStr’s rangeOfComposedCharacterSequenceAtIndex:0
–>  {​​​​​location:0, ​​​​​length:1​​​}
set bStr to aStr’s substringWithRange:subStrRange
–>  (NSString) “ぴ”–OK

★Click Here to Open This Script 

2015/01/26 テキストをhexdump(ASOC)v4

NSDataを使ってhexdumpを行うASOCのscriptの、若干の機能追加版です。

文字列の整形にCocoaの機能を使うように変更(データ量が増えた時にも安心できるように)しました。ただ、期待したよりは速くない感じです。

AppleScript名:テキストをhexdump(ASOC)v4
– Created 2015-01-26 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set aStr to “あいうえお”

–Hexdump
set theNSString to current application’s NSString’s stringWithString:aStr
set theNSData to theNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
set theString to (theNSData’s |description|()’s uppercaseString())

–Remove “< " ">” characters in head and tail
set tLength to (theString’s |length|()) - 2
set aRange to current application’s NSMakeRange(1, tLength)
set theString2 to theString’s substringWithRange:aRange

–Replace Space Characters
set aString to current application’s NSString’s stringWithString:theString2
set bString to aString’s stringByReplacingOccurrencesOfString:” “ withString:“”

set aResList to splitString(bString, 2) as list
–> {​​​​​”E3″, ​​​​​”81″, ​​​​​”82″, ​​​​​”E3″, ​​​​​”81″, ​​​​​”84″, ​​​​​”E3″, ​​​​​”81″, ​​​​​”86″, ​​​​​”E3″, ​​​​​”81″, ​​​​​”88″, ​​​​​”E3″, ​​​​​”81″, ​​​​​”8A”​​​}

–Split NSString in specified aNum characters
on splitString(aText, aNum)
  
  
set aStr to current application’s NSString’s stringWithString:aText
  
if aStr’s |length|() aNum then return aText
  
  
set anArray to current application’s NSMutableArray’s new()
  
set mStr to current application’s NSMutableString’s stringWithString:aStr
  
  
set aRange to current application’s NSMakeRange(0, aNum)
  
  
repeat while (mStr’s |length|()) > 0
    if (mStr’s |length|()) < aNum then
      anArray’s addObject:(current application’s NSString’s stringWithString:mStr)
      
mStr’s deleteCharactersInRange:(current application’s NSMakeRange(0, mStr’s |length|()))
    else
      anArray’s addObject:(mStr’s substringWithRange:aRange)
      
mStr’s deleteCharactersInRange:aRange
    end if
  end repeat
  
  
return (current application’s NSArray’s arrayWithArray:anArray)
  
end splitString

★Click Here to Open This Script 

2015/01/25 テキストをhexdump(ASOC)v2

NSDataを使ってhexdumpを行うASOCのscriptの、若干の機能追加版です。

2文字ごとにペアにして出力するようにしました。

AppleScript名:テキストをhexdump(ASOC)v2
– Created 2015-01-25 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set aStr to “あいうえお”
set theNSString to current application’s NSString’s stringWithString:aStr
set theNSData to theNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
set theText to text 2 thru -2 of (theNSData’s |description|()’s uppercaseString() as text)
set tList to words of theText
set ttext to tList as string

set aRes to my splitString:ttext everyChar:2
–> {​​​​​”E3″, ​​​​​”81″, ​​​​​”82″, ​​​​​”E3″, ​​​​​”81″, ​​​​​”84″, ​​​​​”E3″, ​​​​​”81″, ​​​​​”86″, ​​​​​”E3″, ​​​​​”81″, ​​​​​”88″, ​​​​​”E3″, ​​​​​”81″, ​​​​​”8A”​​​}

–文字列を指定文字数で分割
on splitString:aText everyChar:aNum
  
  
set aStr to current application’s NSString’s stringWithString:aText
  
if aStr’s |length|() aNum then return aText
  
  
set anArray to current application’s NSMutableArray’s new()
  
set mStr to current application’s NSMutableString’s stringWithString:aStr
  
  
set aRange to current application’s NSMakeRange(0, aNum)
  
  
repeat while (mStr’s |length|()) > 0
    if (mStr’s |length|()) < aNum then
      anArray’s addObject:(current application’s NSString’s stringWithString:mStr)
      
mStr’s deleteCharactersInRange:(current application’s NSMakeRange(0, mStr’s |length|()))
    else
      anArray’s addObject:(mStr’s substringWithRange:aRange)
      
mStr’s deleteCharactersInRange:aRange
    end if
  end repeat
  
  
return (current application’s NSArray’s arrayWithArray:anArray) as list
  
end splitString:everyChar:

★Click Here to Open This Script