Can Applescript set statements be nested?

Actually Mac OS X 10.11.6 (not an option to pick)

Et Al:
I have a script (converted to an app and put into login items for my account) that I have been using for a few years now.
The app will close all Finder windows and give me two side-by-side each with a different target.


tell application "Finder"
	activate
	close every Finder window
	--
	open home
	tell the Finder window 1
		set toolbar visible to true
		set the sidebar width to 140
		set the current view to list view
		set the bounds to {15, 30, 1265, 1350}
		end tell
	set var_LeftWindow to the target of the front window
	--
	open computer container
	tell the Finder window 1
		set toolbar visible to true
		set the sidebar width to 140
		set the current view to column view
		set the bounds to {1290, 30, 2548, 1350}
	end tell
	set var_RightWindow to the target of the front window
end tell

The two variables are for possible future use.

Can the set statements be nested? I couldn’t find anything about this through various forums nor Google.

Hints, thoughts, examples, etc. will be most welcomed.

BG

What do you mean by nested?

You can wrap the set command in an handler to make the (implicit) run handler much simpler.

tell application "Finder"
	activate
	close every Finder window
end tell
set var_LeftWindow to setWindow(false)
set var_RightWindow to setWindow(true)

on setWindow(computerContainer)
	tell application "Finder"
		if computerContainer then
			open computer container
		else
			open home
		end ifa
		tell the Finder window 1
			set toolbar visible to true
			set the sidebar width to 140
			if computerContainer then
				set the current view to column view
			else
				set the current view to list view
			end if
			if computerContainer then
				set the bounds to {1290, 30, 2548, 1350}
			else
				set the bounds to {15, 30, 1265, 1350}
			end if
		end tell
		set var_RightWindow to the target of the front window
	end tell
end setWindow

AppleScript can use nested set statements. For example, you can create 2 handlers (with set commands in them) and use one of them as a parameter to another handler.

But this is only necessary when the behaviour of the 2nd handler depends on the result of the first. In the OP’s example, there is no dependency when opening 2 windows. Therefore, it is enough to apply 1 handler and create 2 windows in turn. Only to shorten the code makes sense:


tell application "Finder"
	close every Finder window
	tell (get bounds of container window of desktop) to set {theWidth, theHeight} to {item 3, item 4}
	activate
	my openWindow(home, {30, 24, theWidth / 2, theHeight})
	my openWindow(computer container, {theWidth / 2 + 4, 24, theWidth, theHeight})
end tell

on openWindow(target, theBounds)
	tell application "Finder"
		open |target|
		tell the Finder window 1
			set toolbar visible to true
			set the sidebar width to 140
			set the current view to list view
			set the bounds to theBounds
		end tell
		set varWindow to the target of the front window
	end tell
end openWindow

NESTED SETTING EXAMPLE (set to set):

on run
	set onRunHanlersValue to my setA() -- set onRunHanlersValue to setting of a
end run

on setA()
	set a to 2
end setA

LITTLE MORE COMPLICATED NESTED SETTING EXAMPLE (set to set of set):


on run
	set onRunHandlersValue to my callHandler(my calledHandler())
end run

on callHandler(theHandler)
	set callHandlersValue to theHandler
end callHandler

on calledHandler()
	set a to 2
end calledHandler

NESTED SETTING EXAMPLE USING SCRIPT OBJECT (set to set to set):


on run
	set theResult to my callHandler()
end run

on callHandler()
	script calledHandler
		on calledHandler()
			set a to 2
		end calledHandler
	end script
	set callHandlersValue to calledHandler's calledHandler()
end callHandler

Last example: set to set to set to set :lol: :

on run
	set onRunHandlersValue to my callHandler(my calledHandler())
end run

on callHandler(theHandler)
	set callHandlersValue to theHandler
end callHandler

on calledHandler()
	script setA
		on setA()
			set a to 2
		end setA
	end script
	set calledHandlersValue to setA's setA()
end calledHandler