script idling

I’m trying to figure out how to idle (or something similar) an applescript in applescript studio. The applescript that my application is based on would idle and run every minute (i think). The script is set to activate when I press a button from Interface builder. I’ve tried this as the beginning and end of my script:

on idle theObject
	--my script
	return (60)
end idle

When I try this I get error 1708. All I need is a way to run the script every few minutes. So if theres a way to do it within the script or with other parts of Xcode I would really appreciate the help.

Note:
I also tried this and it would run the app normally but wouldn’t idle the script.

on clicked theObject
	return idle
end clicked

on idle
	--my script
	return 60
end idle

Hey boohickey11,

Had this same problem a while back. The problem is that you have to give your app the idler in interface builder.
Open your project’s mainmenu.nib file and select the “File’s Owner” object (in the window that has all you windows and menus). Open up the inspector, choose “applescript” from the pulldown, nad check “on idle” (under “application”). Screenhots available upon request.

I tried finding the “on idle” checkbox under “application” in my nib “File’s Owner” but I could’nt find it. I looked at the menu for the app window and all the objects but none of them had the “application” directory in the applescript menu. The objects had Action, Drag and Drop, Key, Mouse, Nib, View. The window itself had Nib, Panel, and Window.

I’m using OS 10.3.9
Xcode 1.5

Hi boohickey,

I’ve used some idler handler before so I think I can give you some general idea/format of an idler and you can go from there.

You should put your IB object in the “on clicked theObject” then activate the idler. If a certain time hasn’t been reached then keep idling (in this case, give it the total idle time of 60 if you wish to idle for a minute.

The generalized coding could be like below.

property countdown_boohoo: false
property idleTime: 60
property timeToActivate: 0

global currDate

on clicked theObject
set currDate to current date (gets the current time)
do other tasks if any?
set timetoActivate to (current date + idleTime) --this is your idle time of 60 seconds from current time in date format
set countdown_boohoo to true (activates the countdown)
end

Note: the idler below will return 1 second on each idle; when it has idled for a minute you can then give it the task you want the script to do; the idler will work only if related to the computer’s clock, that’s why the current time and the timetoActivate.

on idle
if currDate is not greater than timetoActivate then
do something (if you want to run some other tasks)
return 1(continue idling by a second)
else
if currDate is greater than timetoActivate then
do something when the minute is over
set countdown_boohoo to false
end if
end if
end

I realize you want the idler to activate every minute and then do something in your script. I am sure you can figure it out from the generalized codes that I gave above. It can be done for sure.

Hope this gets you going. Good luck!

archseed :slight_smile:

boohickey11, you need to select the File’s Owner in the nib window. Application will be first in the AppleScript palette. Idle is the second option in the list of Application handlers available.

I found the nib file’s owner checkboxes and ckecked idle. It then gave me an applescript error 1708 so I changed my on Clicked theObject to on Idle theObject. It is idling now but it idles as soon as I start the application where as I want it to start idling when I click a button in my IB window.

property shouldIdle : false

on clicked theObject
	set shouldIdle to true
	-- if you want it to toggle, use:
	-- set shouldIdle to not shouldIdle
end clicked

on idle
	if shouldIdle then
		-- do what you want to do!
	else
		-- do something else
	end
end idle

I zipped my project file and put it on my website. heres the link.
http://www.geocities.com/boohickey11/ApScriptApp

This will help y’all figure out how I want my app to work.

Hi,

Correction on my earlier post.

It is indeed proper to create the idler handler by activating the “idler” in the IB as suggested by other posters in this thread – even if idling is tied up with the computer clock, which actually requires more codes.

Much regret :expressionless: I did learn something in this thread.

archseed :slight_smile:

I still can’t get idle to work. I have a special situation where my main nib is just the menu, and separate nibs (for the windows) are then loaded for panther or for tiger. the main nib’s file owner controls “will finish launching” and “lauched”. But when I also enable “idle” there, it does not appear to connect properly to the idle handler.

If I try to enable idle under the other nibs that load later, the app never finishes launching and I just get an applescript error -2016 or something like that.

Hi boohickey,

I think there might be a way to do this. When you turn the idle on, did you want it to continue indefinately or be able to stop it somehow? I was thinking that the button could be a togler to turn the idle on and off.

Later,

Here’s what I came up with:

property idle_off : true

on idle theObject
if idle_off then
quit
else
beep 1
end if
return 2
end idle

on clicked theObject
set idle_off to not idle_off
end clicked

on quit
if not idle_off then
idle
end if
end quit

Note that this is not the proper way to use a quit handler and maybe any handler would do in AS Studio. I haven’t tested it much, but I think it’s still idling, but nothing is being done. Still testing how to make the idle stop.

Editted:
Yeah it is still idling, so doing this would be the same as the script above:

property idle_off : true

on idle theObject
if idle_off then
– do nothing
else
beep 1
end if
return 2
end idle

on clicked theObject
set idle_off to not idle_off
end clicked

Causing an error might turn it off or …

later,

Think I got it. Instead of the quit handler as in AppleScript, the on should quit handler seems to work in turning of the idle. Something like this:

property init_run : true

on idle theObject
if init_run then
quit
else
try
display dialog “Press cancel to quit.”
on error
quit
end try
end if
return 2
end idle

on clicked theObject
set init_run to false
idle
end clicked

on should quit theObject
return not init_run
end should quit

I’m not sure if you wanted a toggler, so this one doesn’t toggle. Still testing it out.

gl,