Here's what I get when using the script (pasted back in below):
Note that the script keeps running if you give an invalid symbol, and the cells are filled with N/A.
Just be sure not to have any blank rows in the middle of the table or at the bottom and it should work.
SG
--In the following line specify the types of data to retrieve in the order desired:
property quoteProperties : "l1r0e7e8s6"
(*
See https://code.google.com/p/yahoo-finance-managed/wiki/enumQuoteProperty
Example: last trade price followed by pe followed by this year's estimated EPS ==> "l1roe7"
*)
--In the following line change the default values in black as needed to target the desired table:
property t : {targetDoc:"InvestimentosMAC.numbers", targetSheet:"Base", targetTable:"Tabela", symbolsColNum:1, symbolsStartRow:2}
(*
Example: To populate a table named "Quotes" in sheet "Portfolio A" in document "Investments" with symbols starting on row 2 of column B==>
{targetDoc:"Investments.numbers", targetSheet:"Portfolio A", targetTable:"Quotes",symbolsColNum:2, symbolsStartRow:2}
*)
property decSymbol : ","
tell application "Numbers" to tell document (t's targetDoc) to tell sheet (t's targetSheet) to tell table (t's targetTable)
tell column (t's symbolsColNum) to set symbConcat to my joinList(value of cells (t's symbolsStartRow as number) thru (count cells), "+")
try
set the clipboard to my commaToTab(my getYData(symbConcat, quoteProperties))
--add this line to convert decimals from . to , ("European format")
set the clipboard to my findReplace(the clipboard, ".", ",")
set pasteStr to the clipboard
on error
return --halt script if error getting Yahoo data
end try
set the selection range to cell (t'ssymbolsStartRow) of column ((t'ssymbolsColNum) + 1)
tell application "Numbers" to activate
tell application "System Events" to keystroke "v" using {option down, shift down, command down}
display notification "Stock data has been updated" with title "Numbers"
end tell
to getYData(qSymb, qProp) -- get Yahoo! data
try
set baseURL to "http://download.finance.yahoo.com/d/quotes.csv?"
set {symbStr, propStr} to {"s=" & qSymb, "&f=" & qProp}
set yData to do shell script "curl -s " & quoted form of (baseURL & symbStr & propStr)
if yData's text 1 thru 2 is "<!" then error --intercept Yahoo error page
if yData's text 1 thru 3 is "0.0" then error --a little more error-checking
return yData
on error
display alert "Trouble getting data from Yahoo! Check symbols. Ensure there are no blanks in the column and no footer rows, and that you have the right values in t's properties."
return
end try
end getYData
to joinList(aList, separator) --convert AS list to delimited string
set {oTid, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, separator}
set lstStr to aList as string
set text item delimiters of AppleScript to oTid
return lstStr
end joinList
to commaToTab(str) -- Numbers 3 wants tab-separated for pasting
try
set {oTid, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, ","}
set strLst to text items of str
set AppleScript'stext item delimiters to tab
set tsvStr to strLst as text
set AppleScript'stext item delimiters to oTid
return tsvStr
on error
display alert "Error!"
return
end try
end commaToTab
to findReplace(tt, f, r)
set {AppleScript'stext item delimiters, oTid} to {f, AppleScript'stext item delimiters}
set lst to text items of tt
set AppleScript'stext item delimiters to r
set tt to lst as string
set AppleScript'stext item delimiters to oTid
return tt
end findReplace
--end of script