Applescript to move a window relative to it's current position

I want to move the position of a current window 5 pixels up, and 5 pixels right.
I have found code to move a window to a fixed position.
I have found code to get the position of a window.
I can't figure out how to get it all to work together.
I can imagine doing this either by getting the window position into a variable and using the result, or by a position relative command if there is one.
Here's what I have so far, which works but uses a fixed position only.

set delta to 5
set screenOneXOffset to -120
set screenOneYOffset to -1300
tell application "System Events"
    set position of first window of application process "Google Chrome" to ¬  
    {screenTwoXOffset + delta, screenOneYOffset + delta}
end tell

How to get the position of a window (but then not sure how to use it):

tell application "System Events" to get properties of window 1 of application process "RStudio"

Solution 1:

I want to move the position of a current window 5 pixels up, and 5 pixels right.

I do not have RStudio, however, here is an example using Google Chrome.

Example AppleScript code:

if not running of application "Google Chrome" then return
tell application "System Events"
    tell application process "Google Chrome"
        if not (exists window 1) then return
        set |position| to position of window 1
        set position of window 1 to ¬
            {((item 1 of |position|) + 5), ((item 2 of |position|) - 5)}
    end tell
end tell

Notes:

Since you didn't state how you intended to implement the AppleScript code, I've included some error handling that may not be necessary, e.g. if not running of application "Google Chrome" then return (Or whichever application you set it for.)

Once a window had been repositioned to where it meets the bottom of the menu bar, or top of the screen if the menu bar is hidden, it will no longer move upwards and will only continue to move to the right.



Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Solution 2:

I achieved the same thing in a clunkier way ;))
I used Safari rather than Chrome or RStudio.

properties is a huge list, most of which you don't need. You can just get position which is a simple list of two integers {x, y}
You can capture any result using result
My struggle was to capture both, item 1 & item 2 of result, which I completely failed in, until I first set them to a variable Pos which I could then call from. I'm sure someone else could figure how to get around that in one step.

tell application "System Events" to get position of window 1 ¬
    of application process "Safari"

set Pos to result
set posX to item 1 of Pos
set posY to item 2 of Pos
set delta to 5

tell application "System Events"
    set position of first window of application process "Safari" to ¬
        {(posX + delta), (posY - delta)}
end tell