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