I often like to set the bounds of certain apps so I can ‘reset’ their window’s position, and I often run a script to get the bounds of an app’s window.
So I was trying to write a script to get the bounds of a window, format it and then copy it to the clipboard (so I can paste it into another script).
I came up with the script below, and then realised that it won’t work as each property isn’t alway 4 characters, or even always 3 characters, as they change depending on the size and position of the window
… duh!
tell application "Safari" to get the bounds of window 1
set windowBounds to the result as string
set leftEdge to text 1 thru 4 of windowBounds
set topEdge to text 5 thru 8 of windowBounds
set rightEdge to text 9 thru 12 of windowBounds
set bottomEdge to text 13 thru 16 of windowBounds
set windowBounds to "{" & leftEdge & ", " & topEdge & ", " & rightEdge & ", " & bottomEdge & "}"
set the clipboard to windowBounds
So is there any way to do what I’m trying to do, taking into account that the amount of characters for each edge isn’t consistent?
When you want to work with the items of a list, maybe try not coercing the list to a string and then re-dividing it.
tell application "Safari" to set theBounds to the bounds of window 1
tell theBounds to set {leftEdge, topEdge, rightEdge, bottomEdge} to {item 1, item 2, item 3, item 4}
set windowBounds to "{" & leftEdge & ", " & topEdge & ", " & rightEdge & ", " & bottomEdge & "}"
set the clipboard to windowBounds
Hey, the above line is a bit unusual for me. I can understand the command: tell Application “Finder” to etc… , since Finder has its own dictionary of commands that it can respond to.
But “theBounds” is a variable. Which commands does it understand ?
Marc’s answer was, of course, entirely correct. But for other ways to think about it…
I could have written:
set {leftEdge, topEdge, rightEdge, bottomEdge} to {item 1 of theBounds, item 2 of theBounds, item 3 of theBounds, item 4 of theBounds}
But that’s a lot of redundant “of theBounds.”
If I’m going to do a bunch of things that are all relative to a particular variable, instead of specifying the variable every time, I can address the commands to the variable with a “tell.” Then, since I’m telling that variable to do things, all further commands assume the scope is within that variable, so I don’t need to specify it anymore.