Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I've got a little bit of ruby that sizes and repositions windows to a saved layout. It uses a snippet of AppleScript to actually do the positioning:

osascript -e 'tell application "Twitter" to set the bounds of the front window to {894, 22, 1604, 1049}'

There are two problems though:

  1. This doesn't work for every app, for instance when I use it to set the position of Gitbox, I get the following error:

    37:43: execution error: Gitbox got an error: Can’t get bounds of window 1. (-1728)
    
  2. Some window positions aren't saved, for instance if I run the script to reposition everything, then I close the MacVim window and open a new one, it will not remember the position of the window that was just closed.

Is there a more robust way to set the size and positions of windows?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

The problem you are having with Gitbox is because not all application are scriptable. Which means you cannot talk to it via applescript. What you can do for apps like that is use System Events to do some of it for you.

*Check if scriptable *

set theApp to "Gitbox"

tell application "System Events"

    set isScriptable to has scripting terminology of application process theApp

    if isScriptable then
        my scriptableApp(theApp)

    else
        my nonScriptableApp(theApp)

    end if
end tell
on scriptableApp(theApp)
    tell application theApp to get the bounds of the front window
end scriptableApp
on nonScriptableApp(theApp)
    tell application "System Events"
        set the props to get the properties of the front window of application process theApp
        set theSBounds to {size, position} of props
    end tell
end nonScriptableApp

Get the bounds via system events

#get the bounds via system events 

tell application "System Events"
    set the props to get the properties of the front window of application process "Gitbox"
    set theSBounds to {size, position} of props
end tell

Set the bounds via system events

--set theSBounds to {{799, 490}, {513, 430}} #This is a test line that will set the bounds list so you can see the set bound code working un comment to use it

#set the bounds via system events 
tell application "System Events"
    set size of front window of application process "Gitbox" to item 1 of theSBounds
    set position of front window of application process "Gitbox" to item 2 of theSBounds
end tell
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.