No internet connection
  1. Home
  2. How to

Safari Launch Quit Toggle

By John Costello @John_Costello
    2020-07-01 20:18:31.983Z

    Desired Workflow

    I would like to have a button on my SD XL open Safri, but if Safari is already open, then quit Safari. (Or any app.)

    Question

    How can I make a launch/quit application toggle?

    Command Info

    ID: user:ckbh254mu00003t10obrbnu93:ckc276x6a0002bk10d0zje6rb
    Name: Launch Safari
    

    Source

    undefined
    

    Links

    User UID: I7doJ14mp3dUHDwLh8NxCBcVqcK2

    Feedback Key: sffeedback:I7doJ14mp3dUHDwLh8NxCBcVqcK2:-MBB0yM-lm4ePnMEjz1A

    Feedback ZIP

    Solved in post #2, click to view
    • 8 replies
    1. Hi John.

      You'll need the bundle ID as well as the path to the application to do this.

      
      const appBundleId = 'com.apple.Safari';
      const appPath = '/Applications/Safari.app';
      
      var app = sf.ui.app(appBundleId);
      
      if (app.exists)
          sf.system.execAppleScript({ script: `quit app "${appPath}"` });
      else
          sf.system.exec({ commandLine: `open -a "${appPath}"` });
      
      ReplySolution
      1. John Costello @John_Costello
          2020-07-03 20:17:20.119Z

          Wow! Works perfect! I am very new to scripting but I see the unlimited potential. Thank you Christian!

          1. John Costello @John_Costello
              2020-07-03 23:07:15.163Z

              Hello Christian!

              I have learned how to find bundle ID's using the terminal. I have been able to adapt this script to many other apps, making for a great universal launch/quit streamdeck layout. But for some reason I can't make it work with apple apps like mail, notes, messages. Any help would be greatly appreciated. Thank you!

              1. John Costello @John_Costello
                  2020-07-11 21:07:13.196Z

                  I figured out that the path name for some apple applications are:

                  System/Applications/mail

                  This adapts Christians script very nicely indeed.

              2. In reply tochrscheuer:
                SSeverin Ahn @Severin_Ahn
                  2021-05-18 17:52:08.649Z

                  Hi Christian, I'm trying to do this with the "Audio-Midi-Setup" app but can't get it to work. This is what I have. Any suggestions?

                  const appBundleId = 'com.apple.audio.AudioMIDISetup'; const appPath = '/Applications/Utilities/Audio-MIDI-Setup.app';

                  var app = sf.ui.app(appBundleId);

                  if (app.exists) sf.system.execAppleScript({ script: quit app "${appPath}"}); else sf.system.exec({ commandLine:open -a "${appPath}" });

                  1. Hi Severin. At first glance: Your path to the app is wrong - it should have regular spaces in place of dashes :)

                    1. The easiest way to get the correct path is to select the app in Finder and hit Option+Cmd+C, then paste the path into SF :)

                • S
                  In reply toJohn_Costello:
                  Severin Ahn @Severin_Ahn
                    2021-05-18 21:06:04.927Z

                    That already solved it! Thanks!