macOS Big Sur:用于打开网页、阅读文本和写入 excel 文件的 AppleScript

macOS Big Sur: AppleScript to open a webpage, read text, and write to an excel file

我开始使用 Automator 运行 一个脚本来打开网页、读取文本并写入 excel 但我 运行 遇到错误并在此处发布了一个问题:

macOS Big Sur: How to use automator (or something else) to build a script for collecting data over time

现在我找到了如何用 AppleScript 做类似的事情,我的代码如下:

to goToWebPage(theWebPage)
tell application "Safari"
    activate
    set URL of document 1 to theWebPage
end tell
end goToWebPage

goToWebPage("https://coinmarketcap.com/currencies/bitcoin/markets/")

to getInputByClass(theClass, num) -- defines a function with two inputs, theClass and num
tell application "Safari" --tells AS that we are going to use Safari    
    set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1 -        -   uses JavaScript to set the variable input to the information we want
end tell

return input --tells the function to return the value of the variable input
end getInputByClass

getInputByClass("r", 0)

脚本打开网页,但随后 returns 出现错误“未定义变量输入”。为什么脚本没有定义输入?我是否在使用多年前在网上找到的样板示例中的错误代码?我想打开网页和 select 一个每分钟左右更改一次的文本值,并将其写入 excel 文件。任何帮助表示赞赏。我是 AppleScript 和 Automator 的新手运行。

######################## 编辑: 感谢您的反馈和修改。

这是一个非常简洁的解决方案,谢谢。但我不知道要花多长时间才能理解它的作用。它还需要写入 Excel 工作簿,但一旦我有时间研究它使用的高级功能,这是制作精简版本的好方法。尽管到目前为止我最喜欢 AppleScript 而不是其他语言,因为它使用更基本的英语命令,可以通过逐步阅读代码的作用来理解这些命令,因此我可以比 shorthand 代码更难解决错误跟踪并且无法阅读。不过我可以借这个。

这是我工作的更新版本。我添加了更多的股票,我不介意它使用像 Safari 和 JavaScript 这样的计算机应用程序,尽管我希望我可以避免使用 Automator 并弄清楚如何在后台获取它并计算 运行而我在 excel 数据操作和代码 writing/learning 之间徘徊。我正在考虑将其保存为 Applet 或应用程序并使用空闲处理程序。您提供的简短解决方案实际上可能适用于空闲处理程序。我的没有,因为我认为它的功能太多了。我买了这台电脑并下载了程序来使用它们,所以如果我牺牲一小部分计算能力来自动和连续地 运行 处理事情,我不会感到困扰。这是我的代码。抱歉,如果它没有按照您的喜好组织,但我正在学习并且代码对我来说很有意义来解决和编写它所以我不介意其他人是否胡言乱语。除了我在其他论坛上处理的 Excel 月条目外,这有效。如果有人知道为什么当 AppleScript 将其写入 Excel 时 excel returns 在 ("=TEXT(TODAY(), MMMM)") 中间轻拍一个 @ 符号,我将不胜感激解决我目前的进度。

--Boiler plate code to manipulate the HTML to let us pull the market price of the stock.--
--3 sets of modifiers for the 3 stocks--
#########################################################
to extractTextBitcoin(searchTextBitcoin, startTextBitcoin, endTextBitcoin)
    set tid to AppleScript's text item delimiters
    set startTextBitcoin to ">"
    set searchTextBitcoin to {"priceValue___11gHJ", 0 & searchTextBitcoin}
    set AppleScript's text item delimiters to startTextBitcoin
    set endItemsBitcoin to text item -1 of searchTextBitcoin
    set AppleScript's text item delimiters to endTextBitcoin
    set beginningToEndBitcoin to text item 1 of endItemsBitcoin
    set AppleScript's text item delimiters to startTextBitcoin
    set endTextBitcoin to (text items 2 thru -1 of beginningToEndBitcoin) as record
    set AppleScript's text item delimiters to tid
end extractTextBitcoin

to extractTextLitecoin(searchTextLitecoin, startTextLitecoin, endTextLitecoin)
    set tid to AppleScript's text item delimiters
    set startTextLitecoin to ">"
    set searchTextLitecoin to {"priceValue___11gHJ", 0 & searchTextLitecoin}
    set AppleScript's text item delimiters to startTextLitecoin
    set endItemsLitecoin to text item -1 of searchTextLitecoin
    set AppleScript's text item delimiters to endTextLitecoin
    set beginningToEndLitecoin to text item 1 of endItemsLitecoin
    set AppleScript's text item delimiters to startTextLitecoin
    set endTextLitecoin to (text items 2 thru -1 of beginningToEndLitecoin) as record
    set AppleScript's text item delimiters to tid
end extractTextLitecoin

to extractTextDogecoin(searchTextDogecoin, startTextDogecoin, endTextDogecoin)
    set tid to AppleScript's text item delimiters
    set startTextDogecoin to ">"
    set searchTextDogecoin to {"priceValue___11gHJ", 0 & searchTextDogecoin}
    set AppleScript's text item delimiters to startTextDogecoin
    set endItemsDogecoin to text item -2 of searchTextDogecoin
    set AppleScript's text item delimiters to endTextDogecoin
    set beginningToEndDogecoin to text item 1 of endItemsDogecoin
    set AppleScript's text item delimiters to startTextDogecoin
    set endTextDogecoin to (text items 2 thru -1 of beginningToEndDogecoin) as record
    set AppleScript's text item delimiters to tid
end extractTextDogecoin
#########################################################
--A tell statement to open the webpage where the stocks are measured--
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/bitcoin/"
end tell

delay 2

--A function that differentiates the data on the web page by class and number. It
--also uses JavaScript to write the data to a useable format.
to getInputByClassBitcoin(theClass, num)
    tell application "Safari"
        set BitcoinInput to do JavaScript "
        document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
    end tell
    return BitcoinInput
end getInputByClassBitcoin

--The function with the class and number criteria manually pulled from the web page--
getInputByClassBitcoin("priceValue___11gHJ", 0)

--Setting the instataneous stock price to a variable to input in Excel--
set BitcoinPrice to getInputByClassBitcoin("priceValue___11gHJ", 0)
on BitcoinFunction(BitcoinPrice)
    set BitcoinFunction to extractTextBitcoin(BitcoinPrice, "<div class=>", "</div>")
    return BitcoinFunction(BitcoinPrice)
end BitcoinFunction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/litecoin/"
end tell

delay 2

to getInputByClassLitecoin(theClass, num)
    tell application "Safari"
        set LitecoinInput to do JavaScript "
        document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
    end tell
    return LitecoinInput
end getInputByClassLitecoin

getInputByClassLitecoin("priceValue___11gHJ", 0)

set LitecoinPrice to getInputByClassLitecoin("priceValue___11gHJ", 0)
on LitecoinFuction(LitecoinPrice)
    set LitecoinFuction to extractTextLitecoin(LitecoinPrice, "<div class=>", "</div>")
    return LitecoinFuction(LitecoinPrice)
end LitecoinFuction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/dogecoin/"
end tell

delay 2

to getInputByClassDogecoin(theClass, num)
    tell application "Safari"
        set DogecoinInput to do JavaScript "
        document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
    end tell
    return DogecoinInput
end getInputByClassDogecoin

getInputByClassDogecoin("priceValue___11gHJ", 0)

set DogecoinPrice to getInputByClassDogecoin("priceValue___11gHJ", 0)
on DogecoinFuction(DogecoinPrice)
    set DogecoinFuction to extractTextDogecoin(DogecoinPrice, "<div class=>", "</div>")
    return DogecoinFuction(DogecoinPrice)
end DogecoinFuction
(*
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/Ethereum/"
end tell

delay 2

to getInputByClassEthereum(theClass, num)
    tell application "Safari"
        set EthereumInput to do JavaScript "
        document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
    end tell
    return EthereumInput
end getInputByClassEthereum

getInputByClassEthereum("priceValue___11gHJ", 0)

set EthereumPrice to getInputByClassEthereum("priceValue___11gHJ", 0)
on EthereumFunction(EthereumPrice)
    set EthereumFunction to extractTextEthereum(EthereumPrice, "<div class=>", "</div>")
    return EthereumFunction(EthereumPrice)
end EthereumFunction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/Binance_coin/"
end tell

delay 2

to getInputByClassBinance_coin(theClass, num)
    tell application "Safari"
        set Binance_coinInput to do JavaScript "
        document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
    end tell
    return Binance_coinInput
end getInputByClassBinance_coin

getInputByClassBinance_coin("priceValue___11gHJ", 0)

set Binance_coinPrice to getInputByClassBinance_coin("priceValue___11gHJ", 0)
on Binance_coinFunction(Binance_coinPrice)
    set Binance_coinFunction to extractTextBinance_coin(Binance - coinPrice, "<div class=>", "</div>")
    return Binance_coinFunction(Binance_coinPrice)
end Binance_coinFunction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/Tether/"
end tell

delay 2

to getInputByClassTether(theClass, num)
    tell application "Safari"
        set TetherInput to do JavaScript "
        document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
    end tell
    return TetherInput
end getInputByClassTether

getInputByClassTether("priceValue___11gHJ", 0)

set TetherPrice to getInputByClassTether("priceValue___11gHJ", 0)
on TetherFunction(TetherPrice)
    set TetherFunction to extractTextTether(TetherPrice, "<div class=>", "</div>")
    return TetherFunction(TetherPrice)
end TetherFunction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/Polkadot/"
end tell

delay 2

to getInputByClassPolkadot(theClass, num)
    tell application "Safari"
        set PolkadotInput to do JavaScript "
        document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
    end tell
    return PolkadotInput
end getInputByClassPolkadot

getInputByClassPolkadot("priceValue___11gHJ", 0)

set PolkadotPrice to getInputByClassPolkadot("priceValue___11gHJ", 0)
on PolkadotFunction(PolkadotPrice)
    set PolkadotFunction to extractTextPolkadot(PolkadotPrice, "<div class=>", "</div>")
    return PolkadotFunction(PolkadotPrice)
end PolkadotFunction
#########################################################
*)
--Opens the compiled Excel workbook, negates user input, finds the next available--
--cell to input data, and fills the fields with Year, Month, Day, Time, and Price--
set {year:y, day:d} to (current date)
tell application "Microsoft Excel"
    open "/Users/clusterflux/Desktop/人CRYPTO人_excel.xlsx"
    set display alerts to false
    activate object sheet "ㅇㅅㅇBITCOINㅇㅅㅇ"
    first row index of (get end (last cell of column 9) direction toward the top)
    set LastRowBitcoin to first row index of (get end (last cell of column 9) direction toward the top)
    --write date and time for each market reading to excel file
    set value of cell ("I" & LastRowBitcoin + 1) to y
    set value of cell ("J" & LastRowBitcoin + 1) to ("=TEXT(TODAY(), MMMM)")
    set value of cell ("K" & LastRowBitcoin + 1) to d
    set value of cell ("L" & LastRowBitcoin + 1) to (time string of (current date))
    set value of cell ("M" & LastRowBitcoin + 1) to BitcoinPrice
    activate object sheet "ㅇㅅㅇLITECOINㅇㅅㅇ"
    first row index of (get end (last cell of column 9) direction toward the top)
    set LastRowLitecoin to first row index of (get end (last cell of column 9) direction toward the top)
    set value of cell ("I" & LastRowLitecoin + 1) to y
    set value of cell ("J" & LastRowLitecoin + 1) to ("=TEXT(TODAY(), MMMM)")
    set value of cell ("K" & LastRowLitecoin + 1) to d
    set value of cell ("L" & LastRowLitecoin + 1) to (time string of (current date))
    set value of cell ("M" & LastRowLitecoin + 1) to LitecoinPrice
    activate object sheet "ㅇㅅㅇDOGECOINㅇㅅㅇ"
    first row index of (get end (last cell of column 9) direction toward the top)
    set LastRowDogecoin to first row index of (get end (last cell of column 9) direction toward the top)
    set value of cell ("I" & LastRowDogecoin + 1) to y
    set value of cell ("J" & LastRowDogecoin + 1) to ("=TEXT(TODAY(),  MMMM)")
    set value of cell ("K" & LastRowDogecoin + 1) to d
    set value of cell ("L" & LastRowDogecoin + 1) to (time string of (current date))
    set value of cell ("M" & LastRowDogecoin + 1) to DogecoinPrice
    set workbookName to ("人CRYPTO人_excel.xlsx") as string
    set destinationPath to (path to desktop as text) & workbookName
    save active workbook in destinationPath
    delay 2
    --close every workbook saving no--
    --tell application "Microsoft Excel"--
    --quit--
end tell

set closeURLs to {"https://coinmarketcap.com/currencies/bitcoin/", "https://coinmarketcap.com/currencies/litecoin/", "https://coinmarketcap.com/currencies/dogecoin/"}

repeat with theURL in closeURLs
    tell application "Safari" to close (every tab of every window whose URL contains (contents of theURL))
end repeat

我在此解决方案中提供的 AppleScript 代码取自我在您的其他主题中发布的深入解决方案

初级 AppleScript Writer 在空闲处理程序方面遇到问题

这是一种不同的 AppleScript 方法,它使您无需打开 Safari、使用 JavaScript、Automator 或使用文本项分隔符即可检索您的比特币价格值。

property eGrepBitcoinPrice : "priceValue___11gHJ\">\$\d{2},\d{3}.\d{2}"
property currentBitcoinPrice : missing value

set currentBitcoinPrice to do shell script ¬
    "curl --no-keepalive 'https://coinmarketcap.com/currencies/bitcoin/markets/' " & ¬
    "| grep -Eo " & quoted form of eGrepBitcoinPrice & " | cut -c 21-"