Bounds and Icon Size in Maverick

I can’t seem to find an example of setting bounds of a folder and set the Icon size of an open folder (Window 1).
Am I doing something wrong or is this broken in Maverick.
Thanks in advance for any help.
PolishPrince


tell application "Finder"
	get name of window 1 --of ( current folder)
	set FolderName to (name of window 1 as text)
	get bounds of window 1
	--set bounds of window 1 to {0, 45, 1900, 1200}
	
	if bounds of window 1 is equal to {0, 45, 850, 600} then
		beep 2
		set Icon_Size to 64 as integer
	else
		beep 3
		set Icon_Size to 128 as integer
	end if
end tell
 

Hello.

tell application "Finder"
	tell Finder window 1
		set current view to icon view
		tell its icon view options
			set icon size to 128
		end tell
		set {a, b} to {container of its target as alias, its target}
		set target of it to a
		set target of it to b
	end tell
end tell

McUsrII

Thanks for the Reply. The code for the Icon Size works fine, but one of the functions of my script is to be able to adjust the Icon Size with an If statement comparing the bounds of the window size. My problem is I can’t get the If statement to get the window bounds and compare so I can adjust the window size along with the adjusted Icon Size.

PolishPrince

Hello.

You can’t compare more than one item of a list at a time, unless you have some special handler that compares the elements individually.

I think it should suffice for you to compare the 3d and 4th element of the bounds property of a window, which represents it’s width and height respectively, with respect to Finder windows.

tell application "Finder"
	set b to bounds of Finder window 1
	if item 3 of bounds < 300 and item 4 of bounds > 1100 then
		
		-- ...
	end if
end tell

You should really have a look in the dictionary.

McUsrII

Changing the Icon Size by itself works and Changing the Bounds by itself works but the Icon Size will not change when they are combined together in the same Tell statement. This is what I have so far.


--> Clicking this application should select large window with larger icons. Select the application again and it should go back to smaller window and smaller icons.
tell application "Finder"
	--get bounds of window 1
	set b to bounds of Finder window 1
	
	if item 3 of b is equal to 1850 and item 4 of b is equal to 1200 then
		beep 2
		tell Finder window 1
			set current view to icon view
			tell its icon view options
				set icon size to 128
			end tell
		end tell
		set bounds of Finder window 1 to {0, 45, 800, 600}
		
	else if item 3 of b is equal to 800 and item 4 of b is equal to 600 then
		beep 3
		tell Finder window 1
			set current view to icon view
			tell its icon view options
				set icon size to 256
			end tell
		end tell
		set bounds of Finder window 1 to {0, 45, 1850, 1200}
		
	else
		beep 4
		tell Finder window 1
			set current view to icon view
			tell its icon view options
				set icon size to 64
			end tell
		end tell
		
		set bounds of Finder window 1 to {0, 45, 800, 600}
		
	end if
end tell

Hello.

You haven’t switched back and forth between list view, ( or some other view) to make the icon size change thake place.

See the three lines involving a, and b my script in post #2. Other than that: you are clearly in the right direction.
Now if you consistentlym start by changing bounds, then changes the icon view, and then switches the target of the window for a brief, then I am sure we have a winner!

Well done! :slight_smile:

Hello.

I played around a little bit with your script, and I ended up with the script below. It is not bullet proof in any sense, but at least you won’t get a run time error if you try to run it on either the computer container, or a Spotlight window.

You may speed up things a little later on, should you need it, by embedding the script in a script, and tell that script to run from the top level of your script. (script theScript . the code . end script… tell theScript to run).

--> Clicking this application should select large window with larger icons. 
-- Select the application again and it should go back to smaller window and smaller icons.
tell application id "MACS"
	-- Assures us that we have a valid finder window.
	if (count its Finder windows) = 0 then ¬
		tell (make new Finder window) to set its target to (path to home folder as alias)
	
	tell Finder window 1
		local trg, bnds, ics_sz
		set trg to its target
		try
			if class of trg is not folder then error number -128
		on error
			tell me to display notification "No Valid Finder Window"
			error number -128 -- it was a Spotlight window!
		end try
		set bnds to bounds of it
		
		if item 3 of bnds is equal to 1850 and item 4 of bnds is equal to 1200 then
			beep 2
			set ics_size to 128
			set bounds of it to {0, 45, 800, 600}
			
		else if item 3 of bnds is equal to 800 and item 4 of bnds is equal to 600 then
			beep 3
			set ics_size to 256
			set bounds of it to {0, 45, 1850, 1200}
			
		else
			beep 4
			set ics_size to 64
			set bounds of it to {0, 45, 800, 600}
			
		end if
		set current view to icon view
		tell its icon view options
			set icon size to ics_size
		end tell
		
		-- we refresh the window to reflect the icon size change!
		set {other, trg} to {container of its target as alias, trg as alias}
		set target to other
		set target to trg
		
	end tell
end tell

Hello.

Here is another idea of my own, it just cycles through different icon sizes in the front Finder Window, you may change it to suit your needs. :slight_smile:

-- Copyright 2013 © McUsr and Put in public domain
script cycleIconSize
	tell application id "MACS"
		-- Assures us that we have a valid finder window.
		if (count its Finder windows) = 0 then ¬
			tell (make new Finder window) to set its target to (path to home folder as alias)
		
		tell Finder window 1
			local trg, bnds, ics_sz
			set trg to its target
			try
				if class of trg is not folder then error number -128
			on error
				tell me to display notification "No Valid Finder Window" subtitle "Cycle Icon Size"
				error number -128 -- it was a Spotlight window!
			end try
			set current view to icon view
			tell its icon view options
				local hit
				set hit to false
				repeat with i from 1 to 4
					if icon size = (32 + (i - 1) * 16) or icon size < (32 + i * 16) then
						set {hit, icon size} to {true, 32 + i * 16}
						exit repeat
					end if
				end repeat
				if not hit then set icon size to 32
			end tell
			-- we refresh the window to reflect the icon size change!
			set {other, trg} to {container of its target as alias, trg as alias}
			set target to other
			set target to trg
			
		end tell
	end tell
end script

tell cycleIconSize to run

McUsrII

Thank you so much for your help. Your version is working just as I wanted it to.
Sometimes I get confused when writing Applescripts with questions like ( Do i use the Finder, System Events or container window). Is there any particular tutorials or books that would help.

PolishPrince

Hello.

I think there is no right answer, but generally, you’ll script the finder only if the solution has to be in Finder, like if we do something like we just did, or if you want the result of some operation to be readily displayed in Finder.

I there is no need to invoke the finder to see the result after something is run, then there is no need for Finder, and System Events is a better alterntative, especially now, under Mavericks.

There are several reasons for this, one of them being that Finder is a real resource hog, that really needs much power to update the internal datastructures of its windows. The second is speed: The consumption of rescources, and the need for screen updating also make it into a slow performer.

Everybody should really start by scripting the Finder. :slight_smile:

You may look for a good tutorial at MacOsXAutomation.com (The one where you make a window arrangment script.) If there is any article about Finder in the Unscripted section here, then I recommend that one as well.

When I need to script something, I tend to start off by seeing if Bill Cheesman has written an article about it at MacTech.com. :slight_smile:

There is also a still exiting guide from Apple.com, that is named “Apple Script Finder Guide”, but it is old and I don’t think everything still holds in it. But by all means, it can’t hurt.

I hope this helps, and remember that there aren’t any stupid questions around here, only stupid answers. :slight_smile: