r/applescript • u/lkvmh • Jul 21 '22
How to Loop through a record
Hi,
I did a script to fetch cryptocurrencies prices using apple script and Json helper in order to update a Numbers' sheet. I'm currently stuck here :
global recordNumbers
global recordFetched
set recordNumbers to {{|name|:"cardano", price:null}, {|name|:"secret", price:null}}
set recordFetched to {{cardano:{eur:0.495413}}, {secret:{eur:1.29}}}
|name| of item 2 of recordNumbers
repeat with coinId from 1 to count recordNumbers
set coinName to |name| of item coinId of recordNumbers
set coinPrice to eur of coinName of item coinId of recordFetched
end repeat
coinName
coinPriceThe
recordNumbersIs the record fetched from my numbers sheet, I just wrote some results of the executed corresponding code.
The
recordFetchedis the record fetched through the coingecko API and JSON Helper, I just wrote some results of the executed corresponding code here.
I'm stuck at successfully setting coinPrice to the value displayed in the record fetch :
In the the first iteration I want to set "coin Price" to 0.495413 and in the second iteration to 1.29.
It seems to me the apple script doesn't replace
coinName in
set coinPrice to eur of coinName of item coinId of recordFetchedby the value successfully set in
set coinName to |name| of item coinId of recordNumbersDo you have an idea of why ? How would you fix it ? Thanks
1
u/estockly Jul 21 '22
I think the problem is how records are handled in AppleScript compared to the way it handles lists.
The record format is: {itemLabel: itemValue, nextItemLabel: nextItemValue} and so on.
The list format is: {itemValue, nextItemValue} and so on.
(in your script recordNumbers and recordsFetched are both lists of records)
The problem is that you cannot (easily) create and use item labels for records on the fly. They have to be hard wired into the script.
This would work:
set coinPrice to eur of cardano of item coinId of recordFetchedIf you can't hard wire the record label (cardano, in this case) because of the way the data is provided, you can, possibly, use a script object to workaround. If that's the case I think I have an example.
0
u/[deleted] Jul 21 '22
[deleted] — view deleted comment