Script for setting Safari window position

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"

Solution 1:

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).

Solution 2:

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.