MacOS – Simple 3 line applescript – Error -128

applescriptmacos

I have a scenario where I'd like 2 separate applescripts.

The idea is super simple. 2 Applications, but only one runs at a time.

Example: Close application 1, launch application 2 (and vise versa)

Here are the 2 scripts I have:

A.) OpenSteam

tell application "Plex Home Theater" to quit
delay 5
tell application "Steam" to activate

B.) OpenPlex:

tell application "Steam" to quit
delay 5
tell application "Plex Home Theater" to activate

The script "OpenSteam" works perfectly fine. It does exactly what I want it to do. However, the same script in reverse (OpenPlex) does not seem to work.

Steam closes, but my script then gives me the following error *Note, Plex is not launched:

error "Steam got an error: User canceled." number -128

I have no idea what this means, and was hoping someone could help me out.

I got my initial information from here: https://forums.plex.tv/index.php/topic/27945-harmony-one-custom-command/#entry179765 I have revised the example scripts to fit my needs.

Best Answer

Your headache lays with how Steam handles its exit status. Steam probably doesn't have applescript integration, so it considers a quit command from applescript an unclean exit. It would still quit, but it will let you know about it. What you need is a way to ignore the error message and execute the rest of the script nevertheless.

try
    tell application "Steam" to quit
    on error error_message number error_number
        if error_number is equal to -128 then
        --Keep Calm and Carry On
        else
            display dialog error_message
        end if
end try
delay 5
tell application "Plex Home Theater" to activate

You can combine the two scripts into one toggle/switch:

set steamRunning to false
set plexRunning to false

try
    do shell script "pgrep -l -f 'Steam.app'"
    set steamRunning to true
end try

try
    do shell script "pgrep -l -f 'Plex Home Theater'"
    set plexRunning to true
end try

if steamRunning is true and plexRunning is false then

    try
        tell application "Steam" to quit
    on error error_message number error_number
        if error_number is equal to 128 then
            --Keep Calm and Carry On
        else
            display dialog error_message
        end if
    end try
    delay 5
    tell application "Plex Home Theater" to activate

end if


if steamRunning is false and plexRunning is true then

    tell application "Plex Home Theater" to quit
    delay 5
    tell application "Steam" to activate

end if

I don't have either app so I cannot debug. There could be multiple processes named "steam", given its game-thing, so I used "Steam.app", assuming that's the app name under /Applications/.