Applescript变量在其他脚本中不可用

时间:2015-03-09 15:37:31

标签: variables applescript

我制作了一个可以设置变量' msshutdown'是,然后关闭计算机。现在我将另一个脚本导出为一个应用程序,作为登录项添加,启动程序' MainStage'如果' msshutdown'设置为是,然后设置' msshutdown'没有。 这都是因为我希望计算机在登录时不启动任何应用程序,除非我使用第一个脚本关闭它。 但似乎第二个脚本无法找到变量' 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

2 个答案:

答案 0 :(得分:0)

最简单的解决方案是将变量写入文件,然后在需要时读取它。一个简单的文本文件就可以完成这项工作。

第一个脚本:

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

答案 1 :(得分:0)

将下面的脚本保存到名称为shutdownStore

的〜/ Libraries / Script Libraries文件夹中
 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
相关问题