4

I'm trying to get the bounds of my desktop window by using applescript with the following code:

tell application "Finder"
    get bounds of window of desktop
end tell

I keep getting "execution error: Finder got an error: Can’t get bounds of window of desktop. (-1728)".

I'm running Lion. Any idea how I can get this to work?

pasawaya
  • 11,515
  • 7
  • 53
  • 92

1 Answers1

6

The Desktop and a window are two different things. The Desktop is actually a folder located at Macintosh HD:Users:yourusername:Desktop:. If you mean that you want to get bounds of a window in the Finder, then you need to identify the window in question. Something like this will work...

tell application "Finder"
    set windowCount to (count windows)
    if windowCount > 0 then
        set windowBounds to bounds of window 1 --> Note the '1'
        return windowBounds
    end if
end tell

Note the checking to see if any windows are actually open as Applescript will return an error if there are no windows open.

If you are looking to get the bounds of your screen, then all you need is the following...

tell application "Finder"
    get bounds of window of desktop --> weird but that's Applescript for you
end tell
--> Result: {0, 0, 1440, 900}

If the ability to quit the Finder or make it disappear is enabled, then the above will not work.

Philip Regan
  • 5,005
  • 2
  • 25
  • 39
  • 1
    i'm trying to get the bounds of my screen. i'm not getting the same result as you. as i mentioned, i'm seeing "execution error: Finder got an error: Can’t get bounds of window of desktop. (-1728)". – Chazz Hollywood Jul 14 '12 at 23:50
  • Clearly reading comprehension is not my thing today. -1729 is an AEObjectNotFound error. Do you have this code wrapped in something else, or are you running multiple monitors. The latter complicates things considerably if so. I am running Lion as well, and I can run the above code without error. – Philip Regan Jul 15 '12 at 00:04
  • nope, i just have that code in my file and nothing else. i don't have multiple monitors setup. and i've tried this on another mac i have and same issue. what version of lion are you running? i'm on 10.7.4 – Chazz Hollywood Jul 15 '12 at 00:39
  • The only way I can duplicate this error is if the Finder has the Quit Finder menu item enabled. Any chance you activated the Quit menu? – Philip Regan Jul 15 '12 at 00:59
  • 4
    got it! i forgot i had turned on this preference: defaults write com.apple.finder CreateDesktop -bool false thanks for steering me in the right direction – Chazz Hollywood Jul 15 '12 at 01:30
  • This appears to get the bounds for **all** screens, combined, in a multi-monitor setup. – SilverWolf Nov 21 '17 at 23:30