How to get current folder position

I have a script that will set a Finder window to a specific size and position on the screen. I would like to know how to get the current position of the window I want to set, so each window I run this script on will not be moved to the same place on the screen.

I would drag the Finder onto the Script Editor to see its library, but I don’t know what application to drag in order to do that…

Hi,

Try this

tell application "Finder"
	set theBounds to bounds of the front Finder window
end tell

Yu can get the Finder dictionary by going to Edit>Open Dictionary [or key Cmd-Shift-O] in Script Editor, or better stil I keep the Script Editor Library open Window>Library [or key Cmd-Shift-L]

Best wishes

John M

tell application "Finder" to bounds of window 1 --> {52, 44, 760, 760}

You can browse some dictionaries in the Window > Library menu item (I think it includes the Finder)
If you are using Smile, you can use the cool-tool Dictiobrary (similar to Script Editor’s Library, with some extra functions): http://scriptbuilders.net/files/dictiobrary1.1.html

I use this for window placement scripting

To see where a window is, place it and run


tell application "Finder"
	position of every window
end tell

That will give you the {x,y} position. Isn’t bounds the size of the window (distance of the 4 sides to screen edges)?
SC

I thought “bounds” would be more useful, since sweyhrich mentioned both “size” and “position”, and the four points will help with overlaps :wink:

“Bounds” is in the form {x1, y1, x2, y2}, where where 1 is the position of the top left corner, and 2 is the position of the bottom right corner.

Thanks for the comments thus far!

Let me clarify what I need.

I have this line of Applescript that works:

set bounds of thisFolder to {220, 440, 1100, 740}

My purpose is to fix a window to a specific height and width. What I would like to do, however, is not have EVERY window that I run this script on end up at 220, 440 for its x,y position on the screen. If I could know the CURRENT x,y position of that window, I could change the above to:

set bounds of thisFolder to {x, y, 1100, 740}

So, how is this done?


tell application "Finder" to set WinName to name of every window

tell application "Finder" to set WinPosition to position of every window

set AppleScript's text item delimiters to ","
set Xcord to text item 1 of (text item 1 of WinPosition)
set Ycord to text item 2 of (text item 1 of WinPosition)
set DispCoord to "{" & Xcord & ", " & Ycord & "}" as string

display dialog "Window '" & text item 1 of WinName & "' has current position " & DispCoord

If I understand you correctly, then you can use this to display the current position of the frontmost window of the finder. Select any Finder window and run this to get its current position. You can set windows where you want them and log the positions.

One note, in the line
set bounds of thisFolder to {220, 440, 1100, 740}
{220,440} is the position, so as jj said bounds gives you the whole picture.
SC

Sitcom,

Thanks for the detailed reply!

Now, your script says to get the position of EVERY window on the screen. What I would like it to do is just work with the window I selected before running the script. That is, can I do something like this:

tell application "Finder" to set WinPosition to position of front window

set AppleScript's text item delimiters to ","
set Xcord to text item 1 of (text item 1 of WinPosition)
set Ycord to text item 2 of (text item 1 of WinPosition)
set bounds of front window to {Xcord, Ycord, 1100, 740}

No
You need to drop your Finder icon on your script editor icon to open the Finder dictionary.Then you can see what you have to work with. Click on ‘window’ under “Window Classes” and you can see all the properties. “Front” is not one of them, nor is “frontmost”.

I worked around this by getting “every window” knowing that this returns a list where as you said “the window I selected before running the script” will always be the first item in this list. When you select a new Finder window it is moved to the front of the list; that is why I get text item 1 of text item 1(The X coordinate of the first coordinate set in the list) and text item 2 of text item 1 (The Y coordinate of the first coordinate set in the list). Those lines are only for display purposes; to view the coordinates in the result window of the script editor, all you need is

tell application "Finder" to set WinPosition to position of every window
text item 1 of WinPosition

In earlier X systems, position was not changable. The correct way to go about it would be to get the ID of the window, and refer to it that way:

run that to get the window ID; lets say it returns {6, 15, 16}. ‘6’ is the front window ID, ‘15’ is the second layer, ‘16’ the last.

Then the proper syntax to set bounds/ position SHOULD be

Anyone? Bueller? Anyone?
SC

Okay. Based on our previous conversation, I have now modified my script to read this way:

tell application "Finder" to set WinName to name of every window

tell application "Finder" to set WinPosition to position of every window

set AppleScript's text item delimiters to ","
set Xcord to text item 1 of (text item 1 of WinPosition)
set Ycord to text item 2 of (text item 1 of WinPosition)

tell application "Finder" to set bounds of window 1 to {Xcord, Ycord, 1100, 740}

This one works just peachy in NOT moving the position of the window. It STINKS, however, because it makes the window as large as possible. Even if I change that 1100, 740 number to something small like 300, 100 it STILL makes the window as large as possible.

What am I missing here?

Interesting. I’ve been following this, but just tried it. When I

tell application "Finder" to set x to name of every window

, it returns an empty list. Could it be dual screens that is screwing this up? Same results for “position of every window”. Am I missing an OSAXen?

Hi Steven,

If I understand you - you want the front window to be a specific size.

Try this:

tell application "Finder"
	set theBounds to bounds of the front Finder window -- reference to window bounds in form {x1, y1, x2, y2}
	set item 3 of theBounds to (item 1 of theBounds) + 400 -- make x2 = x1 + 400
	set item 4 of theBounds to (item 2 of theBounds) + 300 -- make y2 = y1 + 300
	set bounds of the front Finder window to theBounds -- set the window to the new bounds
end tell

x2 must be larger than x1 and y2 must be larger than y1 - otherwise undesirable things happen like getting an unclickable Finder window.

Hi NovaScotian,

tell application "Finder" to set x to name of every window

works on my machine.

Best wishes

John M

Complete mis-understanding on my part. Telling the Finder to get the name of every window produces a list of Finder windows only, not of all the windows open on the desktop.

Duh… While I was declaring it didn’t work, I didn’t have any Finder windows open.

John M,

That is a great, compact script, and is just what I was looking for. I experimented and found that 900 and 300 give me a window that is the size I wanted.

Now, a final question regarding the settings for saving a script. If I check “save as”, I’m presented with saving as a Script, Application, Script Bundle, Application Bundle, or text. I would like to have this in my Finder window bar (that is, the bar that is in each window in the Finder), and to execute quickly. If I save it as an Application, it has to bounce in the Dock for a short time before it runs. If I save it as a script, the Script Editor has to bounce, but if I leave the Script Editor open, it runs immediately. What is my best choice?

Hi Steven,

In this situation I’d personally save it as an Application.

Best wishes

John M