How to check in AppleScript if an app is running, without launching it - via osascript utility

后端 未结 6 683
眼角桃花
眼角桃花 2020-11-30 20:11

Consider the following AppleScript:

on is_running(appName)
    tell application \"System Events\" to (name of processes) contains appName
end is_running

set         


        
6条回答
  •  失恋的感觉
    2020-11-30 20:28

    OK, I know this question is really old, but I stumbled on it looking for a different issue and had to pipe in considering how complicated some of these responses are.

    The simple code to achieve what you want(ed) is:

    tell application "System Events"
      if application process "Safari" exists then
        -- do stuff you want to do only if Safari exists
      end if
    end tell
    

    On older systems, the syntax used to be:

    tell application "System Events"
      if exists of application process "Safari" is true then
        -- do stuff you want to do only if Safari exists
      end if
    end tell
    

    One of these should definitely work for you, intrepid searcher of Applescript solutions for action only when an app is running.


    Oh! Bonus tip: And if you're not sure what the application process name is exactly (it is usually but not always the app name), before coding your final script run…

    tell application "System Events"
       get every application process
    end tell
    

    And find your app process name in the results.

    Here's a screen grab of running that command. (Note the zillions of Google Chrome Helper instances. Thanks Google!)

    enter image description here

    HTH!

提交回复
热议问题