How can I use AppleScript to open a new Safari window on the current desktop space?

5,024

You can put the following code into the apple script editor and save the file as application.

tell application "Safari"
    make new document
    activate
end tell

Now you can simply double click on the script and you will be able to open a new safari window inside of the current "active" space, even though you may have another safari window open in another desktop space. It will not switch.

Share:
5,024
Josiah Sprague
Author by

Josiah Sprague

Updated on September 18, 2022

Comments

  • Josiah Sprague
    Josiah Sprague over 1 year

    I need to open a new Safari window in the current desktop space using AppleScript. NOT move to another space which has Safari running and then open another Safari window.

    Of course, if Safari is not running then it should start a new Safari process in the current space.

  • Josiah Sprague
    Josiah Sprague about 10 years
    I have this script, but it opens the new window on the space where Safari is currently activated, not on my currently active desktop space.
  • Josiah Sprague
    Josiah Sprague about 10 years
    Actually, sometimes it opens two new Safari windows. One on my current desktop, and one on another desktop, which it then switches to. I can't figure out why more than one new document would be created. I am using an Alfred shortcut to call the script.
  • Josiah Sprague
    Josiah Sprague about 10 years
    I'm using 10.9.2. It could be a conflict with Alfred or TotalSpaces2 I suppose. I'll keep playing around with it.
  • kevin
    kevin about 10 years
    Hmm...it is certainly a possibly there may be 3rd party interference. If you are seeing 2 windows popup after running the script, a quick and dirty way to check if its coming from your script is add say "I am opening new window now" as the 2nd line. You'll be able to hear when your script is running and at least you tell if your script is for some reason running twice, hence the 2 windows popping up instead of one.
  • Josiah Sprague
    Josiah Sprague about 10 years
    Hmm... Looks like the script only runs once. Still doesn't open on the current space, but instead switches to the previous space that had Safari active.
  • kevin
    kevin about 10 years
    Ok, this may give you some helpful information. I have an extra laptop that has 10.9 installed. I copied the above script and ran it on desktop 2,3,4. Each time I ran the script on a new desktop it opened 1 new safari window on that desktop. It did not switch. This tells me that you may be getting some 3rd party interference. If possible I would suggest to temporary disable or temporary remove your 3rd party apps that you think may be causing conflict and try it again.