I have made a few scipts into applications. It seems like it is tough to get them to quit without doing a force quit. I know my scripts are not professional, but I have tested this with simple ones and it does the same. Any reason for this?
do u have any repeats? thats often a reason?
Hi Ian,
That’s the problem with infinite repeat loops that have no breaks.
repeat
delay 1
end repeat
Even this script with the ‘delay’ command uses the processor most of the time. What you can do is use the unix sleep:
repeat
do shell script “sleep 1” – 1 second
end repeat
The higher the number, the less cpu intensive. Or, you can use the idle handler like I showed you in the other post. One thing about the idle handler is you can’t go less than 1.
on idle
– do something
return 1 – second
end idle
This gives up the cpu for 1 second and returns.
To quit an infitite repeat loop with no break, try pressing Command + “.”.
gl,
i do use those alot. Here is a longer script I pieced together today
property currentState : 0
property currentState1 : 0
repeat
-------------------------------------
if (keys pressed) contains "F1" then
if currentState is 0 then
tell application "System Events"
tell process "SecuritySpy"
click menu item "Main preview window" of menu "Window" of menu bar item "Window" of menu bar 1
click menu item "Camera Status window" of menu "Window" of menu bar item "Window" of menu bar 1
end tell
end tell
else
tell application "System Events"
tell process "SecuritySpy"
click menu item "Close" of menu "File" of menu bar item "File" of menu bar 1
click menu item "Close" of menu "File" of menu bar item "File" of menu bar 1
end tell
end tell
end if
set currentState to (currentState + 1) mod 2
---------------------- --------------
else
if (keys pressed) contains "F2" then
if currentState1 is 0 then
tell application "System Events"
tell process "SecuritySpy"
click menu item "Set all cameras to active mode" of menu "Control" of menu bar item "Control" of menu bar 1
end tell
end tell
else
tell application "System Events"
tell process "SecuritySpy"
click menu item "Set all cameras to passive mode" of menu "Control" of menu bar item "Control" of menu bar 1
end tell
end tell
end if
set currentState1 to (currentState1 + 1) mod 2
end if
end if
-----------------------------------
delay 0.5
end repeat
Check “AS_HotKey” here (Jon’s source code…):
http://homepage.mac.com/jonn8/as/html/misc_projects.html
With this sample code you can create a responsive Studio app, as it installs a global keystroke and simply does “nothing” while it isn’t invoked.
About repeat loops, I think “command + dot” is more responsive than “command + Q”.
About CPU, I think the best option would be and adaptation of “AS_HotKey”, or just moving all the repeat loop inside an “idle” handler, which is much more cpu-friendly and creates more responsive apps.
Oooops. Just noted this was already mentioned by kel
I originally tried this, but I cant get it wo do anything. Am I doing something wrong? do I need a repeat in this too?
on idle
if (keys pressed) contains "F7" then
display dialog "hi"
end if
return 1
end idle
I downloaded the hotkey stuff, but here is no explainaion on how to use it. Any ideas? Thanks, Ian
bonedoc,
Save the idle handler as a STAY OPEN application. It wont work in the Script Editor.
gl,
Hi bonedoc.
As kel says, an idle handler will normally only work in a stay-open script application.
Perhaps a word or two about idle handlers might help here.
Whenever a stay-open script application is not responding to incoming events, AppleScript sends it periodic idle commands, which cause the statements in the idle handler to be executed.
The default interval between these idle commands is 30 seconds. However, if an idle handler returns a positive number, that number becomes the rate (in seconds) at which the handler is called. If the handler returns a non-numeric value, the rate is not changed.
If the last statement in an idle handler returns a positive number (such as from the setting of some variable), and you don’t wish this to be used to change the rate of idle commands, return 0 at the end of the handler.
A regular script application does not receive idle commands. Once all statements in the run handler (including those in any subroutines called) have been executed, the application will quit automatically. (So if a stay-open application is required to quit at some point, without external intervention, it should include an explicit quit statement.)
Compiled scripts (whether run from a script editor, from Script Menu or from within some other application) do not receive idle commands.
Tip: If, while editing a script in Script Editor, you want to briefly test an idle handler, simply insert an idle command at the end of your run handler. (This will obviously call the idle handler just once.)
on idle
-- idle statements here - for example:
display dialog "Executing idle handler."
end idle
-- regular run statements here - for example:
display dialog "Executing run handler."
idle (* temporary test *)