Applescript 变量在其他脚本中不可用
Applescript variable not usable in other script
我制作了一个将变量 'msshutdown' 设置为 yes 然后关闭计算机的 applescript。现在我将另一个脚本作为应用程序导出并添加为登录项,如果 'msshutdown' 设置为是,然后将 'msshutdown' 设置为否,则启动程序 'MainStage'。
这都是因为我希望计算机在登录时不启动任何应用程序,除非我使用第一个脚本将其关闭。
但似乎第二个脚本找不到变量'msshutdown'。如何让第二个脚本读取第一个脚本中变量的状态,然后对其进行编辑?
第一个脚本:
set msshutdown to yes
tell application "Finder"
shut down
end tell
第二个脚本:
if msshutdown is yes then
tell application "MainStage 3"
activate
end tell
set msshutdown to no
end if
最简单的解决方案是将变量写入文件,然后在需要时读取它。一个简单的文本文件就可以完成这项工作。
第一个脚本:
writeVar("yes")
tell application "Finder"
shut down
end tell
on writeVar(theVar)
do shell script "echo " & quoted form of (theVar as text) & " > ~/varFile.txt"
end writeVar
第二个脚本:
if readVar() is "yes" then
tell application "MainStage 3"
activate
end tell
writeVar("no")
end if
on writeVar(theVar)
do shell script "echo " & quoted form of theVar & " > ~/varFile.txt"
end writeVar
on readVar()
do shell script "cat ~/varFile.txt"
end readVar
将下面的脚本保存到名为 shutdownStore
的 ~/Libraries/Script 库文件夹中
use AppleScript version "2.3"
use scripting additions
property shutDownCacheName : "shutdownStore"
property shutdownCache : missing value
to saveShutDownStatus(theShutDownStatus)
set cachePath to ((path to library folder from user domain as text) & "caches:" & "net.mcusr." & my shutDownCacheName)
set shutdown of my shutdownCache to theShutDownStatus
store script my shutdownCache in cachePath replacing yes
end saveShutDownStatus
on loadShutDownStatusFromScriptCache()
set cachePath to ((path to library folder from user domain as text) & "caches:" & "net.mcusr." & my shutDownCacheName)
local script_cache
try
set my shutdownCache to load script alias cachePath
on error
script newScriptCache
property shutdown : false
end script
set my shutdownCache to newScriptCache
end try
return shutdown of my shutdownCache
end loadShutDownStatusFromScriptCache
on getShutDownStatus()
set last_shutDownStatus to loadShutDownStatusFromScriptCache()
return last_shutDownStatus
end getShutDownStatus
从你的脚本中使用它,就像我修改它们一样:
第一个脚本:
use AppleScript version "2.3"
use scripting additions
use mss : script "shutdownStore"
set msshutdown to yes
saveShutDownStatus(msshutdown) of mss
tell application "Finder"
shut down
end tell
第二个脚本:
use AppleScript version "2.3"
use scripting additions
use mss : script "shutdownStore"
set msshutdown to getShutDownStatus() of mss
if msshutdown is yes then
tell application "MainStage 3"
activate
end tell
set msshutdown to no
saveShutDownStatus(msshutdown) of mss
end if
我制作了一个将变量 'msshutdown' 设置为 yes 然后关闭计算机的 applescript。现在我将另一个脚本作为应用程序导出并添加为登录项,如果 'msshutdown' 设置为是,然后将 'msshutdown' 设置为否,则启动程序 'MainStage'。 这都是因为我希望计算机在登录时不启动任何应用程序,除非我使用第一个脚本将其关闭。 但似乎第二个脚本找不到变量'msshutdown'。如何让第二个脚本读取第一个脚本中变量的状态,然后对其进行编辑?
第一个脚本:
set msshutdown to yes
tell application "Finder"
shut down
end tell
第二个脚本:
if msshutdown is yes then
tell application "MainStage 3"
activate
end tell
set msshutdown to no
end if
最简单的解决方案是将变量写入文件,然后在需要时读取它。一个简单的文本文件就可以完成这项工作。
第一个脚本:
writeVar("yes")
tell application "Finder"
shut down
end tell
on writeVar(theVar)
do shell script "echo " & quoted form of (theVar as text) & " > ~/varFile.txt"
end writeVar
第二个脚本:
if readVar() is "yes" then
tell application "MainStage 3"
activate
end tell
writeVar("no")
end if
on writeVar(theVar)
do shell script "echo " & quoted form of theVar & " > ~/varFile.txt"
end writeVar
on readVar()
do shell script "cat ~/varFile.txt"
end readVar
将下面的脚本保存到名为 shutdownStore
use AppleScript version "2.3"
use scripting additions
property shutDownCacheName : "shutdownStore"
property shutdownCache : missing value
to saveShutDownStatus(theShutDownStatus)
set cachePath to ((path to library folder from user domain as text) & "caches:" & "net.mcusr." & my shutDownCacheName)
set shutdown of my shutdownCache to theShutDownStatus
store script my shutdownCache in cachePath replacing yes
end saveShutDownStatus
on loadShutDownStatusFromScriptCache()
set cachePath to ((path to library folder from user domain as text) & "caches:" & "net.mcusr." & my shutDownCacheName)
local script_cache
try
set my shutdownCache to load script alias cachePath
on error
script newScriptCache
property shutdown : false
end script
set my shutdownCache to newScriptCache
end try
return shutdown of my shutdownCache
end loadShutDownStatusFromScriptCache
on getShutDownStatus()
set last_shutDownStatus to loadShutDownStatusFromScriptCache()
return last_shutDownStatus
end getShutDownStatus
从你的脚本中使用它,就像我修改它们一样: 第一个脚本:
use AppleScript version "2.3"
use scripting additions
use mss : script "shutdownStore"
set msshutdown to yes
saveShutDownStatus(msshutdown) of mss
tell application "Finder"
shut down
end tell
第二个脚本:
use AppleScript version "2.3"
use scripting additions
use mss : script "shutdownStore"
set msshutdown to getShutDownStatus() of mss
if msshutdown is yes then
tell application "MainStage 3"
activate
end tell
set msshutdown to no
saveShutDownStatus(msshutdown) of mss
end if