AppleScript 库不会在每次加载时都计算 属性
AppleScript library doesn't evaluate property every time it's loaded
假设有两个脚本,first.scpt
do shell script "echo first > /tmp/shared_info.txt"
property lib : script "MyLibrary"
lib's display_shared_info()
和second.scpt
do shell script "echo second > /tmp/shared_info.txt"
property lib : script "MyLibrary"
lib's display_shared_info()
图书馆有以下代码:
property shared_info : read file (POSIX file "/tmp/shared_info.txt")
on display_shared_info()
display notification shared_info
end display_shared_info
我的推理是,当运行first.scpt然后second.scpt时,它会先显示"first"然后"second",因为每个脚本首先覆盖 shared_info.txt 然后调用库来显示它的内容。
但是,库似乎不会在每次将 shared_info 属性 加载到脚本中时对其求值?
我想要完成的是库每次加载到脚本中时都会初始化其属性。
AppleScript 属性 在编译时计算。
要在运行时设置它,请将代码放入处理程序
property shared_info : ""
on display_shared_info()
set shared_info to read "/tmp/shared_info.txt" -- works also with POSIX path
display notification shared_info
end display_shared_info
调用处理程序使用
do shell script "echo first > /tmp/shared_info.txt"
tell script "MyLibrary" to display_shared_info()
假设有两个脚本,first.scpt
do shell script "echo first > /tmp/shared_info.txt"
property lib : script "MyLibrary"
lib's display_shared_info()
和second.scpt
do shell script "echo second > /tmp/shared_info.txt"
property lib : script "MyLibrary"
lib's display_shared_info()
图书馆有以下代码:
property shared_info : read file (POSIX file "/tmp/shared_info.txt")
on display_shared_info()
display notification shared_info
end display_shared_info
我的推理是,当运行first.scpt然后second.scpt时,它会先显示"first"然后"second",因为每个脚本首先覆盖 shared_info.txt 然后调用库来显示它的内容。
但是,库似乎不会在每次将 shared_info 属性 加载到脚本中时对其求值?
我想要完成的是库每次加载到脚本中时都会初始化其属性。
AppleScript 属性 在编译时计算。 要在运行时设置它,请将代码放入处理程序
property shared_info : ""
on display_shared_info()
set shared_info to read "/tmp/shared_info.txt" -- works also with POSIX path
display notification shared_info
end display_shared_info
调用处理程序使用
do shell script "echo first > /tmp/shared_info.txt"
tell script "MyLibrary" to display_shared_info()