|
|
AppleScript help
|
|
|
|
Registered User
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Posting in this forum because will get more views.
I have this AS gotten from the Apple forums and modified to fit my use.
It works well, but only works intermittently. It is supposed to update the stock prices and dividend date according to the ticker symbols from Yahoo Finance.
Sometimes it updates all of the ticker symbols and displays "Stock data has been updated" and sometimes it updates none of the ticker symbols and still displays the same message. It appears to be random to me, with most of the time not updating the ticker symbols.
It used to work 100% of the time (with no code changes), but it stopped working, and someone suggested adding the delay of 0.5 seconds there which fixed the issue, and now, it has stopped working again (intermittently works).
Why?
Code:
(*
Retrieves stock quotes and other data from Yahoo! and places them in a Numbers table.
Place symbols in specified column starting at specified row number; no blanks; no footer row.
Retrieved data will be placed in columns immediately to the right of the symbols column.
By SGIII, 201403 v. 1.1
*)
--In the following line specify the types of data to retrieve in the order desired:
property quoteProperties : "l1r1"
(*
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:"Net_worth_2.numbers", targetSheet:"Dividends 2015-2016", targetTable:"Table 1", symbolsColNum:2, symbolsStartRow:3}
(*
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}
*)
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 (19), "+")
try
set the clipboard to my commaToTab(my getYData(symbConcat, quoteProperties))
set pasteStr to the clipboard
on error
return --halt script if error getting Yahoo data
end try
set the selection range to cell (t's symbolsStartRow) of column ((t's symbolsColNum) + 5)
tell application "Numbers" to activate
delay 0.5
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's text item delimiters} to {AppleScript's text 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's text item delimiters} to {AppleScript's text item delimiters, ","}
set strLst to text items of str
set AppleScript's text item delimiters to tab
set tsvStr to strLst as text
set AppleScript's text item delimiters to oTid
return tsvStr
on error
return
end try
end commaToTab
--end of script
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status:
Offline
|
|
|
|
|
|
|
|
|
|
|
Registered User
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|