NSCFConstantStringについて(主に値が空の場合にNSCFConstantStringとして値が来た場合)
ソース | |
---|---|
001 | #!/usr/bin/env osascript |
002 | use AppleScript version "2.8" |
003 | use framework "Foundation" |
004 | use scripting additions |
005 | property refMe : a reference to current application |
006 | |
007 | #まずは__NSCFConstantStringクラスの値を生成しいます |
008 | #値がmissing value=NULLではなく |
009 | #""空のテキストが値として入っているDICT |
010 | set ocidConstantStringDict to refMe's NSMutableDictionary's alloc()'s init() |
011 | ocidConstantStringDict's setValue:("") forKey:("SOMEKEY") |
012 | |
013 | #DICTから空の値を取り出すと |
014 | set ocidSomeValue to ocidConstantStringDict's valueForKey:("SOMEKEY") |
015 | log ocidSomeValue's className() as text |
016 | --> __NSCFConstantString |
017 | #それがConstantStringクラスになる |
018 | |
019 | #missing valueでも""空白文字両方に『マッチしない』 |
020 | if ocidSomeValue = (missing value) then |
021 | log "missing value =" |
022 | end if |
023 | if ocidSomeValue is (missing value) then |
024 | log "missing value is" |
025 | end if |
026 | if ocidSomeValue is ("") then |
027 | log " '' is" |
028 | end if |
029 | if ocidSomeValue = ("") then |
030 | log " '' =" |
031 | end if |
032 | ##ASのテキストにすれば判定可能です |
033 | #なので |
034 | #ocidSomeValueの想定される値がテキストの場合はこれでOK |
035 | if (ocidSomeValue as text) is ("") then |
036 | log " as text is" |
037 | else if (ocidSomeValue as text) is not ("") then |
038 | end if |
039 | if (ocidSomeValue as text) = ("") then |
040 | log " as text =" |
041 | else if (ocidSomeValue as text) ≠ ("") then |
042 | end if |
043 | |
044 | |
045 | #文字数としてはゼロなんです |
046 | log ocidSomeValue's |length|() |
047 | log ocidSomeValue's |length|() as integer |
048 | -->0 |
049 | |
050 | #で、空判定に文字数使えるか? |
051 | set numCntChar to ocidSomeValue's |length|() |
052 | if numCntChar = 0 then |
053 | log "NULLです" |
054 | end if |
055 | #と一見良さそうに見えますが |
056 | |
057 | #戻り値にmissing valueが入った場合 |
058 | set ocidSomeValue to (missing value) |
059 | try |
060 | # missing valueはNULLLなので文字数を数えられない |
061 | log ocidSomeValue's |length|() |
062 | -->ここでエラーになります |
063 | end try |
064 | |
065 | #NSNULLが入っていることが想定される場合は |
066 | set ocidSomeValue to (refMe's NSNull's |null|()) |
067 | if ocidSomeValue = (refMe's NSNull's |null|()) then |
068 | log "NSNullとしては判定は可能" |
069 | end if |
070 | |
071 | #テキストとして比較するには意図した判定結果になります |
072 | set ocidSomeValue to ocidConstantStringDict's valueForKey:("SOMEKEY") |
073 | log (ocidSomeValue's isEqualToString:("")) as boolean |
074 | --> true |
075 | #Classとして比較するもOK |
076 | set ocidSomeValue to ocidConstantStringDict's valueForKey:("SOMEKEY") |
077 | log (ocidSomeValue's isKindOfClass:(refMe's NSCFConstantString's class)) as boolean |
078 | -->false |
079 | log (ocidSomeValue's isKindOfClass:(refMe's __NSCFConstantString's class)) as boolean |
080 | --> true |
081 | |
082 | |
083 | |
084 | ############################ |
085 | #なので対処方法は |
086 | # = イコールで判定しない |
087 | set ocidSomeValue to ocidConstantStringDict's valueForKey:("SOMEKEY") |
088 | #CLASS名を取得して |
089 | set strClassName to ocidSomeValue's className() as text |
090 | if strClassName does not contain "ConstantString" then |
091 | log "通常の値として取得出来ています『たぶん』" |
092 | else if strClassName contains "ConstantString" then |
093 | log "たぶんNULLです" |
094 | #文字数を数えて |
095 | set numCntChar to ocidSomeValue's |length|() |
096 | #0なら |
097 | if numCntChar = 0 then |
098 | log "ConstantString の空オブジェクトです" |
099 | else if ocidSomeValue = (missing value) then |
100 | log "missing valueです" |
101 | else |
102 | log "他のテキストの値が入っていると思われます" |
103 | end if |
104 | end if |
AppleScriptで生成しました |