-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.applescript
2754 lines (1813 loc) · 63.8 KB
/
Dictionary.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
A native AppleScript implementation of a mutable key/value storage. The resulting data can be written to disk as a raw text `.applescript` file and both opened with a text editor and loaded when run within Script Editor or using osascript.
Values of type `integer`, `real`, and `date` are converted to text during serialization and the process is reversed when reading the file back. This should work regardless of any differences in the sending or receiving system’s date and number formatting settings.
Example:
```
set dict to newDictionary()
dict's addValueForKey("some_string", "Hello World")
dict's addValueForKey("some_date", date "Thursday, 1955-02-24 at 9:41:00 AM")
dict's addValueForKeyPathRecursively("constants/math/pi", 3.14159265359)
dict's addValueForKeyPath("constants/math/phi", 1.61803398875)
dict's addValueForKeyPath("constants/math/one", 1)
dict's addValueForKeyPath("constants/math/infinity", "∞")
dict's addValueForKey("some_list", {"A", 1, 2.0, true, "E"})
dict's addValueForKey("delete_me", "This will not be in the dictionary")
dict's removeValueForKey("delete_me")
dict's writeToFile("~/Desktop/DictionaryTest.applescript")
```
The output saved to file after running the script above looks like this:
```
{dict: ¬
{ ¬
{_:"some_string", s:"Hello World"}, ¬
{_:"some_date", t:"1955-02-24 09:41:00"}, ¬
{_:"constants", d: ¬
{dict: ¬
{ ¬
{_:"math", d: ¬
{dict: ¬
{ ¬
{_:"pi", f:"3.14159265359"}, ¬
{_:"phi", f:"1.61803398875"}, ¬
{_:"one", i:"1"}, ¬
{_:"infinity", s:"∞"} ¬
}, v:1 ¬
} ¬
} ¬
}, v:1 ¬
} ¬
}, ¬
{_:"some_list", a: ¬
{ ¬
{s:"A"}, ¬
{i:"1"}, ¬
{f:"2.0"}, ¬
{b:true}, ¬
{s:"E"} ¬
} ¬
} ¬
}, v:1 ¬
}
```
Special care was placed upon the human readability of the produced output. Once compiled in Script Editor the white space might minimally change. Notice that each key/value pair is a native AppleScript record. The following property names are used:
- `_` Key (underscore)
- `d` dictionary
- `b` boolean
- `i` integer
- `f` real (float)
- `s` text (string)
- `t` date (time)
- `a` list (array)
- `r` record
- `v` unknown value
*)
property kScriptPath : false -- Used by other scripts when loading this script; set it to the script's actual path when doing so
property kTestFilePath : "~/Desktop/DictionaryTest.applescript"
on ________________________________Script_Functions()
end ________________________________Script_Functions
on run
(*
Runs all tests.
*)
return shortTest()
speedTest()
longTest()
end run
on shortTest()
(*
Performs a short test.
*)
try
log "Running short test"
set testDate to current date
set day of testDate to 20
set month of testDate to 6
set year of testDate to 1985
set time of testDate to 9 * 60 * 60
set dict to newDictionary()
dict's addValueForKey("test", "Hello World")
dict's addValueForKey("birthday", testDate)
dict's addValueForKeyPathRecursively("constants/math/pi", 3.14159265359)
dict's addValueForKeyPath("constants/math/phi", 1.61803398875)
dict's addValueForKeyPath("constants/math/delete_me", 888)
dict's removeValueForKeyPath("constants/math/delete_me")
if dict's existsKeyPath("constants/math/delete_me") then
error "Key still exists"
end if
dict's writeToFile(kTestFilePath)
set textRep1 to dict's textRepresentation()
log textRep1
set dictAsText to "{dict: ¬
{ ¬
{_:\"test\", s:\"Hello World\"}, ¬
{_:\"birthday\", t:\"1985-06-20 09:00:00\"}, ¬
{_:\"constants\", d: ¬
{dict: ¬
{ ¬
{_:\"math\", d: ¬
{dict: ¬
{ ¬
{_:\"pi\", f:\"3.14159265359\"}, ¬
{_:\"phi\", f:\"1.61803398875\"} ¬
}, v:1 ¬
} ¬
} ¬
}, v:1 ¬
} ¬
} ¬
}, v:1 ¬
}"
set dictAsRecord to {dict:¬
{¬
{_:"test", s:"Hello World"}, ¬
{_:"birthday", t:"1985-06-20 09:00:00"}, ¬
{_:"constants", d:¬
{dict:¬
{¬
{_:"math", d:¬
{dict:¬
{¬
{_:"pi", f:"3.14159265359"}, ¬
{_:"phi", f:"1.61803398875"} ¬
}, v:1 ¬
} ¬
} ¬
}, v:1 ¬
} ¬
} ¬
}, v:1 ¬
}
set fromRecordDict to newDictionaryFromRecordRepresentation(dictAsRecord)
set textRep2 to fromRecordDict's textRepresentation()
log textRep2
set fromTextDict to newDictionaryFromTextRepresentation(dictAsText)
set textRep3 to fromTextDict's textRepresentation()
log textRep3
if textRep1 is not textRep2 then
error "Text representation differs (1 and 2)"
end if
if textRep1 is not textRep3 then
error "Text representation differs (1 and 3)"
end if
log "Short test ran successfully"
on error eMsg number eNum
set eMsg to "Short test failed: " & eMsg
log eMsg & " (" & (eNum as text) & ")"
end try
end shortTest
on speedTest()
(*
Performs a speed test creating 1000 dictionary items and modifying them.
*)
try
log "Running performance test"
set startDate to current date
set keyCount to 1000
log "Running stress test setting and changing " & keyCount & " key(s)"
set dict to newDictionary()
repeat with i from 1 to keyCount
dict's addValueForKey("key_" & (i as text), "Hello World")
end repeat
repeat with i from 1 to keyCount
dict's setValueForKey("key_" & (i as text), i as real)
end repeat
dict's writeToFile(kTestFilePath)
set finishedMessage to "Finished performance test after " & (((current date) - startDate) as text) & " second(s)"
log finishedMessage
return finishedMessage
on error eMsg number eNum
set eMsg to "Performance test failed: " & eMsg
log eMsg & " (" & (eNum as text) & ")"
end try
end speedTest
on longTest()
(*
Performs a long test.
*)
try
log "Running long test"
log "***********************************************************************"
log " BUILDING DICTIONARY "
log "***********************************************************************"
set dict to newDictionary()
set dict2 to newDictionary()
dict2's addValueForKey("first_key", 0)
repeat with i from 1 to 10
dict2's addValueForKey("key_" & ((i + 2) as text), i)
end repeat
set dict3 to newDictionary()
dict3's addValueForKey("dict3_first_key", 0)
repeat with i from 1 to 3
dict3's addValueForKey("dict3_key_" & ((i + 2) as text), i)
end repeat
set dict4 to newDictionary()
dict4's addValueForKey("dict4_first_key", 0)
repeat with i from 1 to 3
dict4's addValueForKey("dict4_key_" & ((i + 2) as text), i)
end repeat
set dict5 to newDictionary()
dict's addValueForKey("a_string", "Hello World")
dict's addValueForKey("a_string_with_quotes", "Hello \"World\"")
dict's addValueForKey("a_string_with_backslashes", "Hello World \\\\")
dict's addValueForKey("a_string_with_backslashes_and_quotes", "Hello \"\\\\\"World \\\\")
dict's addValueForKey("some_text", "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.")
dict's addValueForKey("an_emoji", "👾")
dict's addValueForKey("a_boolean_true", true)
dict's addValueForKey("a_boolean_false", false)
dict's addValueForKey("an_integer", 1234)
dict's addValueForKey("a_negative_integer", -1234)
dict's addValueForKey("a_float", 1.234)
dict's addValueForKey("a_negative_float", -1.234)
dict's addValueForKey("a_large_number", 1.23456789012346E+19)
dict's addValueForKey("a_date", current date)
dict's addValueForKey("a_record", {a:"1", b:"2", c:"3"})
dict's addValueForKey("a_large_record", {a:"Hello World,
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.", b:"Magna aliquyam erat, sed diam voluptua.
At vero eos et accusam,"})
dict's addValueForKey("empty_list", {})
dict's addValueForKey("empty_dict", dict5)
dict's addValueForKey("full_list", {"A", "B", "C"})
dict's addValueForKey("mixed_list", {"A", "👾", 1234, 1.234, current date, {}})
dict's addValueForKey("another_dict", dict2)
dict's addValueForKey("delete_me", "ABC")
dict's removeValueForKey("delete_me")
dict's addValueForKey("multiple_dicts", {dict3, dict4})
dict's addValueForKey("nested_list", {{{{{{}}}}}})
dict's addValueForKey("nested_lists", {{{}}, {{}}, {{}}})
dict's addValueForKeyPathRecursively("path/to/a/deeply/nested/dictionary", newDictionaryWithValueForKey("pi", 3.14159265359))
dict's addValueForKeyPath("path/to/a/deeply/nested/dictionary/phi", 1.61803398875)
set dataBefore to dict's recordRepresentation()
log "***********************************************************************"
log " DATA IN MEMORY "
log "***********************************************************************"
log dataBefore
log "***********************************************************************"
log " WRITING "
log "***********************************************************************"
dict's writeToFile(kTestFilePath)
log "***********************************************************************"
log " READING "
log "***********************************************************************"
set dict6 to newDictionary()
dict6's readFromFile(kTestFilePath)
set dataAfter to dict6's recordRepresentation()
log "***********************************************************************"
log " DATA FROM DISK "
log "***********************************************************************"
log dataAfter
log "***********************************************************************"
log " TESTS "
log "***********************************************************************"
set testNumber to 1
log "Running test " & testNumber
set r to dict6's existsKey("an_integer")
if r is not true then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 2
log "Running test " & testNumber
set r to dict6's existsKey("delete_me")
if r is not false then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 3
log "Running test " & testNumber
set r to dict6's valueForKey("a_string_with_backslashes")
if r is not "Hello World \\\\" then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 4
log "Running test " & testNumber
set r to dict6's valueForKey("a_string_with_backslashes_and_quotes")
if r is not "Hello \"\\\\\"World \\\\" then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 5
log "Running test " & testNumber
set r to dict6's classForKey("a_float")
if r is not real then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 6
log "Running test " & testNumber
set r to dict6's classForKey("a_boolean_true")
if r is not boolean then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 7
log "Running test " & testNumber
set r to dict6's classForKey("an_integer")
if r is not integer then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 8
log "Running test " & testNumber
set r to dict6's classForKey("a_float")
if r is not real then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 9
log "Running test " & testNumber
set r to dict6's classForKey("a_date")
if r is not date then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 10
log "Running test " & testNumber
set r to dict6's classForKey("mixed_list")
if r is not list then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 11
log "Running test " & testNumber
set r to dict6's classForKey("a_record")
if r is not record then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 12
log "Running test " & testNumber
set r to dict6's valueForKeyPath("path/to/a/deeply/nested/dictionary/pi")
if r is not 3.14159265359 then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
set testNumber to 12
log "Running test " & testNumber
set r to dict6's valueForKeyPath("path/to/a/deeply/nested/dictionary/phi")
if r is not 1.61803398875 then
log "Sub-test " & testNumber & " failed with result: " & r
error "Test failed" number testNumber
end if
log "Long test ran successfully"
on error eMsg number eNum
set eMsg to "Long test failed: " & eMsg
log eMsg & " (" & (eNum as text) & ")"
end try
end longTest
on newDictionaryFromRecordRepresentation(aRecord)
(*
Creates a new dictionary from its record representation
*)
set dict to newDictionary()
return dict's _convertLoadedDict(aRecord)
end newDictionaryFromRecordRepresentation
on newDictionaryFromTextRepresentation(aString)
(*
Creates a new dictionary from its record representation
*)
set dict to newDictionary()
tell dict
set filePath to _uniqueFileHFSPath(path to temporary items folder as text, "applescript-dict_text-representation", "txt")
_writeTextToFile(aString, filePath)
readFromFile(filePath)
end tell
return dict
end newDictionaryFromTextRepresentation
on newDictionaryWithValueForKey(k, v)
(*
Creates a new dictionary adding the value for the specified key.
*)
set dict to newDictionary()
dict's addValueForKey(k, v)
return dict
end newDictionaryWithValueForKey
on newDictionary()
(*
Returns an empty dictionary.
*)
script Dictionary
property _version : 1
property _keyValuePairs : {}
property _keyList : {}
property _keyListAsText : ASCII character 10
property _valueTypes : {}
property _keyPathDelimiter : "/"
property _longestKeyInDict : false
property _nl : " ¬" & (ASCII character 10)
property _indentation : " "
property _decimalPointSymbol : false
(* Section: Instance Functions *)
on ________________________________General()
end ________________________________General
on isDictionary(obj)
(*
Checks whether the specified object is a dictionary.
Example:
```
set dict to newDictionary()
set dict2 to newDictionary()
return dict's isDictionary(dict2)
```
*)
try
return _typeForValue(obj) is "script/Dictionary"
on error eMsg number eNum
_handleError("isDictionary", eMsg, eNum)
end try
end isDictionary
on empty()
(*
Checks whether this dictionary is empty.
Example:
```
set dict to newDictionary()
if dict's empty() then
-- Do something
end if
```
*)
try
if (count of _keyList) is 0 then
return true
else
return false
end if
on error eMsg number eNum
_handleError("empty", eMsg, eNum)
end try
end empty
on allKeys()
(*
Returns all keys.
Example:
```
set dict to newDictionary()
repeat with i from 1 to 4
dict's addValueForKey("key_" & i as text, i)
end repeat
return dict's allKeys()
```
*)
try
return _keyList
on error eMsg number eNum
_handleError("allKeys", eMsg, eNum)
end try
end allKeys
on keyCount()
(*
Returns the count of all keys.
Example:
```
set dict to newDictionary()
repeat with i from 1 to 4
dict's addValueForKey("key_" & i as text, i)
end repeat
return dict's keyCount()
```
*)
try
return count of _keyList
on error eMsg number eNum
_handleError("keyCount", eMsg, eNum)
end try
end keyCount
on allKeysRecursively()
(*
Returns the keys of this dictionary and all its nested dictionaries. The keys of nested dictionaries are returned as key paths.
Example:
```
set dict to newDictionary()
dict's addValueForKeyPathRecursively("a/b/c/d/e/f", "test")
dict's addValueForKeyPathRecursively("a/b/x/d/e/f", "test")
dict's addValueForKeyPathRecursively("z/b/x/d/e/f", "test")
return dict's allKeysRecursively()
```
*)
try
return _recursiveKeys()
on error eMsg number eNum
_handleError("allKeysRecursively", eMsg, eNum)
end try
end allKeysRecursively
on allValues()
(*
Returns all values of this dictionary.
Example:
```
set dict to newDictionary()
repeat with i from 1 to 4
dict's addValueForKey("key_" & i as text, i)
end repeat
return dict's allValues()
```
*)
try
if _keyList is {} then return {}
set collectedValues to {}
repeat with i from 1 to count of _keyList
set end of collectedValues to _value(i)
end repeat
return collectedValues
on error eMsg number eNum
_handleError("allValues", eMsg, eNum)
end try
end allValues
on ________________________________Key()
end ________________________________Key
on existsKey(aKey)
(*
Returns true if the specified key exists, otherwise false.
*)
try
set aKey to _sanitizeKey(aKey)
return _existsKey(aKey)
on error eMsg number eNum
_handleError("existsKey", eMsg, eNum)
end try
end existsKey
on tryKey(aKey, defaultValue)
(*
If the specified key exists, its value is returned. Otherwise the value specified as **defaultValue** is returned.
*)
try
set aKey to _sanitizeKey(aKey)
if not _existsKey(aKey) then
return defaultValue
else
return _valueForKey(aKey)
end if
on error eMsg number eNum
_handleError("tryKey", eMsg, eNum)
end try
end tryKey
on addValueForKey(aKey, aValue)
(*
Adds the value for the specified key. The key must not exists. If it does an error 2 is raised.
*)
try
set aKey to _sanitizeKey(aKey)
try
set pos to _pos(aKey)
on error
set pos to false
end try
if pos is not false then
error "Key \"" & aKey & "\" already exists" number 2
else
_add(aKey, aValue, false)
end if
on error eMsg number eNum
_handleError("addValueForKey", eMsg, eNum)
end try
end addValueForKey
on removeValueForKey(aKey)
(*
Removes the value for the specified key. The key must exist otherwise error 1 is raised.
*)
try
set aKey to _sanitizeKey(aKey)
log "Removing value for key \"" & aKey & "\""
set keyPosition to _pos(aKey)
set previousValue to _value(keyPosition)
log "Removing value for " & aKey
set _keyList to _removePositionFromList(_keyList, keyPosition)
set _keyListAsText to (ASCII character 10) & _implodeList(_keyList, ASCII character 10, false) & (ASCII character 10)
set _keyValuePairs to _removePositionFromList(_keyValuePairs, keyPosition)
set _valueTypes to _removePositionFromList(_valueTypes, keyPosition)
return previousValue
on error eMsg number eNum
_handleError("removeValueForKey", eMsg, eNum)
end try
end removeValueForKey
on setValueForKey(aKey, aValue)
(*
Sets the value for the specified key. The key must exist otherwise error 1 is raised.
*)
try
set aKey to _sanitizeKey(aKey)
set pos to _pos(aKey)
_set(pos, aKey, aValue, false)
on error eMsg number eNum
_handleError("setValueForKey", eMsg, eNum)
end try
end setValueForKey
on valueForKey(aKey)
(*
Returns the value for the specified key. The key must exist otherwise error 1 is raised.
*)
try
set aKey to _sanitizeKey(aKey)
return _valueForKey(aKey)
on error eMsg number eNum
_handleError("valueForKey", eMsg, eNum)
end try
end valueForKey
on typeForKey(aKey)
(*
Returns a string representing the type of the value for the specified key. The key must exist otherwise error 1 is raised.
*)
try
return _typeForPair(_pairForKey(_sanitizeKey(aKey)))
on error eMsg number eNum
_handleError("typeForKey", eMsg, eNum)
end try
end typeForKey
on classForKey(aKey)
(*
Returns the AppleScript class for the value of the specified key. The key must exist otherwise error 1 is raised.
*)
try
return _classForPair(_pairForKey(_sanitizeKey(aKey)))
on error eMsg number eNum
_handleError("classForKey", eMsg, eNum)
end try
end classForKey
on positionForKey(aKey)
(*
Returns the 1-based position for the specified key. The key must exist otherwise error 1 is raised.
*)
try
set aKey to _sanitizeKey(aKey)
return _pos(aKey)
on error eMsg number eNum
_handleError("positionForKey", eMsg, eNum)
end try
end positionForKey
on indexForKey(aKey)
(*
Returns the 0-based index for the specified key. The key must exist otherwise error 1 is raised.
*)
try
set aKey to _sanitizeKey(aKey)
return _pos(aKey) - 1
on error eMsg number eNum
_handleError("positionForKey", eMsg, eNum)
end try
end indexForKey
on ________________________________Key_path()
end ________________________________Key_path
on existsKeyPath(keyPath)
(*
Returns true if the key path can be fully satisified, otherwise false.
*)
try
return _existsKeyPath(_sanitizeKeyPath(keyPath), false)
on error eMsg number eNum
_handleError("existsKeyPath", eMsg, eNum)
end try
end existsKeyPath
on tryKeyPath(keyPath, defaultValue)
(*
Returns the value for the key path if a value is found, other **defaultValue** is returned.
*)
try
set keyPath to _sanitizeKeyPath(keyPath)
if not existsKeyPath(keyPath) then
return defaultValue
else
return valueForKeyPath(keyPath)
end if
on error eMsg number eNum
_handleError("tryKeyPath", eMsg, eNum)
end try
end tryKeyPath
on addValueForKeyPath(keyPath, aValue)
(*
Adds the specified value at the key path. The dictionaries found along the path must exist. The final key must not exist.
*)
try