Using a variable to access a property

What I’m doing is creating lists for each of the properties of the date string (Weekday, Month, Day, Year) that are relavent to my scripting needs. I list each potential property value as a key, and assign it a unique string as it’s value. I am having trouble dynamically accessing a key by name and being able to retrieve it’s value.

I’ve create a list of key/value pairs as a property variable in the head of my script. I’ll work here with only the “Weekday” property, as the rest will use the same process on their respective properties of the date string.

property WeekdayValuesList : {|Monday|:"1", |Tuesday|:"2", |Wednesday|:"3", |Thursday|:"4", |Friday|:"5", |Saturday|:"6", |Sunday|:"7"}

Then, later in the script I get the current date (or any other date) and split it up into four variables representing each of the properties listed above. This is no problem, I have the values for each of the current date properties ready to go in string form…

set originalWeekday to weekday of theCurrentDate as string

But then, I want to use each of these variables to dynamically reference the appropriate list, and set a second variable to the value of the list item that matches the key. For example, if the current weekday is “Saturday”, I want to get the value of the key “Saturday” from the ‘WeekdayValuesList’ (which is 6), and set it as the value for the variable ‘modifiedWeekday’. I can access the values by hard-coding the reference to them with…

set modifiedWeekday to |Monday| of WeekdayValuesList

But what I want to do is something to th effect of…

set modifiedWeekday to originalWeekday of WeekdayValuesList

I have tried many variations on this code and cannot get a variable to access the key/value pairs. I am not sure if the problem is caused by the fact that I am referencing predefined AS variables, or if my coding is just ugly. :wink:

Any thoughts?..Thanks in advance…
j

What you expose could be done, but it’s very tricky and can carry unexpected results. I’d use a more simple construct. Eg:

property WeekdayValuesList : "
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday"

set originalWeekday to weekday of (current date) as string

-1 + (count (paragraphs of (text 1 thru (offset of return & originalWeekday in WeekdayValuesList) of WeekdayValuesList))) as text
--> eg, "6"

Or:

set WeekdayValuesList to "  Mo  Tu  We  Th  Fr  Sa  Su"

set originalWeekday to "  " & text 1 thru 2 of (weekday of (current date) as string)
((offset of originalWeekday in WeekdayValuesList) + 3) / 4 as integer as text
--> eg, "6"

Unfortunately I don’t actually want a value as easy as the number “6” for “Saturday” :cry:. The numbers were just my test strings so if I had some success reading the list I could tell which key/value pair was actually being used. In practice the list would really look like the code below, with the values being a handful of characters long and without any real structure, whatsoever. That is the point…that it is close to random but with an easy code key. I would essentially like to encode the date (or any data) with seemingly random strings so I can package the data, say in a text or preferences file, and only be able to extract the date string with the variables I’ve “hidden” in the code of the script. I would like to incorporate this method into finding a way to disguise data that has a predictable format, i.e. date, passwords, usernames, etc. It could be used to do basic encryption of data to enable trial versions, mask passwords, etc.

For example…
An application acquires a string of data (‘regularString’), like a password, either through UI or internally. The script has a variable (or variables) saved in it…

property : encodeCharacters {a:"h47n48",b:"j49fn9",c:"9b325", etc...}

The script breaks apart ‘regularString’ into characters, finds the corresponding value in the list for each character, and appends it onto another variable (‘modifiedString’) which stores the translated/encrypted value. So, if ‘regularString’ was “cab” the script might then set ‘modifiedString’ as a somewhat inhumane “9b325-h47n48-j49fn9”, rather than the obvious “cab”. Then it could save it to file, send it to mars, whatever… When the data was encountered again by the script, the key to unlocking it is saved in the list.

The goal is to create an easy way to turn legible data, into an encoded string of characters that only my script can decode. I can already think of a few ways this could be useful for AS designers, and plan to release my procedures to anyone in need of some basic encrypting and security abilities that are AS native.

Thanks for any more input…
j