2

I wrote a script to open Safari in a new tab with custom window size and window position. However, setting the window position is just not working. Below is the script:

#!/bin/sh

loggedInUser="$(/bin/ls -la /dev/console | /usr/bin/cut -d " " -f 4)"
open /Applications/Safari.app/
osascript -e "tell application \"Safari\"
    make new document
    set URL of document 1 to \"http://link.com"
    set bounds of window 1 to {0, 0, 400, 700}
    set position of window 1 to {40, 50}
    end tell"
    activate
end tell"
4
  • Is this an exact copy of your script? The quotes seem to be wrong. In addition, what does "not working as expected" mean?
    – nohillside
    Oct 1 at 14:33
  • window positioning is just not working.
    – mano
    Oct 1 at 14:36
  • 1
    Can you fix the quotes and the escaping and try again?
    – nohillside
    Oct 1 at 14:38
  • Also, you probably need to either launch Safari from within AppleScript or sleep a few seconds between the open and the osascript
    – nohillside
    Oct 1 at 14:39
0

You have an extra end tell in there and your quoting is weird.

Try this.

set theXposition to 40
set theYposition to 50
set theXsize to 700
set theYsize to 700
tell application "Safari"
    make new document
    set URL of document 1 to "https://www.google.com"
    set bounds of window 1 to {theXposition, theYposition, theXsize, theYsize}
    activate
end tell

The arguments to the bounds property include the window position, so just set it there.

You may get bit if Safari isn't already running, and starting it causes it to restore your last set of session windows (you will get more than the one window you are creating).

0

The following example shell script code works for me:

#!/bin/sh

loggedInUser="$(whoami)"
open -a "Safari"
osascript <<EOS
tell application "Safari"
    make new document
    set URL of document 1 to "http://link.com"
    set bounds of window 1 to {40, 50, 400, 700}
    activate
end tell
EOS

Notes:

The value for bounds may need to be adjusted for your liking, however, this is now a functioning shell script based on your shell script.

2
  • this works, but it doesn't allow the window to move to rightmost bottom corner of the screen, (just like a chat support window). the dimensions of size of the window and X,Y axis is somehow related related it seems
    – mano
    Oct 4 at 17:36
  • @mano RE: "this works, but it doesn't allow the window to move to rightmost bottom corner of the screen," -- Well nowhere in your question did you mention wanting to position the window in the lower right corner and I'm just using the information you presented in your question! If you want the window elsewhere, then set it bounds accordingly. You could size and position the window here you want it and then run tell application "Safari" to return bounds of window 1 and use what it returns for the value of the bounds property in your script. Oct 5 at 1:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .