How do I retrieve a value from an external handler’s return Statement?
Handler:
set text2 to "Applications"
tell application "Finder"
if folder text2 of startup disk exists then
set X to count files in folder text2 of startup disk
set text1 to "Anzahl Dateien in " & text2 & ": " & X
else
set text1 to "Kein Ordner " & text2 & " vorhanden"
end if
return text1
end tell
How do I get the returned data into a variable?
Regards
Sven
What do you mean by “external handler”? A handler in another script? A Script library?
What you have above is not really a handler. Handlers usually start with a “on” or “to” statement. Like so.
on multiplebythree(n)
set n to n * 3
return n
end multiplebythree
You would call this handler from your run handler (or any handler) like
so…
set c to 5
set c to multiplebythree(c) -- calling the handler here will return the value and set c to it
display alert "c * 3 = " & c
The return statement is the correct command to return the value of text1. But only from within a defined handler.
set theResultsOfYourHandler to Finder_Count_Applications()
-->"Anzahl Dateien in Applications: 96"
on Finder_Count_Applications()
set text2 to "Applications"
tell application "Finder"
if folder text2 of startup disk exists then
set X to count files in folder text2 of startup disk
set text1 to "Anzahl Dateien in " & text2 & ": " & X
else
set text1 to "Kein Ordner " & text2 & " vorhanden"
end if
return text1
end tell
end Finder_Count_Applications
You can alao make text2 available everywhere by declaring the property at the top like this
property text2 : “Applications”
Also side note you want to put the return at the very end. After the end tell statement.
Really get into the habit of closing out you “statements / blocks” properly.
on handlers
If then else
tell’s
repeat’s
try’s
timeouts
using considering ignoring
Script Debugger is great as it will automatically I inject these for you. Just make sure your code is balanced properly