0

I have made an applescript that sets variable 'msshutdown' to yes and then shuts down the computer. Now I have another script exported as an application added as a login item that starts up the program 'MainStage' if 'msshutdown' is set to yes and afterwards sets 'msshutdown' to no. This is all because I want the computer to don't launch any apps at login, unless I shut it down using the first script. But it seems the second script can't find the variable 'msshutdown'. How do I make the second script read thew status of the variable in the first script and then edit it?

First script:

set msshutdown to yes
    tell application "Finder"
        shut down
    end tell

Second script:

if msshutdown is yes then
    tell application "MainStage 3"
        activate
    end tell
    set msshutdown to no
end if

2 Answers 2

1

Easiest solution is to write the variable to a file, then read it when needed. A simple text file will do the job.

First Script:

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

Second Script:

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
Sign up to request clarification or add additional context in comments.

Comments

0

Save the script below into the ~/Libraries/Script Libraries folder with the name 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 this from your scripts like I have modified them: First Script:

 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

Second Script:

 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.