Returning values through nested subroutines.

Hi All,

I’m working on a script to process pdf files and sort them into folders. I’m stuck with returning values from sub-routines back to the point where the sub-routine was called.

Is it possible to return values through nested sub-routines? Please advise.

Here’s a simple version of what I’m trying to achieve :

tell application "Finder"
	activate
	set mylist to {"initial script"}
	display dialog mylist giving up after 1
	
	-- Call first subroutine
	my SubRT01(mylist)
	-- Adds entry to the list & returns list
	-- Display new list
	display dialog mylist giving up after 1
	
	
end tell


-- 1st subroutine
on SubRT01(mylist)
	set Itemtwo to "SubRoutine1" as text
	set mylist to {mylist, Itemtwo}
	tell application "Finder"
		activate
		display dialog mylist as text giving up after 1
	end tell
	my SubRT02(mylist)
	delay 2
	return mylist
end SubRT01

-- 2nd (nested) subroutine
on SubRT02(mylist)
	set Itemthree to "SubRoutine2" as text
	set mylist to {mylist, Itemthree}
	tell application "Finder"
		activate
		display dialog mylist as text giving up after 1
	end tell
	
	return mylist
end SubRT02

Thanks in Advance.

Ps. My list isn’t appearing as a list either, some help on this would also be useful, I have one line of text as opposed to separate items!

Hi. Yes, it’s possible to nest multiple subroutines together, however, a subroutine—or “handler” in AS parlance—should be more than just code that’s isolated from the body; each handler should actually serve a discrete function, and it helps if they’re named something describing that function. Your handlers identically concatenate a list that’s been coerced to text, which flattens it into one line.

Below is a functional nested handler example for you to study. Note that “return” is implicit in the last passed value.

set aList to {3, 2, 4}
set anotherList to {1, 0, 5}

my sort(my append(aList, anotherList)) -->{"0", "1", "2", "3", "4", "5"}

———————————————————————————————————————————————————————
on append(firstItem, secondItem)
	firstItem & secondItem
end append

on sort(this)
	set AppleScript's text item delimiters to linefeed
	(do shell script "echo " & (this as text)'s quoted form & " | sort -g")'s paragraphs
end sort


When using subroutines for getting info i use properties instead.


property myList : {}

tell application "Finder"
    activate
    set myList to {"initial script"}
    display dialog myList giving up after 1
    
    -- Call first subroutine
    my SubRT01(myList)
    -- Adds entry to the list & returns list
    -- Display new list
    display dialog myList giving up after 1
    
    
end tell


-- 1st subroutine
on SubRT01(myList)
    set Itemtwo to "SubRoutine1" as text
    set myList to {myList, Itemtwo}
    tell application "Finder"
        activate
        display dialog myList as text giving up after 1
    end tell
    my SubRT02(myList)
    delay 2
end SubRT01

-- 2nd (nested) subroutine
on SubRT02(myList)
    set Itemthree to "SubRoutine2" as text
    set myList to {myList, Itemthree}
    tell application "Finder"
        activate
        display dialog myList as text giving up after 1
    end tell
    
end SubRT02

When returning values from handlers the calling code should catch them:

-- 1st subroutine
on SubRT01(mylist)
	set Itemtwo to "SubRoutine1" as text
	set mylist to {mylist, Itemtwo}
	tell application "Finder"
		activate
		display dialog mylist as text giving up after 1
	end tell
	my SubRT02(mylist)
	delay 2
	return mylist
end SubRT01

should become

-- 1st subroutine
on SubRT01(mylist)
	set Itemtwo to "SubRoutine1" as text
	set mylist to {mylist, Itemtwo}
	tell application "Finder"
		activate
		display dialog mylist as text giving up after 1
	end tell
	set mylist to my SubRT02(mylist)   ##    EDITED
	delay 2
	return mylist
end SubRT01

As Marc said, a return is implicit when you want to return the result of the last action of the handler. You can remove the last 2 lines of the unmodified handler 1, and it will return the value that handler 2 returned.
But 6 months later you’ll have forgotten what the darn thing returns - so being explicit rewards itself.