Archive for the 'NSUnarchiver' Category

2015/08/12 ASOCでScript Editorの構文色分け情報を設定ファイルから取り出す v2

Cocoaの機能を用いて、AppleScriptの構文色分け(色+フォント)情報が入っているplistから情報を取り出し、RGBの各8ビットの値のリストにまとめるAppleScriptです。

NSDeviceRGBColorからRGBの各値を取り出すテストを行ったものです。

AppleScript名:ASOCでScript Editorの構文色分け情報を設定ファイルから取り出す v2
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set AppleScriptFontFile to “~/Library/Preferences/com.apple.applescript.plist”
set aRec to retDictFromPlist(AppleScriptFontFile) of me

set asAttr to AppleScriptSourceAttributes of aRec
set colList to {}
set aMAX to 255 –or 65535 (AS Color は0〜65535 あまり意味はないが、、、)

repeat with i in asAttr
  set aCol to NSColor of i
  
–>  {{(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.568627 0.156863 0.564706 1….}
  
set aColO to (current application’s NSUnarchiver’s unarchiveObjectWithData:aCol)
  
set the end of colList to retColListFromNSColor(aColO, aMAX)
end repeat
colList
–aMAX=255の場合
–> {{128, 0, 128}, {95, 94, 94}, {14, 62, 251}, {120, 52, 203}, {252, 42, 28}, {0, 0, 0}, {145, 82, 17}, {18, 18, 18}, {39, 201, 201}, {15, 62, 251}, {31, 182, 252}, {129, 58, 217}, {93, 54, 146}, {9, 55, 187}, {8, 55, 187}, {9, 55, 186}, {86, 55, 189}, {52, 32, 99}}

–aMax=65535の場合
–> {{32768, 0, 32768}, {24415, 24158, 24158}, {3598, 15934, 64507}, {30840, 13364, 52171}, {64764, 10794, 7196}, {0, 0, 0}, {37265, 21074, 4369}, {4721, 4721, 4721}, {10023, 51657, 51657}, {3855, 15934, 64507}, {7967, 46774, 64764}, {33153, 14906, 55769}, {23901, 13878, 37522}, {2313, 14135, 48059}, {2056, 14135, 48059}, {2313, 14135, 47802}, {22102, 14135, 48573}, {13364, 8224, 25443}}

–Read plist as record
on retDictFromPlist(aPath as text)
  set thePath to current application’s NSString’s stringWithString:aPath
  
set thePath to thePath’s stringByExpandingTildeInPath()
  
set theDict to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath
  
return theDict as record
end retDictFromPlist

–NSColorからrgbの8bitの値を取り出す
on retColListFromNSColor(aCol, aMAX as integer)
  set aRed to round ((aCol’s redComponent()) * aMAX) rounding as taught in school
  
set aGreen to round ((aCol’s greenComponent()) * aMAX) rounding as taught in school
  
set aBlue to round ((aCol’s blueComponent()) * aMAX) rounding as taught in school
  
return {aRed, aGreen, aBlue}
end retColListFromNSColor

★Click Here to Open This Script 

2015/07/27 ASOCでAppleScript設定ファイルの読み込み

Cocoaの機能を用いてScript Editor自身の書式設定ファイル(plist)を読み込むAppleScriptです。

とくに特殊な処理は必要なく、一般的なplistファイルに記録されたDictionaryをrecordに読み込んで、最終的にNSDataから各種データに復元しています。もうちょっとこなれた書き方もありそうですが、とりあえずデータを取り出すレベルまで書いてみた次第です。

Scriptが返している各種Cocoaのオブジェクトについては、Apple純正Script Editorでは表示されず、ASObjC Explorer 4で表示されるものを記載しています。

本当に色とフォント種別(+サイズほか関連情報)しか入っていないのにはびっくりです。

AppleScript名:ASOCでAppleScript設定ファイルの読み込み
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set AppleScriptFontFile to “~/Library/Preferences/com.apple.applescript.plist”
set pathString to current application’s NSString’s stringWithString:AppleScriptFontFile
set newPath to pathString’s stringByExpandingTildeInPath()
set aRec to retDictFromPlist(newPath) of me

set asAttr to AppleScriptSourceAttributes of aRec
set asAttrArray to {}

repeat with i in asAttr
  
  set aCol to NSColor of i
  
set aFont to NSFont of i
  
  set aColO to (current application’s NSUnarchiver’s unarchiveObjectWithData:aCol)
  
set aFonO to (current application’s NSUnarchiver’s unarchiveObjectWithData:aFont)
  
  set the end of asAttrArray to {aColO, aFonO}
  
end repeat

asAttrArray
–>  {​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.568627 0.156863 0.564706 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.372549 0.368627 0.368627 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.054902 0.243137 0.984314 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.470588 0.203922 0.796079 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.988235 0.164706 0.109804 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0 0 0 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.568627 0.321569 0.0666667 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0 0 0 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.152941 0.788235 0.788235 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.0588235 0.243137 0.984314 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.121569 0.713726 0.988235 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.505882 0.227451 0.85098 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.364706 0.211765 0.572549 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.0352941 0.215686 0.733333 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.0313726 0.215686 0.733333 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.0352941 0.215686 0.729412 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.337255 0.215686 0.741176 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}, ​​​​​{​​​​​​​(NSDeviceRGBColor) NSDeviceRGBColorSpace 0.203922 0.12549 0.388235 1, ​​​​​​​(NSFont) “Osaka 12.00 pt. P [] (0×600000249cc0) fobj=0×6000005e9800, spc=3.33″​​​​​}​​​}

–Read plist as record
on retDictFromPlist(aPath)
  set thePath to current application’s NSString’s stringWithString:aPath
  
set thePath to thePath’s stringByExpandingTildeInPath()
  
set theDict to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath
  
return theDict as record
end retDictFromPlist

★Click Here to Open This Script 

2014/12/14 AppleScriptのカラーフォーマットを読み込む

Script Editorで設定したAppleScriptの構文色分けのフォーマットを読み込むAppleScriptです。

as_editor.png
▲Piyomaru Softwareが使用しており、本Blogにも反映させている構文色分け設定。似た色でも個別に識別できるよう、わざわざRGB値を微妙に変えてある

このように、Script Editor上で指定したカラー値(R、G、B)、フォント名、フォントサイズ(point)をまとめて返します。

オリジナルはShaneがAppleScript Users MLに投稿したScript(AppleScript Libraries用)だったのですが、普通のAppleScriptで使いやすいように(若干)書き換えました。

同様の機能を持つルーチンをCocoaの機能を使わずに(数年前に)実現していたのですが、このようにまっとうな方法で調査ができるのであれば、安心感がアップします。

# 実際に既存のAppleScript(構文カラーリングを検出して、構文要素区分にもとづいた処理を行うもの)に入れ替えてみたら・・・設定値と実際のカラー値が合わないらしくて、そのままでは使えませんでした。もう少し調べてみる必要があるようで、、、

AppleScript名:AppleScriptのカラーフォーマットを読み込む
– Created 2013-11-11 by Shane Stanley
– Changed 2014-12-14 by Takaaki Naganoya
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set cList to getAppleScriptSourceColors()
–> {{redValue:145, greenValue:40, blueValue:144, fontName:”Osaka”, fontSize:12.0}, {redValue:95, greenValue:94, blueValue:94, fontName:”Osaka”, fontSize:12.0}, {redValue:14, greenValue:62, blueValue:251, fontName:”Osaka”, fontSize:12.0}, {redValue:120, greenValue:52, blueValue:203, fontName:”Osaka”, fontSize:12.0}, {redValue:252, greenValue:42, blueValue:28, fontName:”Osaka”, fontSize:12.0}, {redValue:0, greenValue:0, blueValue:0, fontName:”Osaka”, fontSize:12.0}, {redValue:145, greenValue:82, blueValue:17, fontName:”Osaka”, fontSize:12.0}, {redValue:0, greenValue:0, blueValue:0, fontName:”Osaka”, fontSize:12.0}, {redValue:39, greenValue:201, blueValue:201, fontName:”Osaka”, fontSize:12.0}, {redValue:15, greenValue:62, blueValue:251, fontName:”Osaka”, fontSize:12.0}, {redValue:31, greenValue:182, blueValue:252, fontName:”Osaka”, fontSize:12.0}, {redValue:129, greenValue:58, blueValue:217, fontName:”Osaka”, fontSize:12.0}, {redValue:93, greenValue:54, blueValue:146, fontName:”Osaka”, fontSize:12.0}, {redValue:9, greenValue:55, blueValue:187, fontName:”Osaka”, fontSize:12.0}, {redValue:8, greenValue:55, blueValue:187, fontName:”Osaka”, fontSize:12.0}, {redValue:9, greenValue:55, blueValue:186, fontName:”Osaka”, fontSize:12.0}, {redValue:86, greenValue:55, blueValue:189, fontName:”Osaka”, fontSize:12.0}, {redValue:52, greenValue:32, blueValue:99, fontName:”Osaka”, fontSize:12.0}}

–AppleScriptの構文色分けのカラー値をRGBで取得する
on getAppleScriptSourceColors()
  
  
– get the info as a dictionary
  
set thePath to current application’s NSString’s stringWithString:“~/Library/Preferences/com.apple.applescript.plist”
  
set thePath to thePath’s stringByExpandingTildeInPath()
  
set theInfo to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath
  
  
– extract relevant part and loop through
  
set theArray to (theInfo’s valueForKey:“AppleScriptSourceAttributes”) as list
  
  
set colList to {}
  
  
repeat with i from 1 to count of theArray
    set anEntry to item i of theArray
    
    
set colorData to NSColor of anEntry
    
set theColor to (current application’s NSUnarchiver’s unarchiveObjectWithData:colorData)
    
    
set rVal to round ((theColor’s redComponent()) * 255) rounding down
    
set gVal to round ((theColor’s greenComponent()) * 255) rounding down
    
set bVal to round ((theColor’s blueComponent()) * 255) rounding down
    
    
    
set fontData to NSFont of anEntry
    
set theFont to (current application’s NSUnarchiver’s unarchiveObjectWithData:fontData)
    
    
set aFontName to theFont’s displayName() as text
    
set aFontSize to theFont’s pointSize()
    
    
set aColRec to {redValue:rVal, greenValue:gVal, blueValue:bVal, fontName:aFontName, fontSize:aFontSize}
    
    
set the end of colList to aColRec
  end repeat
  
  
return colList
end getAppleScriptSourceColors

★Click Here to Open This Script