I’m trying to merge two sets of files from two servers based on a job number. So I ask the user for a list of job numbers, then I need to find the location of the jobs on each server, but I don’t know how many jobs the user may input.
So here’s what I’m thinking…
Each record would look something like this:
set job_record to {job_number: 12345, server1: “Prinergy:Jobs:12345_Client”, server2: “OSX:5_Jobs:12345_Client.06.15.06”}
then to take in the job numbers…
repeat
set data_in to display dialog “Enter Job Number” default answer “” buttons {“Cancel”,“Done”,OK"} default button 3 with icon note
if button returned of data_in is “Done” then exit repeat
set job_num to text returned of data_in
set job_list to job_list & {job_number:data_in, server1: “”, server2: “”} --Server locations will be set later
end repeat
Sorry if my syntax is off… I left my files at work. Everything seems to run fine, but when I try to access the data, I’m getting an error message that item 1 can not be accessed.
I’m trying to access data with a command like:
set x to job_number of job_list
Is this possible… or do I have to define each record individually and then join them together into a list like:
set list1 to {record1, record2, record3…etc}
It early here so it may explain why its a bit confusing as to what your after.
This will compile a list for you.
Then use the repeat with section to target each item of the list and run a action on it.
set job_list to {}
my _dipslay(job_list)
on _dipslay(job_list)
set text_returned to ""
display dialog "Enter Job Number" default answer "" buttons {"Cancel", "Done", "Ok"} default button 3
copy the result as list to {text_returned, button_pressed}
if the button_pressed is "Ok" then
copy text_returned to end of job_list
--copy "job_number :" & text_returned to end of job_list -- example of add other bits in
my _dipslay(job_list)
else if the button_pressed is "Done" then
if text_returned is not equal to "" then -- in case they hit done after the last entry
copy text_returned to end of job_list
end if
end if
end _dipslay
repeat with i from 1 to number of items in job_list
set this_item to item i of job_list
-- insert actions here
end repeat
It’s not totally clear what you’re trying to do. I think you’re saying you want to create a list of records, each record with a unique ‘job_number’ value and with ‘server1’ and ‘server2’ properties that will be set more accurately later.
There are couple of technicalities that can catch you out when you concatenate records to things. It’s simpler and faster to use the construction:
set end of job_list to {job_number:data_in, server1: “”, server2: “”}
When you’ve got your list of records, each record will be a item in the list, so to read the job number of, say, the first one, you’d need something like:
set x to job_number of item 1 of job_list
Or:
set this_job to item 1 of job_list
set x to job_number of this_job
set job_list to {}
repeat
set data_in to display dialog "Enter Job Number" default answer "" buttons {"Cancel", "Done", "OK"} default button 3 with icon note
if button returned of data_in is "Done" then exit repeat
set job_num to text returned of data_in
if (job_num is not "") then -- Mark's suggestion.
set end of job_list to {job_number:job_num, server1:"", server2:""} --Server locations will be set later
end if
end repeat
repeat with this_job in job_list
set job_num to this_job's job_number
-- etc.
set this_job's server1 to "Prinergy:Jobs:12345_Client" -- or whatever
set this_job's server2 to "OSX:5_Jobs:12345_Client.06.15.06" -- ditto
end repeat
Thanks Nigel and Mark,
I tried what you suggested and it worked like a charm.
One more question though…
what’s the difference between these two statements (in relation to a list of records)
set x to x & {num:1234, server: “OSX:”}
and
set end of x to {num:1234, server “OSX:”}
I would think that the first one would add a record, but it actually seems to replace data in the list… and the second one I would think would replace the data, but it adds a record… I don’t get it.
set x to {N:4567}
set x to x & {num:1234, server:"OSX:"} --> {N:4567, num:1234, server:"OSX"
If x is not a record, then you get a list when the record is coerced to match x (coercion adapts the first item’s type)
In your second example, your statement errors. If you want to add to the end concatenate:
set x to {N:4567}
set y to {num:1234, server:"OSX:"}
set x to x & y --> {N:4567, num:1234, server:"OSX:"}
-- or
set x to y & x --> {num:1234, server:"OSX:", N:4567}
Well… Assuming for the moment that x is a list, the second statement inserts the record into the end of the list. The first statement, on the other hand, performs a “concatenation” of the two objects ” that is, it creates a third object which is the result of joining them “end to end” and sets x to this new object instead of to the original list.
The concatenation rules (going on observed behaviour rather than by what it says in the AppleScript Language Guide) are basically these:
1.) If the item to the left of the ampersand is of a concatenatable class (string, Unicode text, list, or record), and the item on the right has the same class, a simple concatenation takes place. The result has the same class as the two originals.
set x to {1, 2, 3, 4, 5} & {6, 7, 8, 9, 10} -- Two lists.
--> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
2.) If the item on the left is concatenatable, and the item on the right has a different class but can be coerced to the same class, the coercion takes place and the result takes part in the concatenation. The concatenation result has the same class as the left hand item.
set x to {1, 2, 3, 4, 5} & 10 -- List & integer.
--> set x to {1, 2, 3, 4, 5} & {10} -- Integer coerced to list.
--> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
3.) If the item on the left is concatenatable, and the item on the right has a different class and can’t be coerced to the same class, an error occurs.
4.) If the item on the left isn’t concatenatable, it is coerced to list and the concatenation goes ahead on that basis.
set x to 1 & "Hello" -- Integer & string.
--> set x to {1} & "Hello" -- Integer coerced to list.
--> set x to {1} & {"Hello"} -- String coerced to list.
--> {1, "Hello"}
set x to 10 & {1, 2, 3, 4, 5} -- Integer & list.
--> set x to {10} & {1, 2, 3, 4, 5} -- Integer coerced to list.
--> {10, 1, 2, 3, 4, 5}
5.) In a concatentation involving a string and a Unicode text, the result is Unicode text regardless of the order in which they’re presented.
From 2.) above, you’ll see that when a record is concatenated to a list, the record is first coerced to list. Coercing most items to list produce a single-item list, but coercing a record to list converts all its record properties to list items:
{num:1234, server:"OSX:"} as list
--> {1234, "OSX:"}
And that’s what happens if you concatenate a record to a list:
set x to {{num:1233, server:"Aardvark:"}} & {num:1234, server:"OSX:"}
--> set x to {{num:1233, server:"Aardvark:"}} & {1234, "OSX:"} -- Record coerced to list.
--> {{num:1233, server:"Aardvark:"}, 1234, "OSX:"}
But it’s probable that you’ll have initialised x to {} before starting the concatenations. {} occupies a limbo between being an empty list and an empty record. If you concatenate a record to it, the result will be a record; if you concatenate anything else, the result will be a list. So, with the serial concatenation of records, you’re actually concatenating record to record rather than record to list.
When you concatenate two records together, the result is another record whose properties are an amalgam of those of the original records. Any properties that are common to both records take their values from the record on the left.
set x to {a:"Fred", b:"Bill"} & {a:1, c:2, d:3}
--> {a:"Fred", b:"Bill", c:2, d:3}
In your case, since all the records have the same properties, you end up with a single record whose property values are identical to the first one.
set x to {}
set x to x & {num:1234, server:"OSX:"}
--> {num:1234, server:"OSX:"}
set x to x & {num:1235, server:"Aardvark:"}
--> {num:1234, server:"OSX:"}
-- etc.
So, using ‘set end of’ is a good way to avoid all that.
Concatenation is possible if you present each new record in a list, but it’s not nearly as efficient as ‘set end of’.
set x to {}
set x to x & {{num:1234, server:"OSX:"}} -- Prewrap the record in a list, for list concatenation.
--> {{num:1234, server:"OSX:"}}
set x to x & {{num:1235, server:"Aardvark:"}}
--> {{num:1234, server:"OSX:"}, {num:1235, server:"Aardvark:"}}