Skip to content

adriannier/applescript-dictionary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Dictionary.applescript

Contents

Overview

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

Script Functions

Runs all tests.

Performs a short test.

Performs a speed test creating 1000 dictionary items and modifying them.

Performs a long test.

Creates a new dictionary adding the value for the specified key.

Returns an empty dictionary.

General

Checks whether the specified object is a dictionary.

Example:

set dict to newDictionary()
set dict2 to newDictionary()
return dict's isDictionary(dict2)

Checks whether this dictionary is empty.

Example:

set dict to newDictionary()

if dict's empty() then

	-- Do something
	
end if

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()

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()

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()

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()

Key

Returns true if the specified key exists, otherwise false.

If the specified key exists, its value is returned. Otherwise the value specified as defaultValue is returned.

Adds the value for the specified key. The key must not exists. If it does an error 2 is raised.

Removes the value for the specified key. The key must exist otherwise error 1 is raised.

Sets the value for the specified key. The key must exist otherwise error 1 is raised.

Returns the value for the specified key. The key must exist otherwise error 1 is raised.

Returns a string representing the type of the value for the specified key. The key must exist otherwise error 1 is raised.

Returns the AppleScript class for the value of the specified key. The key must exist otherwise error 1 is raised.

Returns the 1-based position for the specified key. The key must exist otherwise error 1 is raised.

Returns the 0-based index for the specified key. The key must exist otherwise error 1 is raised.

Key path

Returns true if the key path can be fully satisified, otherwise false.

Returns the value for the key path if a value is found, other defaultValue is returned.

Adds the specified value at the key path. The dictionaries found along the path must exist. The final key must not exist.

Adds the specified value at the key path. The dictionaries found along the path will be created automatically. The final key must not exist.

Removes the value at the specified key path.

Sets the value at the specified key path. The final key and all the keys along the path must exist.

Returns the value at the specified key path.

Returns a string representing the type of the value at the specified key path. Error 1 is raised when any of the keys along the path do not exist.

Returns the AppleScript class for the value at the specified key path. Error 1 is raised when any of the keys along the path do not exist.

Input Output

Writes this dictionary to a text file at the specified path. The path should end with .applescript

Reads data for this dictionary from a text file generated by writeToFile() at the specified path.

Representations

Returns a textual representation of this dictionary.

Returns this dictionary as an AppleScript record

About

A native AppleScript implementation of a mutable key/value storage

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published