Archive for the 'NSRange' Category

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