0
tell application "Google Chrome"

make new window
open location "https://google.com"

end tell

The above command spawns up an instance of google chrome. I want to get the process id of this process so that I can kill it later. Please note that I am using Google chrome for example, but I can spawn up any process any number of times. Just need to get the process id.

  • This code does not spawn a new process every time; it merely opens a new window in the Chrome application (launching Chrome once, if it's not running). Are you trying to open multiple concurrent processes, or are you trying to keep track of the windows that you open with this code? – Ted Wrigley Jun 13 '20 at 15:51
  • Idea is to open a process using this and getting its id so that I can close it later programmatically – knowbalpreet Jun 13 '20 at 15:57
  • Let me just be sure we're speaking the same language. Instead of having one instance of Google Chrome with with (say) five windows, you want to have five instances of Google Chrome each with one window? That's more typical of a MS Windows environment, but you can do it on MacOS if you really want to. My point is that the code you've written **does not** open several instances of Chrome; it opens several windows within one instance of Chrome. – Ted Wrigley Jun 13 '20 at 16:06
  • I need you to be clearer about what you are *trying* to accomplish. Asking for the process id of each window makes no sense, since (on MacOS) they will all generally have the same process id. – Ted Wrigley Jun 13 '20 at 16:08
  • I am not sure what's the best course of action here. My objective is to open Window / instance (if not running already ) and be able to close it later programmatically. I thought that the best way to do this would be to keep track of all new process ids and kill them later. – knowbalpreet Jun 13 '20 at 16:08
  • If all you want to do is close chrome at some future date, you don't need to know its process id. In AppleScript you can use `tell application "Google Chrome" to quit`, and the system will know what to do. – Ted Wrigley Jun 13 '20 at 16:12
  • But will this close the entire instance of Google chrome or just the window? – knowbalpreet Jun 13 '20 at 16:21
  • That will close the entire instance of Chrome. You just want to close the window later? – Ted Wrigley Jun 13 '20 at 16:24
  • Yes i want to close app/window created from this script – knowbalpreet Jun 13 '20 at 16:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215895/discussion-between-knowbalpreet-and-ted-wrigley). – knowbalpreet Jun 13 '20 at 16:31

1 Answers1

1

You can store a reference to the window you make in a variable, and use that variable to close it later. The following opens two windows (to Google and Yahoo), waits three seconds, the closes the Google window:

tell application "Google Chrome"
    set windowGoog to make new window
    tell windowGoog to open location "https://google.com"
    set windowYah to make new window
    tell windowYah to open location "http://Yahoo.com"
end tell

(*
    You can do whatever you need to do here. I've added a three second delay just 
    to give a sense of time, but that's just for show.
*)
delay 3

tell application "Google Chrome"
    close windowGoog
end tell

If you want to keep a reference to the window across multiple runs of the script (e.g., you run the script once to open the window, then run it again later to close it) make the variable a property, and use if statement to check if it has a value, like so:

(* 
    These two lines set 'windowGoog' and 'windowYah' to 'missing value' on the 
    first run, and then remember whatever value you set until the next time 
    you recompile the script
*)
property windowGoog : missing value
property windowYah : missing value

tell application "Google Chrome"
    if windowGoog is missing value then
        set windowGoog to make new window
        tell windowGoog to open location "https://google.com"
    else
        close windowGoog
        set windowGoog to missing value
    end if
    if windowYah is missing value then
        set windowYah to make new window
        tell windowYah to open location "http://Yahoo.com"
    else
        close windowYah
        set windowYah to missing value
    end if
end tell
Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
  • Hey can you also share the example for how to make this variable a property as the objective is to close these windows via different script – knowbalpreet Jun 13 '20 at 18:33
  • This did not work for me. It opened 2 new windows on every execution of the script. Please let me know if I am doing something wrong. – knowbalpreet Jun 14 '20 at 12:31
  • @knowbalpreet: you mean you had 2 windows, then 4, then 6, then 8? Are you running this from the Script Editor? Did you use a fresh new Script Editor window? Don't recompile the the script just run it; if you recompile it will zero-out the property values. – Ted Wrigley Jun 14 '20 at 13:52
  • @knowbalpreet: Piece of advice: if you're having problems, it is *never* enough to say "it didn't work" or report the wrong result you got. You **must** say what *you* did. The problem isn't with the script or the computer; the problem is (always) with how the user is interacting with the computer. You have to say what steps you took, even if they don't seem relevant to you, or no one will be able to help you. – Ted Wrigley Jun 14 '20 at 13:57
  • sorry my bad. I don't have much experience with Applescript, I am running this script from the terminal. this is what I tried to run from the CLI https://pastebin.com/aAPgFmfp. I want to run this script independently, once to start all applications, and then to close them. – knowbalpreet Jun 14 '20 at 14:09
  • @knowbalpreet: I don't know why you are running this from terminal, but if you *need* to do that, first copy the script into Script Editor.app, save the script to disk, and then use osascript to run the saved script: `osascript /path/to/scriptfile.scpt`. That will work. – Ted Wrigley Jun 14 '20 at 14:13
  • Is there a common way to open a new window/instance of any app? like in this example you use `make new window`, this works fine with apps like `Google Chrome` but do not work with for eg: `Visual Studio Code`. I tried finding it online but couldn't find anything significant. – knowbalpreet Aug 08 '20 at 19:08
  • @knowbalpreet: whether or not certain commands are implemented is entirely up to the developer, who has to implement the commands and set up the dictionary. The `make` command is the standard command for this kind of action, but if the developer hasn't implemented it, it won't work. – Ted Wrigley Aug 08 '20 at 19:30
  • Do you know of some other way with which we can open new instance of an app and assign property to it so that it can be closed later. – knowbalpreet Aug 09 '20 at 08:04