I’ve run across a small problem. As far as I can tell, setting window bounds in the 10.4 Finder does not “stick”. The code works fine, but when the window is closed and reopened it reverts to its original size and position. Anyone know a way to get this to stick, maybe via UI scripting to “drag” the grow box?
Here’s my script (sets up a window for “thumbnail view” - useful for photographic collections, especially with the new slideshow options in the 10.4 Finder):
tell application "Finder"
-- Get the current window
set thisFolder to front window
-- Remove the toolbar - it interferes with the window bounds
set toolbar visible of thisFolder to false
-- Change to icon view
set current view of thisFolder to icon view
-- Set the window to be auto-arranged with full-size icons
set properties of the icon view options of thisFolder to {arrangement:arranged by name, icon size:128, shows icon preview:true}
-- Set the window size optimally for thumbnail icons
try
-- Tiger spacing is approximately 165 pixels per icon
set iconSpacingX to 165
-- Pull the screen resolution from the window server (may not work with multiple screens)
set screenX to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number
set screenY to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number
-- Setting window bounds does not "stick" on Tiger (tested through 10.4.1)
set bounds of thisFolder to {4, 48, screenX - (screenX mod iconSpacingX), screenY}
on error error_message number error_number
end try
end tell
Diamondsw
I too have been looking for a way to make the position of a folder stick. After it is reopened it reverts back to its original position. While looking through your script, it is indeed a very useful script for veiwing photo folders. I did some work on your script and i wanted to share it with you. I think that it is a good thing that the position doesn’t stick on this script. I put the compiled application script on my dock and when i open a folder i just hit the icon on my dock and the folder enlarges, but then if you hit the icon on the dock again it puts the folder back as it was originally. If the toolbar was visible when the folder was open then it will put it back. If the toolbar wasn’t visible then it won’t be when the folder is reset. I’m using a 23 inch cinema display and it works great. It should work on any size display though because you used the get screen size method.
Some of my lines on my code are pretty long so make sure when you compile it they are on one line. I am new to Applescript so the code could probably be made much more efficient.
If you or anybody knows how to set the position of a window and make it stick please let us know. Thanks
tell application "Finder"
-- Get the current window
set thisFolder to front window
-- Find out what the toolbar visibility is before we start
if toolbar visible of thisFolder = true then
set TBar to true
else
set TBar to false
end if
-- Change to icon view
set current view of thisFolder to icon view
-- Set the window to be auto-arranged with full-size icons
set properties of the icon view options of thisFolder to {arrangement:arranged by name, icon size:128, shows icon preview:true}
-- Set the window size optimally for thumbnail icons
try
-- Tiger spacing is approximately 165 pixels per icon -- changing it to 174 fills the screen to the right edge
set iconSpacingX to 174
-- Pull the screen resolution from the window server (may not work with multiple screens)
set screenX to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number
set screenY to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number
-- If the window was expanded then we want to compress it back to normal size
-- If not then expand the window
if bounds of thisFolder = {4, 45, screenX - (screenX mod iconSpacingX), (screenY - 55)} or bounds of thisFolder = {4, 45, screenX - (screenX mod iconSpacingX), (screenY - 60)} then
-- Restore the toolbar - set it back like it was
if bounds of thisFolder = {4, 45, screenX - (screenX mod iconSpacingX), (screenY - 60)} then
set toolbar visible of thisFolder to true
set bounds of thisFolder to {10, 50, 970, 500}
-- Set the window to be auto-arranged with 64-size icons
set properties of the icon view options of thisFolder to {arrangement:arranged by name, icon size:64, shows icon preview:true, shows item info:true}
set toolbar visible of thisFolder to true
else
set toolbar visible of thisFolder to false
-- Set the bounds to different size if toolbar visible was false
set bounds of thisFolder to {10, 50, 840, 560}
-- Set the window to be auto-arranged with 64-size icons
set properties of the icon view options of thisFolder to {arrangement:arranged by name, icon size:64, shows icon preview:true, shows item info:true}
set toolbar visible of thisFolder to false
end if
else
if TBar = true then
-- Restore the toolbar - set it back like it was
set toolbar visible of thisFolder to false
set bounds of thisFolder to {4, 45, screenX - (screenX mod iconSpacingX), (screenY - 60)}
else
set toolbar visible of thisFolder to false
set bounds of thisFolder to {4, 45, screenX - (screenX mod iconSpacingX), (screenY - 55)}
set toolbar visible of thisFolder to false
end if
end if
on error error_message number error_number
end try
end tell
Model: Mac G5
AppleScript: 2.1.1
Browser: Safari 416.12
Operating System: Mac OS X (10.4)
Finder’s inability to save scripted changes to a window’s bounds is caused by an unfortunate bug, which I understand may take a while to fix. The only workaround of which I’m aware is rather clunky, since it involves toggling the window’s toolbar - but it does work…
Here’s a reworking of Diamondsw’s script. Besides the workaround mentioned, I’ve suggested an alternative method to determine current screen dimensions, a slightly different algorithm to calculate optimum window width - and a check to make sure an appropriate window is available in Finder:
set p to {4, 48}
tell application "Finder"
activate
tell (get bounds of desktop's window)
tell item -2 to set x to it - (it - 15 - (p's item 1)) mod 180
set b to p & x & item -1
end tell
tell (first window whose class is Finder window) to if exists then
set toolbar visible to false
if bounds is not b then
set bounds to b
set toolbar visible to true
set toolbar visible to false
end if
set current view to icon view
set properties of its icon view options to ¬
{arrangement:arranged by name, icon size:128, shows icon preview:true}
end if
end tell
Kai
Your script works great. I do have some problems following some of the code in Applescript. Is it possible to adjust your code that it would set the bounds to the size of a selected folder or say to a position like {10,50,800,600}. Also can the code set the bounds and position of all the folders in that folder no matter how deep the folders go to the same size . I have been working with this size and position of folders for some time now and it is driving me crazy. Thank you for all the help.
If you want to align the windows of sub-folders precisely, then the toolbar visible property needs to be consistent for them, too. The following suggestion therefore includes a ‘toolbar’ parameter to facilitate this:
to set_bounds for f to b given toolbar:toolbar
tell application "Finder" to tell folder f
tell container window
set already_open to class is Finder window
open
set toolbar visible to toolbar
set bounds to b
set toolbar visible to not toolbar
set toolbar visible to toolbar
if not already_open then close
end tell
repeat with i in (get folders)
my (set_bounds for (i as alias) to b given toolbar:toolbar)
end repeat
end tell
end set_bounds
tell application "Finder" to my (set_bounds for (choose folder) to {10, 50, 800, 600} with toolbar)
(If you don’t want the toolbar to be visible, then simply change “with toolbar” to “without toolbar” in that last statement.)