I've found a script for downloading information about crypto currencies so that I can download into a Numbers spreadsheet using AppleScript. This is the script:
set mySheetName to "Coin Prices"
set myTableName to "Coin Prices"
set tgtCell to "A2"
set theHtml to do shell script "curl -s " & quoted form of "https://www.worldcoinindex.com"
set text item delimiters to {"<tbody>", "</tbody>"}
set tableContents to theHtml's text item 2 # item 2 is the body of the price table
set text item delimiters to {"<h2>"} # site uses new h2 for each currency
set tableChunks to tableContents's text items 2 thru -1
set pasteStr to ""
repeat with aChunk in tableChunks
set text item delimiters to "><span>$ </span><span class=\"span\">"
tell aChunk's text item 1 to set {theSymbol, thePrice} to {first word, last word}
set pasteStr to pasteStr & theSymbol & tab & thePrice & return
end repeat
set the clipboard to pasteStr
tell application "Numbers"
tell front document
tell sheet mySheetName to tell table myTableName
activate
set selection range to range tgtCell
delay 0.3
tell application "System Events" to keystroke "v" using {option down, shift down, command down}
end tell
end tell
end tell
It works perfectly until I set this line:
set theHtml to do shell script "curl -s " & quoted form of "https://www.worldcoinindex.com/watchlist"
I've checked the webpage code and it's exactly the same but I get a big long error box with something to do with Item 2. I won't copy and paste because the error box contains the entire source code for the webpage. The error reads like this though:
Can’t get text item 2 of
From there it's the source code.
Why does this script work on the base of the URL and not subdirectories of the URL?
Thanks for your help folks.
As I do not have an account to access
https://www.worldcoinindex.com/watchlist
and see it source code while logged in, I'll take your word that it has the<tbody>
and</tbody>
tags and offer you an alternate solution to usingcurl
.Assuming you are using Safari and are logged in at the target URL and the page is fully loaded, you can use the following example AppleScript code to get the data you seek.
Add the following to the top of your existing AppleScript script while commenting out the
set theHtml to do shell script ...
line of code.Note that
myTable
in the JavaScript command comes from the table on the main domain and may need to be adjusted for the Watchlist.Look at the page source for e.g:
You can also use e.g.:
Replacing the
...
as shown in the source code forclass=
Update:
Here is a version of example AppleScript code that will open a new Safari document to the target URL, and then dynamically create a Numbers document in the background, bring it frontmost once complete. No using
curl
or parsing HTML in a manner it really shouldn't be done in the first place. No clipboard or pasting into Numbers.Note that the Safari window can stay the background once its
id
has been ascertained, which only takes a moment once the window first appears, you can then set focus elsewhere while the script runs. The new Safari window is actually already in the background when it's created as Safari has not been told toactivate
.I created a login for the site and added the top three coins to my watchlist, and this screenshot is of the dynamically created Numbers document. It will do the same for the main URL as well if that's what you set
theURL
to in the example AppleScript code.Note that as currently coded, if not logged in it will notify you and abort the running of the script. I hope to update the code a bit later to handle not being logged in and dynamically logging in if not, but that is for the next iteration of the example AppleScript code.
Example AppleScript code:
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g.
delay 0.5
, with the value of the delay set appropriately.