so i got my string in perfect format for a record, at least it looks like it.
but it seems i can’t coerce it as a record.
my string looks like this: “userid:"11111", name:"Jxxxs Bxx.", username:"research"”
do i have to worry about the “escaped” forms of the quotation marks (" representing ")?
or can’t you coerce a string to a record?
i also tried piping (using |) userid, no result…
???
when i try to get a certain value out of this as a record, applescript tells me it can’t…
i do it like this:
set myData to "userid:\"11111\", name:\"Jxxxs Bxx.\", username:\"research\""
display dialog (get userid of myData as record)
You would use ‘run script’, because you can’t coerce string to record. Something like this:
set myData to "{userid:\"11111\", name:\"Jxxxs Bxx.\", username:\"research\"}"
set myRecord to run script myData
userid of myRecord
However this is not a good way to create records as you would need to know beforehand what the record labels are anyway. It would be better to start with a predefined record or use some other way of storing your data.
set new_rec to {userid:missing value,name:missing value,username:missing value}
the thing is, i get these information from the upcoming.org api (in xml format).
i could maybe build a record with the variables i expect to get back from the api, but what if these change.
so i thougt it would be good to kind of look for the term user id and get its value, and i thougt the easiest way to do this, is if i put the returned data into a record.
the example data i provided in my previous post is the reformated api return (i deleted <'s and replaced = with : and stuff, so it fits the way a record should look like)
but it doesn’t work like a record…
so what other ways of storing and reading out data would you recommend?
There are many ways to store data, so this is a very difficult question. You might want to create a new post after asking yourself a few questions.
What you want to do is create records where you can add fields dynamically. When you add fields do you want to update previously entered records to reflect the added fields? Do you want to delete fields also? If you delete fields, then you need to delete the fields from each record. How many records are you planning to have? Note that there is a limit when using lists. You can store a lot more records as text. How many fields max will there be? Do you want search and r replace functions (like changing passwords)?
Here’s a simple database script example which just uses lists.
script DB
property key_list : {"name", "date"}
property database : {{"me", (current date) as string}}
--
on AddKey()
display dialog "Enter the new key:" default answer ""
set t to text returned of result
set end of key_list to t
return
end AddKey
--
on AddRecord()
set new_rec to {}
repeat with this_key in key_list
if this_key is "date" then
set t to (current date) as string
else
display dialog ("Enter " & this_key & ":") default answer ""
set t to text returned of result
end if
set end of new_rec to t
end repeat
set end of database to new_rec
return
end AddRecord
--
on SearchDB(this_key, this_name)
set d to false
-- get index of key
set i to 0
repeat
set i to i + 1
set k to item i of key_list
if k is this_key then
set d to true
exit repeat
end if
end repeat
if d then
repeat with this_rec in database
set this_item to item i of this_rec
if this_item is this_name then
set d to this_rec
exit repeat
end if
end repeat
end if
return (contents of d)
end SearchDB
end script
--
choose from list {"add key", "add record", "search by name"}
set user_choice to item 1 of result
tell DB
if user_choice is "add key" then
AddKey()
else if user_choice is "add record" then
AddRecord()
else if user_choice is "search by name" then
display dialog "Enter a name to search for:" default answer ""
set t to text returned of result
SearchDB("name", t)
end if
end tell
Note that I only used three functions which simple AppleScript repeat loops. There isn’t any error checking. This shows what a big job it is just doing this in AppleScript.
You might want to check out hhas database scripts. I forgot what the name of it was.