How can I pass a variable to an Idle block?

Hi-

I’m working on a stay-open script that will ask the user for a number, and then stay open and monitor a folder named with that number on a server . Something like this:

set myNum to text returned of (display dialog "Please enter your Number:" default answer "" with icon note)
set serverFolder to alias ("The Server:" & myNum)
on idle
if serverFolder contains something then do something
return 1
end idle

The problem is I get an error saying “The variable serverFolder is not defined.” If I place the “set…” lines within the idle block, it works, but of course it’s going to keep asking to enter the number every time it cycles around.

I also tried to pass the variable to the idle similar to a subroutine…

on idle(serverFolder)
if serverFolder contains something then do something
return 1
end idle

…but got another error, “<> doesn’t match the parameter {serverFolder} for idle.”

I’d like the script to ask for the number once when it’s initially launched, and then stay open and monitor the server folder like normal.

Thanks 8)

If you put it within the idle handler like this, does it work? You may need to initialize myNum prior to the idle handler (set myNum to “”).

if myNum is "" then
set myNum to text returned of (display dialog "Please enter your Number:" default answer "" with icon note)
end if

– Rob

serverFolder and myNum should be defined as globals if you are going to use them in the idle handler.

add these 2 lines at the top of the script


global serverFolder
global myNum

HTH -john

Hi :slight_smile:
Here another way :

idle "My string"

on idle Var
	display dialog Var with icon 1
end idle

:wink: