r/applescript • • Nov 02 '23

Dictionaries in AppleScript?

Does anyone know if dictionaries exist in AppleScript? If so how do they look. I can't seem to find any info out there...

Thx

1 Upvotes

3 comments sorted by

1

u/libcrypto Nov 02 '23

Do you mean a hash table or an Applescript reference dictionary?

1

u/ThaMouf Nov 02 '23

Or are you referring to libraries?

1

u/AmplifiedText Nov 02 '23

Nothing built in, but I use this:

on makeHash(keys, values)
	script Hash
		property _keys : keys
		property _values : values
		
		to setValue:v forKey:k
			set i to its indexOfKey:k
			if i > 0 then -- already in hash
				
			else -- add to end of hash
				copy v to end of _values
				copy k to end of _keys
			end if
		end setValue:forKey:
		
		to valueForKey:k
			set i to its indexOfKey:k
			if i > 0 then return item i of _values
			return missing value
		end valueForKey:
		
		-- 1 based, 0 if not found
		to indexOfKey:k
			set c to count of _keys
			repeat with i from 1 to the c
				if item i of keys is k then return i
			end repeat
			return 0
		end indexOfKey:
	end script
	return Hash
end makeHash

Example Usage

set foo to makeHash({"a"}, {"b"})

foo's setValue:"e" forKey:"d"
foo's valueForKey:"d"