Let's say I have an Applescript that does THISCODE. And I have another Applescript that does THATCODE. I always run them in sequence, but never at regular intervals. In other words, whichever one I ran last time, I'll always run the other the next time.

Is there a way to combine the two Applescripts into one using a simple toggle? For example, at the end of the Applescript, could I write a variable into the beginning of the script that'll be read the next time, such as EVENvariable or ODDvariable... so that, when the Applescript runs, it'll know which state it was in the last time it ran. Or is there an even more simple and obvious solution I'm missing?


You can do it by setting your own defaults parameter, to store the value between uses/reboots etc, otherwise an Applescript will not remember your current toggled state.

This is a quick example that will set a user-defined parameter [replace with actual company &/or product name & memorable parameter name if you have one, otherwise the example data will do fine] then will just say out loud, 'yes' or 'no' depending on the state of the defaults parameter switch.

You can run it from the open script or save it as an application.

set newTestParameter to "YES"
    set oldTestParameter to do shell script "defaults read com.myCo.myApp myParamSet"
    if oldTestParameter is in {"1", "YES"} then
        set newTestParameter to "NO"
    end if
end try
do shell script "defaults write com.myCo.myApp myParamSet " & newTestParameter

--your actual code, dependant on above switch
if newTestParameter is in {"1", "YES"} then
    say "Yes"
    -- run thisCode
    say "No"
    -- run thatCode
end if

On first run it will write a new file to ~/Library/Preferences called com.myCo.myApp.plist & will contain one parameter, with the current state of your toggle, like this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

You don't need to do anything to 'maintain' this on your own, you can simply keep sending its new value with each script run.

  • Thank you, thank you, THANK YOU! This works like a charm. – 2oh1 Dec 15 '15 at 18:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .