Wednesday, March 20, 2019

#1 2019-03-10 06:38:01 pm

Matt McGregor
Member
From:: Australia
Registered: 2002-12-19
Posts: 67

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 :

Applescript:

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!


G5 MacPro 5.1 : 8GB/1TB/HD 5870
Mac OS : Lion 10.7.5
AppleScript : 2.2.1


Filed under: routine return

Offline

 

#2 2019-03-10 08:08:16 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 855

Re: Returning values through nested subroutines.

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.

Applescript:

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

Offline

 

#3 2019-03-11 08:03:12 am

FredrikM
Member
From:: Förslöv, Sweden
Registered: 2019-02-19
Posts: 15

Re: Returning values through nested subroutines.

When using subroutines for getting info i use properties instead.

Applescript:


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

Offline

 

#4 2019-03-11 09:20:25 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 553

Re: Returning values through nested subroutines.

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

Applescript:

-- 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

Applescript:

-- 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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)