NSDictionary

NSCFConstantStringについて(主に値が空の場合にNSCFConstantStringとして値が来た場合)


【スクリプトエディタで開く】 |

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

| | コメント (0)

[NSDictionary] Plistファイルの読み込み(initWithContentsOfURL)


あくまでも参考にしてください

【スクリプトエディタで開く】 |

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007##自分環境がos12なので2.8にしているだけです
008use AppleScript version "2.8"
009use framework "Foundation"
010use scripting additions
011property refMe : a reference to current application
012
013
014#パス
015set appFileManager to refMe's NSFileManager's defaultManager()
016set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSSystemDomainMask))
017set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
018set ocidFilePathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Frameworks/CoreSpotlight.framework/Versions/A/Resources/schema.loctable") isDirectory:(false)
019#読み込み
020set listResponse to refMe's NSDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL) |error| :(reference)
021if (item 2 of listResponse) = (missing value) then
022  log "initWithContentsOfURL 正常処理"
023  set ocidPlistDict to (item 1 of listResponse)
024else if (item 2 of listResponse) ≠ (missing value) then
025  log (item 2 of listResponse)'s code() as text
026  log (item 2 of listResponse)'s localizedDescription() as text
027  return "initWithContentsOfURL エラーしました"
028end if
029#
030
031
032
033
AppleScriptで生成しました

|

[stringWithFormat]NSDictionaryをテキストに

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

###レコード
set recordDict to {cont:"Japan", pref:"kanagawa", city:"yokohama", divisoon:"Cyuuou"} as record
###ディクショナリ
set ocidDictionary to refMe's NSDictionary's alloc()'s initWithDictionary:recordDict

set ocidRecordStr to refMe's NSString's stringWithFormat_("%@", ocidDictionary)

log ocidRecordStr as text

|

[initWithData]NSDictionaryをテキストに

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

###レコード
set recordDict to {cont:"Japan", pref:"kanagawa", city:"yokohama", divisoon:"Cyuuou"} as record
###ディクショナリ
set ocidDictionary to refMe's NSDictionary's alloc()'s initWithDictionary:recordDict


set ocidAllKeys to ocidDictionary's allKeys()

set strOutPut to "" as text

repeat with itemKeys in ocidAllKeys
    set ocidValue to (ocidDictionary's valueForKey:itemKeys)
    set strOutPut to strOutPut & (itemKeys as text) & ":" & (ocidValue as text) & "\n"
end repeat

log strOutPut as text

|

[initWithData]NSDictionaryをテキストに

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

###レコード
set recordDict to {cont:"Japan", pref:"kanagawa", city:"yokohama", divisoon:"Cyuuou"} as record
###ディクショナリ
set ocidDictionary to refMe's NSDictionary's alloc()'s initWithDictionary:recordDict
###JSONに格納
set ocidJson to refMe's NSJSONSerialization's dataWithJSONObject:ocidDictionary options:0 |error|:(reference)
###テキスト化
set ocidJsonStr to refMe's NSString's alloc()'s initWithData:(item 1 of ocidJson) encoding:(refMe's NSUTF8StringEncoding)
###出力
log ocidJsonStr as text

|

[valueForKey]レコードの値を取得

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSDictionary : a reference to objMe's NSDictionary
property objNSMutableDictionary : a reference to objMe's NSMutableDictionary

####:NSDictionary
####初期化
set ocidDictionary to objNSDictionary's dictionary()
####レーコード
set recordKeyValue to {KeyLangJP:"日本語", KeyLangEN:"英語"} as record
####レコードをNSDictionary
set ocidDictionary to objNSDictionary's dictionaryWithDictionary:recordKeyValue
####valueForKey
set ocidValue to ocidDictionary's valueForKey:"KeyLangJP"
log className() of ocidValue as text
log ocidValue as text



####:NSMutableDictionary
####初期化
set ocidNSMutableDictionary to objNSMutableDictionary's dictionary()
####レーコード
set recordKeyValue to {KeyLangJP:"日本語", KeyLangEN:"英語"} as record
####レコードをNSMutableDictionary
set ocidNSMutableDictionary to objNSMutableDictionary's dictionaryWithDictionary:recordKeyValue
####valueForKey
set ocidValue to ocidNSMutableDictionary's valueForKey:"KeyLangEN"
log className() of ocidValue as text
log ocidValue as text

return

|

[NSDictionary] NSDictionaryの初期化


【スクリプトエディタで開く】 |

[NSDictionary] NSDictionaryの初期化 .scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004フローズン(変更できない)Dictionary
005可変のMutableDictionaryがある
006
007com.cocolog-nifty.quicktimer.icefloe *)
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009use AppleScript version "2.8"
010use framework "Foundation"
011use scripting additions
012
013
014#NSDictionary初期化
015set ocidDictionary to current application's NSDictionary's alloc()'s init()
016log ocidDictionary's className() as text
017--> __NSDictionary0
018
019#可変に変更するにはCOPYする
020set ocidDictionaryM to ocidDictionary's mutableCopy()
021log ocidDictionaryM's className() as text
022--> __NSDictionaryM
023
024
025#レコード
026set recordSample to {|Key1|:"Value1", |Key2|:"Value2"} as record
027
028
029#レコードからDICT形式に
030
031# initWithDictionary
032set ocidDictionary to current application's NSDictionary's alloc()'s initWithDictionary:(recordSample)
033
034#dictionaryWithDictionary
035set ocidDictionary to current application's NSDictionary's dictionaryWithDictionary:(recordSample)
036
037
038#同じ方法で可変DICTにも
039
040# initWithDictionary
041set ocidDictionary to current application's NSMutableDictionary's alloc()'s initWithDictionary:(recordSample)
042
043#dictionaryWithDictionary
044set ocidDictionary to current application's NSMutableDictionary's dictionaryWithDictionary:(recordSample)
045
046
047#リストから
048set listValueList to {"Value1", "Value2"} as list
049set listKeyList to {"Key1", "Key2"} as list
050
051#キーと値のペアになっているリストから
052set ocidDictionary to current application's NSDictionary's dictionaryWithObjects:(listValueList) forKeys:(listKeyList)
053log ocidDictionary's className() as text
054--> __NSDictionaryI
055log ocidDictionary as record
056
057#同じ方法で可変DICTにも
058set ocidDictionary to current application's NSMutableDictionary's dictionaryWithObjects:(listValueList) forKeys:(listKeyList)
059log ocidDictionary's className() as text
060--> __NSDictionaryM
061log ocidDictionary as record
062-->{Key2:"Value2", Key1:"Value1"}
063
064set ocidValue to ocidDictionary's valueForKey:("Key1")
065log ocidValue as text
066--> Value1
067
068
069#DICT形式のPLISTで初期化する
070
071#ファイルパス
072set strFilePath to ("/System/Library/CoreServices/SystemVersion.plist") as text
073set ocidFilePathStr to current application's NSString's stringWithString:(strFilePath)
074set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
075set ocidFilePathURL to current application's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
076
077set listResponse to current application's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL) |error|:(reference)
078-->{NSMutableDictionary,NSError}
079set ocidDictionary to (item 1 of listResponse)
080if (item 2 of listResponse) ≠ (missing value) then
081   set strErrorNO to (item 2 of listResponse)'s code() as text
082   set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
083   current application's NSLog("■" & strErrorNO & strErrorMes)
084   return "エラーしました" & strErrorNO & strErrorMes
085end if
086
087set ocidAllKeys to ocidDictionary's allKeys()
088repeat with itemKey in ocidAllKeys
089   log (ocidDictionary's valueForKey:(itemKey)) as text
090end repeat
091
092
AppleScriptで生成しました

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL
property objNSLog : a reference to objMe's NSLog
property objNSMutableArray : a reference to objMe's NSMutableArray
property objNSMutableDictionary : a reference to objMe's NSMutableDictionary
property objNSDictionary : a reference to objMe's NSDictionary
property objNSArray : a reference to objMe's NSArray


#####通常レコード
set listValueList to {"Value1", "Value2"} as list

set listKeyList to {"Key1", "Key2"} as list


#####NSDictionary初期化
set ocidDictionary to objNSDictionary's alloc()'s init()

#####レコード=不変NSDictionary
set ocidDictionary to objNSDictionary's dictionaryWithObjects:listValueList forKeys:listKeyList

###ログ
log className() of ocidDictionary as text
--> __NSDictionaryM
log ocidDictionary as record

###値を取得
set ocidValue to Key1 of ocidDictionary
###ログ
log className() of ocidValue as text
--> NSTaggedPointerString
log ocidValue as text

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom