初学者 AppleScript Writer 在使用空闲处理程序时遇到问题
Beginner AppleScript Writer having trouble with idle handler
我最近一直在探索编码,我真的很喜欢解决问题。我现在对 AppleScript 感到很舒服,我认为它是我将来想用编码做的事情的一个很好的选择。我的直觉告诉我,Automator 在 RAM 方面的效率较低,而且我不喜欢它的划分方式;限制和混淆。我喜欢脚本语言的沙箱功能。我为网络爬虫构建了一个非常好的脚本,该脚本可以打开在线股票投资组合并计算 运行es 加密货币的市场价格。我计划利用技术决策实验室创建一个加密货币预测工作簿,以实现我的希望和梦想,如果有的话,有朝一日能赚钱 :[ 我有一个白日梦,即制作一个实时 excel 文件,该文件构建每小时波动的地块交易。
为了使其成为一个成熟的自动化系统,我需要某种方式来循环脚本或按计划将其安排到 运行 以获得我希望从中制定的数学模型的大量数据点数据。我已经真的 努力让空闲处理程序工作,但它并不像教程描述的那样运行。似乎你不能对某些命令使用“空闲”,而且每次我使用这个东西时我都会收到一个错误。我找到了一个帮助页面,该页面显示了如何合并“哔哔声”功能以确保空闲循环是 运行ning 并且当我编译并保存为“始终 运行ning 应用程序”时它不会播放哔哔声,所以我想这是我还没有弄清楚的另一个问题。有时我会发出哔哔声,但现在我的程序最终草稿无法正常工作。我曾尝试将它非常小心地插入到 tell 语句中,因为我发现它有时适用于它们。而且我猜你不能让空闲处理程序跨越整个脚本;它需要在一个命令结构树中调用才能工作。但是我仍然没有应用程序 运行 闲置的脚本以及我为研究此解决方案所做的所有工作。任何对空闲处理程序秘密保密的人都可以尽力向我解释脚本的内部工作原理,但我发现学习编码需要很长时间,因为它需要大量非常技术性的阅读很少有机会锻炼自己的学习能力。编码是很多样板代码的重复,我想我会在我白发苍苍的日子里用我到目前为止所学的知识慢慢编写代码。
但是,如果您可以使用这个问题收集一些阅读资料 material,了解如何在笔记本电脑的后台以 30 分钟的增量将编写得很好的脚本 运行计算负载相当好,将不胜感激。我不反对 Automator;它本身就很难,你必须知道所有的事情才能让它发挥作用。正如我所说,有关空闲处理程序以及如何使其工作的任何信息都会有所帮助。此外,如果可以在 AppleScript 中编写代码以在 Microsoft 中生成绘图 Excel,我喜欢为衬衫和谷歌制作模型。
我想我会分享我在最后一周的工作中所做的工作,这些工作值得研究目前在线免费提供的教程。非常感谢任何关于如何使我的脚本变得更好的批评或建议,如果我做得很好,我不介意你抢走你喜欢的东西。这是目前的网络爬虫加密货币股票分析器。它遵循3种货币,将数据写入一个excel文件,包含年月日秒,为更强大的数学模型收集海量数据。我研究了将季节性应用于数据的技术预测技术,因此预测比仅使用 excel 中的趋势线函数更好,尽管由于加密货币的可变性,我不会对市场价格的长期预测投入太多.我只是想关注那些令人痛心的股票崩盘,以便有机会用我可以凑齐的一点钱来维持生计。
--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, endTextDogeecoin)
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 endTextDogeecoin
set beginningToEndDogecoin to text item 1 of endItemsDogecoin
set AppleScript's text item delimiters to startTextDogecoin
set endTextDogeecoin 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 input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
return input
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 FinalFuction(BitcoinPrice)
set FinalFuction to extractTextBitcoin(BitcoinPrice, "<div class=>", "</div>")
return FinalFuction(BitcoinPrice)
end FinalFuction
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 token to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
return token
end getInputByClassLitecoin
getInputByClassLitecoin("priceValue___11gHJ", 0)
set LitecoinPrice to getInputByClassLitecoin("priceValue___11gHJ", 0)
on ReturnFuction(LitecoinPrice)
set ReturnFuction to extractTextLitecoin(LitecoinPrice, "<div class=>", "</div>")
return ReturnFuction(LitecoinPrice)
end ReturnFuction
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 blast to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
return blast
end getInputByClassDogecoin
getInputByClassDogecoin("priceValue___11gHJ", 0)
set DogecoinPrice to getInputByClassDogecoin("priceValue___11gHJ", 0)
on EndFuction(DogecoinPrice)
set EndFuction to extractTextDogecoin(DogecoinPrice, "<div class=>", "</div>")
return EndFuction(DogecoinPrice)
end EndFuction
--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--
tell application "Microsoft Excel"
open "/Users/clusterflux/Desktop/ㅇㅅㅇBITCOINㅇㅅㅇ.xlsx"
set display alerts to false
delete active sheet
first row index of (get end (last cell of column 9) direction toward the top)
set LastRow 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" & LastRow + 1) to "=YEAR(TODAY())"
set value of cell ("J" & LastRow + 1) to "=MONTH(TODAY())"
set value of cell ("K" & LastRow + 1) to "=DAY(TODAY())"
set value of cell ("L" & LastRow + 1) to (time string of (current date))
set value of cell ("M" & LastRow + 1) to BitcoinPrice
set workbookName to ("ㅇㅅㅇBITCOINㅇㅅㅇ.xlsx") as string
set destinationPath to (path to desktop as text) & workbookName
save active workbook in destinationPath
end tell
tell application "Microsoft Excel"
open "/Users/clusterflux/Desktop/ㅇㅅㅇLITECOINㅇㅅㅇ.xlsx"
set display alerts to false
delete active sheet
first row index of (get end (last cell of column 5) direction toward the top)
set LastRow to first row index of (get end (last cell of column 5) direction toward the top)
set value of cell ("C" & LastRow + 1) to "=YEAR(TODAY())"
set value of cell ("D" & LastRow + 1) to "=MONTH(TODAY())"
set value of cell ("E" & LastRow + 1) to "=DAY(TODAY())"
set value of cell ("F" & LastRow + 1) to (time string of (current date))
set value of cell ("G" & LastRow + 1) to LitecoinPrice
set workbookName to ("ㅇㅅㅇLITECOINㅇㅅㅇ.xlsx") as string
set destinationPath to (path to desktop as text) & workbookName
save active workbook in destinationPath
end tell
on idle
return 3
beep
tell application "Microsoft Excel"
open "/Users/clusterflux/Desktop/ㅇㅅㅇDOGECOINㅇㅅㅇ.xlsx"
set display alerts to false
delete active sheet
first row index of (get end (last cell of column 5) direction toward the top)
set LastRow to first row index of (get end (last cell of column 5) direction toward the top)
set value of cell ("C" & LastRow + 1) to "=YEAR(TODAY())"
set value of cell ("D" & LastRow + 1) to "=MONTH(TODAY())"
set value of cell ("E" & LastRow + 1) to "=DAY(TODAY())"
set value of cell ("F" & LastRow + 1) to (time string of (current date))
set value of cell ("G" & LastRow + 1) to DogecoinPrice
set workbookName to ("ㅇㅅㅇDOGECOINㅇㅅㅇ.xlsx") as string
set destinationPath to (path to desktop as text) & workbookName
save active workbook in destinationPath
end tell
end idle
如果我的格式不合适,请提前致歉。我还是新手
这是一种不同的 AppleScript 方法,它使您无需打开 Safari、使用 JavaScript、Automator 或使用文本项分隔符即可检索您的比特币价格值。这可能不是您正在寻找的东西,但至少它提供了一种使用更少代码的不同方法。希望您可以根据自己的需要调整其中的一些内容。
代码中的前 3 个属性定义了将在 do shell script
命令中使用的 regular expressions
,这将从 HTML 源代码中提取美元值。
例如,为了快速解释 property eGrepBitcoinPrice : "priceValue___11gHJ\”>\$\d{2},\d{3}.\d{2}”
的含义……我们将在 HTML 中搜索文本,其中包含“priceValue___11gHJ”,后跟“>”,然后是“ $”后跟任何 2 位数字,然后是“,”后跟任何 3 位数字后跟“。”然后是任意 2 位数字
因为我没有 Microsoft Excel,所以我无法在代码中包含这些命令。但是,我确实创建了一个快速记录功能,它将价格写入桌面“Price Log.txt”上的纯文本文件。可以很容易地禁用或删除此功能。日志命令全部包含在名为 script logCommands
的 script object
中,可以将其与代码中包含 my logCommands's
.
的任何其他行一起删除或注释掉
这是日志文件的快照
在 Script Editor.app 中将以下 AppleScript 代码保存为“保持打开”应用程序。由于它是一个“保持打开”的应用程序,当小程序在脚本 Editor.app 之外启动时,只有显式 on run
处理程序中的内容才会 运行 一次。其余的魔法发生在 on idle
处理程序中……并且此处理程序中的所有内容将每 300 秒 运行 一次。如果您希望命令每 30 分钟重复一次,只需将 return
值设置为 1800。
property eGrepBitcoinPrice : "priceValue___11gHJ\">\$\d{2},\d{3}.\d{2}"
property eGrepLitecoinPrice : "priceValue___11gHJ\">\$\d{3}.\d{2}"
property eGrepDogecoinPrice : "priceValue___11gHJ\">\$\d{1}.\d{5}"
property currentBitcoinPrice : missing value
property currentLitecoinPrice : missing value
property currentDogecoinPrice : missing value
property logToTextFile : missing value
on run -- Executed Only Once.. When This Script Applet Is Launched
activate
set logToTextFile to (display dialog ¬
"Enable Quick Log Mode?" buttons {"No", "Yes"} ¬
default button 2 with title "Log Mode")
if button returned of logToTextFile = "Yes" then
my logCommands's beginLog()
getPrices()
else
getPrices()
return {currentBitcoinPrice, currentDogecoinPrice, currentLitecoinPrice}
end if
end run
on idle
getPrices()
if button returned of logToTextFile = "Yes" then my logCommands's writeToLog()
(* within this idle handler is where you will place
The bulk of your additional code. All of your Excel
Code Goes Here*)
return 300 -- In Seconds, How Often To Run Code In This Idle Handler
end idle
---------- PLACE ALL ADDITIONAL HANDLERS BENEATH THIS LINE ----------
on getPrices()
set currentBitcoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/bitcoin/markets/' " & ¬
"| grep -Eo " & quoted form of eGrepBitcoinPrice & " | cut -c 21-"
set currentLitecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/litecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepLitecoinPrice & " | cut -c 21-"
set currentDogecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/dogecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepDogecoinPrice & " | cut -c 21-"
end getPrices
on quit -- Executed Only When The Script Quits
if button returned of logToTextFile = "Yes" then my logCommands's endLog()
continue quit -- Allows The Script To Quit
end quit
script logCommands
property pathToPriceLog : POSIX path of (path to desktop as text) & "Price Log.txt"
on beginLog()
set startTime to ("Start Time... " & (current date) as text) & ¬
" Price Scanning At 5 Minute Intervals"
do shell script "echo " & startTime & " >> " & ¬
quoted form of pathToPriceLog
end beginLog
on writeToLog()
do shell script "echo " & "Bitcoin:" & quoted form of currentBitcoinPrice & ¬
" Dogecoin:" & quoted form of currentDogecoinPrice & ¬
" Litecoin:" & quoted form of currentLitecoinPrice & ¬
" " & quoted form of (time string of (current date)) & ¬
" >> " & quoted form of pathToPriceLog
end writeToLog
on endLog()
set endTime to quoted form of "End Time... " & (current date) as text
do shell script "echo " & endTime & " >> " & ¬
quoted form of pathToPriceLog
do shell script "echo " & " " & " >> " & ¬
quoted form of pathToPriceLog
end endLog
end script
不幸的是,从脚本 Editor.app 中启动时“保持打开”的应用程序和脚本将不会执行 idle handler
中的内容。因此,“保持打开”应用程序需要从 Finder 中启动,就像任何其他应用程序一样,以观察 idle
命令的结果。这是我包含一个记录到文件功能的主要原因……所以我可以实时观察 idle
命令的结果。
与许多人的想法相反,大多数“保持开放”的应用程序使用的系统资源非常少。
由于更改 URL 源代码而更新了 APPLESCRIPT 代码
property eGrepBitcoinPrice : "priceValue\ \">\$\d{2},\d{3}.\d{2}"
property eGrepLitecoinPrice : "priceValue\ \">\$\d{3}.\d{2}"
property eGrepDogecoinPrice : "priceValue\ \">\$\d{1}.\d{4}"
property currentBitcoinPrice : missing value
property currentLitecoinPrice : missing value
property currentDogecoinPrice : missing value
property logToTextFile : missing value
on run -- Executed Only Once.. When This Script Applet Is Launched
activate
set logToTextFile to (display dialog ¬
"Enable Quick Log Mode?" buttons {"No", "Yes"} ¬
default button 2 with title "Log Mode")
if button returned of logToTextFile = "Yes" then
my logCommands's beginLog()
getPrices()
else
getPrices()
return {currentBitcoinPrice, currentDogecoinPrice, currentLitecoinPrice}
end if
end run
on idle
getPrices()
try
if button returned of logToTextFile = "Yes" then my logCommands's writeToLog()
on error errMsg number errNum
my logCommands's writeToLog()
end try
(* within this idle handler is where you will place
The bulk of your additional code. All of your Excel
Code Goes Here*)
return 300 -- In Seconds, How Often To Run Code In This Idle Handler
end idle
---------- PLACE ALL ADDITIONAL HANDLERS BENEATH THIS LINE ----------
on getPrices()
set currentBitcoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/bitcoin/markets/' " & ¬
"| grep -Eo " & quoted form of eGrepBitcoinPrice & " | cut -c 14-"
set currentLitecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/litecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepLitecoinPrice & " | cut -c 14-"
set currentDogecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/dogecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepDogecoinPrice & " | cut -c 14-"
end getPrices
on quit -- Executed Only When The Script Quits
if button returned of logToTextFile = "Yes" then my logCommands's endLog()
continue quit -- Allows The Script To Quit
end quit
script logCommands
property pathToPriceLog : POSIX path of (path to desktop as text) & "Price Log.txt"
on beginLog()
set startTime to ("Start Time... " & (current date) as text) & ¬
" Price Scanning At 5 Minute Intervals"
do shell script "echo " & startTime & " >> " & ¬
quoted form of pathToPriceLog
end beginLog
on writeToLog()
do shell script "echo " & "Bitcoin:" & quoted form of currentBitcoinPrice & ¬
" Dogecoin:" & quoted form of currentDogecoinPrice & ¬
" Litecoin:" & quoted form of currentLitecoinPrice & ¬
" " & quoted form of (time string of (current date)) & ¬
" >> " & quoted form of pathToPriceLog
end writeToLog
on endLog()
set endTime to quoted form of "End Time... " & (current date) as text
do shell script "echo " & endTime & " >> " & ¬
quoted form of pathToPriceLog
do shell script "echo " & " " & " >> " & ¬
quoted form of pathToPriceLog
end endLog
end script
我最近一直在探索编码,我真的很喜欢解决问题。我现在对 AppleScript 感到很舒服,我认为它是我将来想用编码做的事情的一个很好的选择。我的直觉告诉我,Automator 在 RAM 方面的效率较低,而且我不喜欢它的划分方式;限制和混淆。我喜欢脚本语言的沙箱功能。我为网络爬虫构建了一个非常好的脚本,该脚本可以打开在线股票投资组合并计算 运行es 加密货币的市场价格。我计划利用技术决策实验室创建一个加密货币预测工作簿,以实现我的希望和梦想,如果有的话,有朝一日能赚钱 :[ 我有一个白日梦,即制作一个实时 excel 文件,该文件构建每小时波动的地块交易。
为了使其成为一个成熟的自动化系统,我需要某种方式来循环脚本或按计划将其安排到 运行 以获得我希望从中制定的数学模型的大量数据点数据。我已经真的 努力让空闲处理程序工作,但它并不像教程描述的那样运行。似乎你不能对某些命令使用“空闲”,而且每次我使用这个东西时我都会收到一个错误。我找到了一个帮助页面,该页面显示了如何合并“哔哔声”功能以确保空闲循环是 运行ning 并且当我编译并保存为“始终 运行ning 应用程序”时它不会播放哔哔声,所以我想这是我还没有弄清楚的另一个问题。有时我会发出哔哔声,但现在我的程序最终草稿无法正常工作。我曾尝试将它非常小心地插入到 tell 语句中,因为我发现它有时适用于它们。而且我猜你不能让空闲处理程序跨越整个脚本;它需要在一个命令结构树中调用才能工作。但是我仍然没有应用程序 运行 闲置的脚本以及我为研究此解决方案所做的所有工作。任何对空闲处理程序秘密保密的人都可以尽力向我解释脚本的内部工作原理,但我发现学习编码需要很长时间,因为它需要大量非常技术性的阅读很少有机会锻炼自己的学习能力。编码是很多样板代码的重复,我想我会在我白发苍苍的日子里用我到目前为止所学的知识慢慢编写代码。
但是,如果您可以使用这个问题收集一些阅读资料 material,了解如何在笔记本电脑的后台以 30 分钟的增量将编写得很好的脚本 运行计算负载相当好,将不胜感激。我不反对 Automator;它本身就很难,你必须知道所有的事情才能让它发挥作用。正如我所说,有关空闲处理程序以及如何使其工作的任何信息都会有所帮助。此外,如果可以在 AppleScript 中编写代码以在 Microsoft 中生成绘图 Excel,我喜欢为衬衫和谷歌制作模型。
我想我会分享我在最后一周的工作中所做的工作,这些工作值得研究目前在线免费提供的教程。非常感谢任何关于如何使我的脚本变得更好的批评或建议,如果我做得很好,我不介意你抢走你喜欢的东西。这是目前的网络爬虫加密货币股票分析器。它遵循3种货币,将数据写入一个excel文件,包含年月日秒,为更强大的数学模型收集海量数据。我研究了将季节性应用于数据的技术预测技术,因此预测比仅使用 excel 中的趋势线函数更好,尽管由于加密货币的可变性,我不会对市场价格的长期预测投入太多.我只是想关注那些令人痛心的股票崩盘,以便有机会用我可以凑齐的一点钱来维持生计。
--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, endTextDogeecoin)
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 endTextDogeecoin
set beginningToEndDogecoin to text item 1 of endItemsDogecoin
set AppleScript's text item delimiters to startTextDogecoin
set endTextDogeecoin 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 input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
return input
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 FinalFuction(BitcoinPrice)
set FinalFuction to extractTextBitcoin(BitcoinPrice, "<div class=>", "</div>")
return FinalFuction(BitcoinPrice)
end FinalFuction
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 token to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
return token
end getInputByClassLitecoin
getInputByClassLitecoin("priceValue___11gHJ", 0)
set LitecoinPrice to getInputByClassLitecoin("priceValue___11gHJ", 0)
on ReturnFuction(LitecoinPrice)
set ReturnFuction to extractTextLitecoin(LitecoinPrice, "<div class=>", "</div>")
return ReturnFuction(LitecoinPrice)
end ReturnFuction
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 blast to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
return blast
end getInputByClassDogecoin
getInputByClassDogecoin("priceValue___11gHJ", 0)
set DogecoinPrice to getInputByClassDogecoin("priceValue___11gHJ", 0)
on EndFuction(DogecoinPrice)
set EndFuction to extractTextDogecoin(DogecoinPrice, "<div class=>", "</div>")
return EndFuction(DogecoinPrice)
end EndFuction
--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--
tell application "Microsoft Excel"
open "/Users/clusterflux/Desktop/ㅇㅅㅇBITCOINㅇㅅㅇ.xlsx"
set display alerts to false
delete active sheet
first row index of (get end (last cell of column 9) direction toward the top)
set LastRow 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" & LastRow + 1) to "=YEAR(TODAY())"
set value of cell ("J" & LastRow + 1) to "=MONTH(TODAY())"
set value of cell ("K" & LastRow + 1) to "=DAY(TODAY())"
set value of cell ("L" & LastRow + 1) to (time string of (current date))
set value of cell ("M" & LastRow + 1) to BitcoinPrice
set workbookName to ("ㅇㅅㅇBITCOINㅇㅅㅇ.xlsx") as string
set destinationPath to (path to desktop as text) & workbookName
save active workbook in destinationPath
end tell
tell application "Microsoft Excel"
open "/Users/clusterflux/Desktop/ㅇㅅㅇLITECOINㅇㅅㅇ.xlsx"
set display alerts to false
delete active sheet
first row index of (get end (last cell of column 5) direction toward the top)
set LastRow to first row index of (get end (last cell of column 5) direction toward the top)
set value of cell ("C" & LastRow + 1) to "=YEAR(TODAY())"
set value of cell ("D" & LastRow + 1) to "=MONTH(TODAY())"
set value of cell ("E" & LastRow + 1) to "=DAY(TODAY())"
set value of cell ("F" & LastRow + 1) to (time string of (current date))
set value of cell ("G" & LastRow + 1) to LitecoinPrice
set workbookName to ("ㅇㅅㅇLITECOINㅇㅅㅇ.xlsx") as string
set destinationPath to (path to desktop as text) & workbookName
save active workbook in destinationPath
end tell
on idle
return 3
beep
tell application "Microsoft Excel"
open "/Users/clusterflux/Desktop/ㅇㅅㅇDOGECOINㅇㅅㅇ.xlsx"
set display alerts to false
delete active sheet
first row index of (get end (last cell of column 5) direction toward the top)
set LastRow to first row index of (get end (last cell of column 5) direction toward the top)
set value of cell ("C" & LastRow + 1) to "=YEAR(TODAY())"
set value of cell ("D" & LastRow + 1) to "=MONTH(TODAY())"
set value of cell ("E" & LastRow + 1) to "=DAY(TODAY())"
set value of cell ("F" & LastRow + 1) to (time string of (current date))
set value of cell ("G" & LastRow + 1) to DogecoinPrice
set workbookName to ("ㅇㅅㅇDOGECOINㅇㅅㅇ.xlsx") as string
set destinationPath to (path to desktop as text) & workbookName
save active workbook in destinationPath
end tell
end idle
如果我的格式不合适,请提前致歉。我还是新手
这是一种不同的 AppleScript 方法,它使您无需打开 Safari、使用 JavaScript、Automator 或使用文本项分隔符即可检索您的比特币价格值。这可能不是您正在寻找的东西,但至少它提供了一种使用更少代码的不同方法。希望您可以根据自己的需要调整其中的一些内容。
代码中的前 3 个属性定义了将在 do shell script
命令中使用的 regular expressions
,这将从 HTML 源代码中提取美元值。
例如,为了快速解释 property eGrepBitcoinPrice : "priceValue___11gHJ\”>\$\d{2},\d{3}.\d{2}”
的含义……我们将在 HTML 中搜索文本,其中包含“priceValue___11gHJ”,后跟“>”,然后是“ $”后跟任何 2 位数字,然后是“,”后跟任何 3 位数字后跟“。”然后是任意 2 位数字
因为我没有 Microsoft Excel,所以我无法在代码中包含这些命令。但是,我确实创建了一个快速记录功能,它将价格写入桌面“Price Log.txt”上的纯文本文件。可以很容易地禁用或删除此功能。日志命令全部包含在名为 script logCommands
的 script object
中,可以将其与代码中包含 my logCommands's
.
这是日志文件的快照
在 Script Editor.app 中将以下 AppleScript 代码保存为“保持打开”应用程序。由于它是一个“保持打开”的应用程序,当小程序在脚本 Editor.app 之外启动时,只有显式 on run
处理程序中的内容才会 运行 一次。其余的魔法发生在 on idle
处理程序中……并且此处理程序中的所有内容将每 300 秒 运行 一次。如果您希望命令每 30 分钟重复一次,只需将 return
值设置为 1800。
property eGrepBitcoinPrice : "priceValue___11gHJ\">\$\d{2},\d{3}.\d{2}"
property eGrepLitecoinPrice : "priceValue___11gHJ\">\$\d{3}.\d{2}"
property eGrepDogecoinPrice : "priceValue___11gHJ\">\$\d{1}.\d{5}"
property currentBitcoinPrice : missing value
property currentLitecoinPrice : missing value
property currentDogecoinPrice : missing value
property logToTextFile : missing value
on run -- Executed Only Once.. When This Script Applet Is Launched
activate
set logToTextFile to (display dialog ¬
"Enable Quick Log Mode?" buttons {"No", "Yes"} ¬
default button 2 with title "Log Mode")
if button returned of logToTextFile = "Yes" then
my logCommands's beginLog()
getPrices()
else
getPrices()
return {currentBitcoinPrice, currentDogecoinPrice, currentLitecoinPrice}
end if
end run
on idle
getPrices()
if button returned of logToTextFile = "Yes" then my logCommands's writeToLog()
(* within this idle handler is where you will place
The bulk of your additional code. All of your Excel
Code Goes Here*)
return 300 -- In Seconds, How Often To Run Code In This Idle Handler
end idle
---------- PLACE ALL ADDITIONAL HANDLERS BENEATH THIS LINE ----------
on getPrices()
set currentBitcoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/bitcoin/markets/' " & ¬
"| grep -Eo " & quoted form of eGrepBitcoinPrice & " | cut -c 21-"
set currentLitecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/litecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepLitecoinPrice & " | cut -c 21-"
set currentDogecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/dogecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepDogecoinPrice & " | cut -c 21-"
end getPrices
on quit -- Executed Only When The Script Quits
if button returned of logToTextFile = "Yes" then my logCommands's endLog()
continue quit -- Allows The Script To Quit
end quit
script logCommands
property pathToPriceLog : POSIX path of (path to desktop as text) & "Price Log.txt"
on beginLog()
set startTime to ("Start Time... " & (current date) as text) & ¬
" Price Scanning At 5 Minute Intervals"
do shell script "echo " & startTime & " >> " & ¬
quoted form of pathToPriceLog
end beginLog
on writeToLog()
do shell script "echo " & "Bitcoin:" & quoted form of currentBitcoinPrice & ¬
" Dogecoin:" & quoted form of currentDogecoinPrice & ¬
" Litecoin:" & quoted form of currentLitecoinPrice & ¬
" " & quoted form of (time string of (current date)) & ¬
" >> " & quoted form of pathToPriceLog
end writeToLog
on endLog()
set endTime to quoted form of "End Time... " & (current date) as text
do shell script "echo " & endTime & " >> " & ¬
quoted form of pathToPriceLog
do shell script "echo " & " " & " >> " & ¬
quoted form of pathToPriceLog
end endLog
end script
不幸的是,从脚本 Editor.app 中启动时“保持打开”的应用程序和脚本将不会执行 idle handler
中的内容。因此,“保持打开”应用程序需要从 Finder 中启动,就像任何其他应用程序一样,以观察 idle
命令的结果。这是我包含一个记录到文件功能的主要原因……所以我可以实时观察 idle
命令的结果。
与许多人的想法相反,大多数“保持开放”的应用程序使用的系统资源非常少。
由于更改 URL 源代码而更新了 APPLESCRIPT 代码
property eGrepBitcoinPrice : "priceValue\ \">\$\d{2},\d{3}.\d{2}"
property eGrepLitecoinPrice : "priceValue\ \">\$\d{3}.\d{2}"
property eGrepDogecoinPrice : "priceValue\ \">\$\d{1}.\d{4}"
property currentBitcoinPrice : missing value
property currentLitecoinPrice : missing value
property currentDogecoinPrice : missing value
property logToTextFile : missing value
on run -- Executed Only Once.. When This Script Applet Is Launched
activate
set logToTextFile to (display dialog ¬
"Enable Quick Log Mode?" buttons {"No", "Yes"} ¬
default button 2 with title "Log Mode")
if button returned of logToTextFile = "Yes" then
my logCommands's beginLog()
getPrices()
else
getPrices()
return {currentBitcoinPrice, currentDogecoinPrice, currentLitecoinPrice}
end if
end run
on idle
getPrices()
try
if button returned of logToTextFile = "Yes" then my logCommands's writeToLog()
on error errMsg number errNum
my logCommands's writeToLog()
end try
(* within this idle handler is where you will place
The bulk of your additional code. All of your Excel
Code Goes Here*)
return 300 -- In Seconds, How Often To Run Code In This Idle Handler
end idle
---------- PLACE ALL ADDITIONAL HANDLERS BENEATH THIS LINE ----------
on getPrices()
set currentBitcoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/bitcoin/markets/' " & ¬
"| grep -Eo " & quoted form of eGrepBitcoinPrice & " | cut -c 14-"
set currentLitecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/litecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepLitecoinPrice & " | cut -c 14-"
set currentDogecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/dogecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepDogecoinPrice & " | cut -c 14-"
end getPrices
on quit -- Executed Only When The Script Quits
if button returned of logToTextFile = "Yes" then my logCommands's endLog()
continue quit -- Allows The Script To Quit
end quit
script logCommands
property pathToPriceLog : POSIX path of (path to desktop as text) & "Price Log.txt"
on beginLog()
set startTime to ("Start Time... " & (current date) as text) & ¬
" Price Scanning At 5 Minute Intervals"
do shell script "echo " & startTime & " >> " & ¬
quoted form of pathToPriceLog
end beginLog
on writeToLog()
do shell script "echo " & "Bitcoin:" & quoted form of currentBitcoinPrice & ¬
" Dogecoin:" & quoted form of currentDogecoinPrice & ¬
" Litecoin:" & quoted form of currentLitecoinPrice & ¬
" " & quoted form of (time string of (current date)) & ¬
" >> " & quoted form of pathToPriceLog
end writeToLog
on endLog()
set endTime to quoted form of "End Time... " & (current date) as text
do shell script "echo " & endTime & " >> " & ¬
quoted form of pathToPriceLog
do shell script "echo " & " " & " >> " & ¬
quoted form of pathToPriceLog
end endLog
end script