NSMutableDictionary

逆順Dictionary・レコードを作る


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
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
007use AppleScript version "2.8"
008use framework "Foundation"
009use scripting additions
010
011property refMe : a reference to current application
012
013###元になるレコード
014set recordCenter to {|札幌管区気象台|:"010100", |気象庁|:"010300", |新潟地方気象台|:"010500", |広島地方気象台|:"010700", |名古屋地方気象台|:"010400", |大阪管区気象台|:"010600", |福岡管区気象台|:"010900", |高松地方気象台|:"010800", |仙台管区気象台|:"010200", |鹿児島地方気象台|:"011000", |沖縄気象台|:"011100"} as record
015
016###正順のレコードから可変DICTを作る
017set ocidCenterDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
018ocidCenterDict's setDictionary:(recordCenter)
019###↑のDICTのキーリストを取得
020set ocidAllKeyArray to ocidCenterDict's allKeys()
021###格納用の可変DICTを初期化
022set ocidReverseCenterDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
023###キーの数だけ繰り返し
024repeat with itemAllKeys in ocidAllKeyArray
025  ##キーから値を取り出して
026  set ocidCenterNo to (ocidCenterDict's valueForKey:(itemAllKeys))
027  ###逆順DICTにADD追加していく
028  (ocidReverseCenterDict's setValue:(itemAllKeys) forKey:(ocidCenterNo))
029end repeat
030
031log "元のレコード"
032log ocidCenterDict as record
033log "逆順にしたレコード"
034log ocidReverseCenterDict as record
AppleScriptで生成しました


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

#!/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

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC 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 webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom