我正在使用我的脚本检查文本文件,并根据其更改向我发送更新。
每次我在“重复”标签内使用“属性”标签时,它都会出现故障并给我错误:“预期”结束“但找到”属性“。”我可以使用“set”,但我不希望每次重复脚本时重置值。谢谢!
repeat
set theCEXRaw to read file "Macintosh HD:Users:adamvallorani:Desktop:CEX:CEXRaw"
set theGHS to paragraph 11 of theCEXRaw
set theBTC to paragraph 13 of theCEXRaw
set thePRICE to paragraph 26 of theCEXRaw
property oldBTC : 0
if theBTC > oldBTC then
set theBTC to oldBTC
set notif to "Your BTC has increased!"
display notification notif with title "CEXalert"
end if
set BuyGHS to "0.02356001"
set SellGHS to "0.02361999"
if thePRICE > SellGHS then
set notif2 to "Sell your GHS now!"
display notification notif2 with title "CEXalert"
end if
if thePRICE < BuyGHS then
set notif3 to "Buy some GHS now!"
display notification notif3 with title "CEXalert"
end if
delay 2
end repeat
“属性”行是错误 - 预期“结束”,但发现“属性” -
答案 0 :(得分:2)
属性语句是创建预初始化的全局变量的声明,必须出现在任何处理程序代码之外。如果将属性语句移动到脚本的顶部,AppleScript将接受它。出于举例的目的,您可以简单地说set oldBTC to 0
。
答案 1 :(得分:0)
<强>解决方案强>
乍一看,我认为你可能有一个范围问题,因为必须在重复循环之外移动属性语句。要成为有效的属性必须在重复循环之外和之前定义。但这并不完全正确。属性是在脚本执行后保留其状态的值,因此您需要考虑这是否是捕获值的适当方法(脚本运行之间将保留属性值)。使用如下行:
在repeat语句之外创建顶级变量更合适set variablename to 0 -- or some other value
这将强制每次脚本运行时重置变量,并提供正常的值。
如果您尝试保存脚本属性而不是变量,可以在Applescript Language Guide的以下页面上看到一些关于如何执行此操作的非常好的示例。另请注意它们在Applescript中的作用范围。
如果所有其他方法都失败了,您可以采用以下代码,这样您就可以在循环中记录变量:
set wordList to words in "Where is the hammer?"
repeat with currentWord in wordList
log currentWord
if contents of currentWord is equal to "hammer" then
display dialog "I found the hammer!"
end if
end repeat
请记住,变量必须最初在重复循环之外设置,然后在循环中根据需要进行更改。一种更简单的方法是使用重复循环中的值,将其分配给临时变量并记录结果,以确保获得所需的内容。