首页 > 解决方案 > 初学者 AppleScript Writer 遇到空闲处理程序问题

问题描述

我最近一直在探索编码,我真的很喜欢解决问题。我现在对 AppleScript 感到很舒服,我认为这是我将来想通过编码做的一个不错的选择。我的直觉告诉我,Automator 在 RAM 方面的效率会更低,我不喜欢它的分割方式;限制和混淆。我喜欢脚本语言的沙盒功能。我为网络爬虫构建了一个非常好的脚本,它可以打开在线股票投资组合并修剪加密货币的市场价格。我计划利用技术决策实验室来创建一个加密货币预测工作簿,以实现我的希望和梦想,如果有一天能赚钱:[我梦想制作一个实时 excel 文件,该文件可以绘制每小时交易波动的图表。

为了使它成为一个成熟的自动化系统,我需要某种方法来循环脚本或安排它按计划运行,以获得我希望从数据中制定的数学模型的大量数据点。我真的试过很难让空闲处理程序工作,但它不像教程描述的那样运行。似乎您不能在某些命令中使用“空闲”,并且每次使用该东西时都会出错。我找到了一个帮助页面,该页面显示了如何合并“哔”功能以确保空闲循环正在运行,当我编译并保存为“始终运行的应用程序”时,它不会播放哔声,所以我想这是我的另一个问题还没想通。我有时会发出哔哔声,但现在我的程序的最终草稿我无法让它工作。我曾尝试将它非常小心地插入到tell语句中,因为我发现它有时可以与它们一起使用。而且我猜你不能让空闲处理程序跨越整个脚本;它需要在一个命令结构树中调用才能工作。但是我仍然没有让应用程序从空闲状态运行脚本,我在研究这个解决方案时所做的所有工作。任何对空闲处理程序秘密保密的人都可以尽力向我解释脚本的内部工作原理,但我发现学习编码需要很长时间,因为这是很多非常技术性的阅读很少有机会锻炼自己的学习能力。编码是很多样板的重复,我想我会用我到目前为止所学到的知识在我白发苍苍的日子里慢慢地编写代码。任何对空闲处理程序秘密保密的人都可以尽力向我解释脚本的内部工作原理,但我发现我需要很长时间来学习编码,因为这是很多非常技术性的阅读很少有机会锻炼自己的学习能力。编码是很多样板的重复,我想我会用我到目前为止所学到的知识在我白发苍苍的日子里慢慢地编写代码。任何对空闲处理程序秘密保密的人都可以尽力向我解释脚本的内部工作原理,但我发现学习编码需要很长时间,因为这是很多非常技术性的阅读很少有机会锻炼自己的学习能力。编码是很多样板的重复,我想我会用我到目前为止所学到的知识在我白发苍苍的日子里慢慢地编写代码。

但是,如果您可以使用这个问题来收集一些阅读材料,了解如何在笔记本电脑的背景下以 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

如果我的格式不符合要求,请提前道歉。我还是个新手。

标签: excelloopsapplescriptmodelingmacos-big-sur

解决方案



推荐阅读