AppleScript NSMutableDictionary

逆順Dictionary・レコードを作る


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

###元になるレコード
set recordCenter to {|札幌管区気象台|:"010100", |気象庁|:"010300", |新潟地方気象台|:"010500", |広島地方気象台|:"010700", |名古屋地方気象台|:"010400", |大阪管区気象台|:"010600", |福岡管区気象台|:"010900", |高松地方気象台|:"010800", |仙台管区気象台|:"010200", |鹿児島地方気象台|:"011000", |沖縄気象台|:"011100"} as record

###正順のレコードから可変DICTを作る
set ocidCenterDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidCenterDict's setDictionary:(recordCenter)
###↑のDICTのキーリストを取得
set ocidAllKeyArray to ocidCenterDict's allKeys()
###格納用の可変DICTを初期化
set ocidReverseCenterDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###キーの数だけ繰り返し
repeat with itemAllKeys in ocidAllKeyArray
  set strAllKeys to itemAllKeys as text
  ##キーから値を取り出して
  set ocidCenterNo to (ocidCenterDict's valueForKey:(strAllKeys))
  ##キーと値を逆にセットした逆レコードを作って
  set ocidItemDict to (refMe's NSDictionary's dictionaryWithObject:(strAllKeys) forKey:(ocidCenterNo))
  ###逆順DICTにADD追加していく
(ocidReverseCenterDict's addEntriesFromDictionary:(ocidItemDict))
end repeat


log ocidCenterDict as record
log ocidReverseCenterDict as record

|

[setDictionary]レコードをDictionaryにしてキーallKeysをリストに(キー名称日本語)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.6"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()
set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace()

###アイコンファイル名のレコード
set recordIcon to {|アクア|:"aqua.icns", |ブルー|:"blue.icns", |ダークブルー|:"darkblue.icns", |デフォルト|:"generic.icns", |グレー|:"gray.icns", |グリーン|:"green.icns", |オレンジ|:"orange.icns", |ピンク|:"pink.icns", |レッド|:"red.icns", |グラデーション|:"syft.icns", |バイオレット|:"violet.icns", |ホワイト|:"white.icns", |イエロー|:"yellow.icns"} as record
##可変レコード
set ocidIconDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
##レコードセット
ocidIconDict's setDictionary:(recordIcon)
##キーリスト
set ocidAllKeys to ocidIconDict's allKeys()
##ダイアログ用にリストに
set listAllKeys to ocidAllKeys as list
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
###ダイアログ
try
  set listResponse to (choose from list listAllKeys with title "選んでください" with prompt "アイコンを選んでください" default items (item 8 of listAllKeys) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed)
on error
  log "エラーしました"
return "エラーしました"
end try
if listResponse is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse) as text
###アイコンのファイル名
set ocidIconFileName to (ocidIconDict's valueForKey:(strResponse))
return ocidIconFileName as text



|

[NSMutableDictionary]基本

#!/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
property refNSMutableDictionary : a reference to refMe's NSMutableDictionary

######ログ表示
doLogView()

####初期化 空
set ocidNewMutableDictionary to refNSMutableDictionary's alloc()'s initWithCapacity:0
set ocidNewMutableDictionary to refNSMutableDictionary's alloc()'s init()
log ocidNewMutableDictionary as record
log className() of ocidNewMutableDictionary as text
(**)
(*__NSDictionaryM*)


####setObject
ocidNewMutableDictionary's setObject:"日本語" forKey:"Key01"
ocidNewMutableDictionary's setObject:"英語" forKey:"Key02"
ocidNewMutableDictionary's setObject:"" forKey:"Key03"
log ocidNewMutableDictionary as record
log className() of ocidNewMutableDictionary as text
(*Key01:日本語, Key02:英語, Key03:*)
(*__NSDictionaryM*)

####setValue
ocidNewMutableDictionary's setValue:"仏語" forKey:"Key03"
log ocidNewMutableDictionary as record
log className() of ocidNewMutableDictionary as text
(*Key01:日本語, Key02:英語, Key03:仏語*)
(*__NSDictionaryM*)

####addEntries
set recordAisa to {Key04:"中国語", Key05:"台湾語"} as record

ocidNewMutableDictionary's addEntriesFromDictionary:recordAisa
log ocidNewMutableDictionary as record
log className() of ocidNewMutableDictionary as text
(*Key02:英語, key04:中国語, Key01:日本語, Key05:台湾語, Key03:仏語*)
(*__NSDictionaryM*)

####addEntries
set recordLang to {Key01:"JP", Key02:"EN", Key03:"FR", Key04:"CH", Key05:"TW"}

ocidNewMutableDictionary's setDictionary:recordLang
log ocidNewMutableDictionary as record
log className() of ocidNewMutableDictionary as text
(*Key02:EN, Key05:TW, Key01:JP, Key04:CH, Key03:FR*)
(*__NSDictionaryM*)

####removeObject
ocidNewMutableDictionary's removeObjectForKey:"Key05"
log ocidNewMutableDictionary as record
log className() of ocidNewMutableDictionary as text
(*Key02:EN, Key01:JP, Key04:CH, Key03:FR*)
(*__NSDictionaryM*)

####removeObjects
set listLangKey to {"Key04", "Key03"} as list

ocidNewMutableDictionary's removeObjectsForKeys:listLangKey
log ocidNewMutableDictionary as record
log className() of ocidNewMutableDictionary as text
(*Key02:EN, Key01:JP*)
(*__NSDictionaryM*)

####removeAllObjects
ocidNewMutableDictionary's removeAllObjects()
log ocidNewMutableDictionary as record
log className() of ocidNewMutableDictionary as text


###解放
ocidNewMutableDictionary's autorelease()
ocidNewMutableDictionary's release()

#########################ログ表示
to doLogView()

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
tell application "Script Editor"
tell application "System Events"
tell process "Script Editor"
tell window 1
tell splitter group 1
tell splitter group 1
tell group 1
tell checkbox "返された値"
set boolValue to value as boolean
end tell
if boolValue is false then
click checkbox "返された値"
end if
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end if
end repeat

end doLogView
#########################

|

[NSMutableDictionary]可変レコード 値を入れる

#!/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 objNSMutableDictionary : a reference to objMe's NSMutableDictionary

set ocidNSDictionaryM to objNSMutableDictionary's dictionary()
set AAA to "美しい日本語" as text
set BBB to 123.456 as number
set CCC to 30 as integer
set DDD to {1, 2, 3}

tell ocidNSDictionaryM
its setObject:AAA forKey:"日本語"
its setObject:BBB forKey:"数値"
its setObject:CCC forKey:"整数"
its setObject:DDD forKey:"リスト"
end tell

log ocidNSDictionaryM as record
-->(*数値:123.456, 整数:30, リスト:1, 2, 3, 日本語:美しい日本語*)

set XXX to |数値| of ocidNSDictionaryM
log XXX as text
-->(*123.456*)

|

[NSMutableDictionary]可変レコード

#!/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 recordList to {Key1:"Value1", Key2:"Value2"} as record

#####可変レコード初期化
set ocidMustDict to objNSMutableDictionary's alloc()'s init()

#####可変レコード=NSMutableDictionaryに変換
set ocidMustDict to objNSMutableDictionary's dictionaryWithDictionary:recordList
###ログ
log className() of ocidMustDict as text
--> __NSDictionaryM
log ocidMustDict as record

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

###値を設定
set Key1 of ocidMustDict to "AAAAA"
###ログ
log className() of ocidMustDict as text
--> __NSDictionaryM
log ocidMustDict as record

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom