Wednesday, December 12, 2018

#1 2018-11-09 02:34:30 am

Krayframe
Member
Registered: 2018-06-12
Posts: 5

Returning Values from Handler

Hi

Trying to find out why my handler doesn't add "Header-Blank" to the text string


Applescript:

set mySectionHeaderPref to "all blank"
set myBigOrderList to "a
b
c
d
"

--
--
addHeader(mySectionHeaderPref, myBigOrderList)
--
--
set myBigOrderList to myBigOrderList & "End"
--
--
on addHeader(mySectionHeaderPref, myBigOrderList)
   local mySectionHeaderPref, myBigOrderList
   --
   if mySectionHeaderPref is equal to "all blank" then
       set myBigOrderList to myBigOrderList & "Header-Blank" & linefeed
   end if
end addHeader

Offline

 

#2 2018-11-09 03:24:18 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11526
Website

Re: Returning Values from Handler

It a scope question and the answer is actually in your code.

Explicit
 

Applescript:

local mySectionHeaderPref, myBigOrderList

– which is redundant by the way – declares myBigOrderList as (new) local variable in the scope of the handler which is different from the variable myBigOrderList in the implicit run handler.

You have a few options:

1) Remove the second parameter and declare myBigOrderList in the handler as global.

Applescript:

on addHeader(sectionHeaderPref)
   global myBigOrderList
   
   if sectionHeaderPref is equal to "all blank" then
       set myBigOrderList to myBigOrderList & "Header-Blank" & linefeed
   end if
end addHeader

2) Declare the outer myBigOrderList as property

Applescript:

property myBigOrderList : "a
b
c
d
"

...

on addHeader(sectionHeaderPref)
   if sectionHeaderPref is equal to "all blank" then
       set myBigOrderList to myBigOrderList & "Header-Blank" & linefeed
   end if
end addHeader

3) Return the value and assign it back.

Applescript:

set myBigOrderList to addHeader(mySectionHeaderPref, myBigOrderList)

...


on addHeader(mySectionHeaderPref, myBigOrderList)
-- in the handler myBigOrderList is a local variable, it's a different object as the variable in the run handler
   if mySectionHeaderPref is equal to "all blank" then
       set myBigOrderList to myBigOrderList & "Header-Blank" & linefeed
   end if
   return myBigOrderList
end addHeader

Last edited by StefanK (2018-11-09 03:28:03 am)


regards

Stefan

Offline

 

#3 2018-11-13 03:39:09 am

Krayframe
Member
Registered: 2018-06-12
Posts: 5

Re: Returning Values from Handler

thank you for your reply and explanation. smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)