Any way to iterate two lists simultaneously?

I’m aware of the fact that AppleScript can only handle a single thread at a time, but many times, there has been ways to work around those limitations even if it meant that the script weren’t as elegant as it would be in the perfect world. So, just investigating that option again.

Let’s say I have a LIST A that contains items 1, 2 and 3.

It’s a very basic thing to iterate objects of a list and perform some repetitive action onto them all as you go, fine. That is: do X to 1, 2, and 3, which are the items contained by LIST A.

But what if I have another list, LIST B, that contains items X, Y and Z;
Is there a way to simultaneously iterate both lists so that instead of doing one and the same thing with every item of LIST A, you could get 1X, 2Y and 3Z?

Hi. Most likely, you mean this simple process:


set aList to {1, 2, 3}
set bList to {"X", "Y", "Z"}

repeat with i from 1 to count bList
	set item i of aList to ((item i of aList) as string) & item i of bList
end repeat

return aList

Or, this:


set aList to {Action1, Action2, Action3}
set bList to {ActionX, ActionY, ActionZ}

repeat with i from 1 to count bList
	my performActionWithNumber(item i of aList)
	my performActionWithLeter(item i of bList)
end repeat

on performActionWithNumber(anAction)
	-- process number
end performActionWithNumber

on performActionWithLeter(anAction)
	-- process letter
end performActionWithLeter

Thank you so much!
The actual script is quite a bit more complex (escaping JSON quotes and all that mess, anyone?) and it will take me some time to apply the technique to what I have here so far. But let’s assume this problem solved unless I return! :wink:

Here, on the “Code Exchange” forum, there is a very interesting script that may suit you even more - Coerce keys and values into a record. The author of the script is DJ Bazzie Wazzie:


set theKeys to {"Hello world!", "B", "name", "D"}
set theValues to {true, current date, "hello", 1}

createUserDefinedRecord(theKeys, theValues)

on createUserDefinedRecord(theKeys, theValues)
	-- Remove keys that are not strings
	set theKeys to every text of theKeys
	-- If there are more keys than values then stop
	if (count of theKeys) > (count of theValues) then return missing value
	
	-- Create a list containing keys subsequently followed by it's associated value
	set rawList to {}
	repeat with i from 1 to count theKeys
		set end of rawList to item i of theKeys
		set end of rawList to item i of theValues
	end repeat
	
	-- Create a record who is similar to records containing only user defined keys in AppleEvents
	set rawRecord to {«class usrf»:rawList}
	
	-- A script retuning the same value as given to force coercion between AppleEvent and AppleScript later
	script x
		on run argv
			return item 1 of argv
		end run
	end script
	
	-- We'll use run script, osax command, so rawRecord is handles by the
	-- AppleEvent manager whose value is given back in the same 
	-- format as records containing user defined keys. AppleScript will coerce
	-- the list in a proper way and make the coercion for us.
	return run script x with parameters {rawRecord}
end createUserDefinedRecord