on error produces error

I am trying to make an AppleScript that will alert me when the mouse is unplugged.

I am using OS X 10.2.8 and AppleScript 1.9.

tuggboat666 showed me how to do it, but when I run the script

set the_script to “ioreg -l | grep ‘"HIDDefaultBehavior" = "Mouse"’”
do shell script the_script
–do stuff you want when the mouse is plugged in (I changed this to Beep 2)
on error
–do stuff you want when the mouse is unplugged (I changed this to Beep 5)

it gives me a Synax Error - Expected function name, command name or function name but found “error”.

The script worked for Adam Bell on a dual-core G5/2.3 10.4.5

When I run just the

set the_script to “ioreg -l | grep ‘"HIDDefaultBehavior" = "Mouse"’”
do shell script the_script

and the mouse is plugged in it gives me the result that it found the ioreg command. If it is unplugged it gives me an Execution Error - and error of type one has occurred. I know it works, but my AppleScript doesn’t like the on error part of it.

I would like for nothing to happen if the mouse is plugged in and for it to beep 10 times if it isn’t plugged in.

Anyone know what I can do?

Thanks,
SWeeT

Sorry,

I forgot one line that is needed for the ‘on error’ to work. The correct syntax would be:

set the_script to “ioreg -l | grep ‘"HIDDefaultBehavior" = "Mouse"’”
try
do shell script the_script
Beep 2
on error
Beep 5

This instructs the system to 'try’to execute the shell script. If it works, all is well, if the shell script throws an error, the statements under ‘on error’ are executed.

Andy

Model: G5 Dual 2.7 GHz
AppleScript: 2.0 (v43.1)
Browser: Safari 312
Operating System: Mac OS X (10.4)

I tried it on a different Mac. I work at two different places. I think the Macs are about the same. The processors are not the same speed and they don’t have the same amount of memory. All the software on them is the same.

When I run this part of the script -

set the_script to “ioreg -l | grep ‘"HIDDefaultBehavior" = "Mouse"’”
do shell script the_script

I get this error - sh: grep : command not found

I can go into Terminal and type ioreg -l | grep Mouse and it will find all the commands with Mouse in them. So, I know it works. I just get that error in AppleScript.

I will be at the other place I work at on Tuesday. The computer there gave me the same error, but then it started working.

I will try it there on Tuesday and see what happens.

Thanks for replying to my message again =)

HAHA (yes, I am excited :lol: ) thanks for helping with that!! I got it to work. Using my AppleScript, I have to put a space between the some of the lines -

set the_script to “ioreg -l | grep ‘"HIDDefaultBehavior" = "Mouse"’”

try
do shell script the_script
beep 0
on error
beep 5
end try

I am still going to need help because I don’t understand how to make it work without clicking run to make it work. I will try finding the answers and if I can’t find them, I will ask in another thread. Thanks again for all your help!!!

Connie

Connie,

When you save the applescript as an application, you can choose the option of having a startup screen or not. The startup screen is the thing that asks you if you want to run the script. So just do a ‘save as’ on your script and uncheck the box

Andy

Browser: Safari 412
Operating System: Mac OS X (10.4)

I understand that, but I don’t understand how it will run in the background. I mean… I just don’t get where I put it and how it knows to run when the computer is started. How does it know to run all the time and know when the mouse has been disconnected? Doesn’t it have to be put somewhere? Does it need more script? Sorry to be dense.

I am taking a Mac home tonight so I can work on it this weekend. It is like a big puzzle and I think about it when I am trying to sleep and while I am driving. Oh, how sad =(

Thanks for helping me. This stuff is so cool!!! :stuck_out_tongue:

Look at the example provided by Nigel Garvey here:

http://bbs.applescript.net/viewtopic.php?id=15146

You need an idle handler like so:

set the_script to “ioreg -l | grep ‘"HIDDefaultBehavior" = "Mouse"’”
on idle
try
do shell script the_script
beep 0
on error
beep 5
end try
return 30 – number of seconds to next check
end idle

this script was automatically tagged for
color coded syntax by Script to Markup Code
written by Jonathan Nathan

Browser: Safari 412
Operating System: Mac OS X (10.4)

After much thought, I decided what I want the script to do. I want the computer to alert me when the mouse has been unplugged by turning up the voulume, say something very annoying, and then log out. I got the script working for that.

set the_script to “ioreg -l | grep ‘"HIDDefaultBehavior" = "Mouse"’”

try
do shell script the_script
beep 0
on error
say “HelpMeHelpMeHelpMeHelpMe! The mouse is unplugged!!” using “Good News”
try
ignoring application responses
tell application “loginwindow” to «event aevtrlgo»
end ignoring
on error mes
display dialog "Error: " & mes giving up after 10
end try
end try

It works great, but when I add the idle handler it doesn’t work anymore.

I tried two ways.

set the_script to “ioreg -l | grep ‘"HIDDefaultBehavior" = "Mouse"’”

on idle
try
do shell script the_script
beep 0
on error
tell application “Finder” to set volume 7
say “HelpMeHelpMeHelpMeHelpMe! The Mouse is unplugged!” using “Good News”
try
ignoring application responses
tell application “loginwindow” to «event aevtrlgo»
end ignoring
on error mes
display dialog "Error: " & mes giving up after 10
end try
end try
return 30
end idle

I also tried putting the “on idle” before the “set the_script to”. Neither of them work.

Any suggestions?

Thanks,
Connie

Try putting the statement that defines the variable the_script inside the idle handler, SWeeT.

Oh yeah - you should also be able to invoke a logout with something like this:

tell application "System Events" to log out

I tried that and it wouldn’t work. I don’t know if my system is older and that is why it doesn’t work or what. Thanks for the help.

Hi sweet,

maybe you’ve tried to run the on idle script from the script editor?
An on idle script only works, when saved as stay open programm (see options in the save dialog).

And as kai said: the variable ths_script is a local variable which is not known inside the on idle loop. you can set it insid the loop - or define the variable the_script as global:

global the_script
set the_script to …

D.

global set the_script
set the_script to

That worked! Thank you sooooo much!

Now I have another problem. I saved the script as an application and checked Stay Open and Never Show Start Up boxes. Then I put the script in the applications folder. I went to System Preferences then to Login Items. I added the script and then checked the Hide box. Now it bounces on the dock. Is there a way to keep it from showing up on the dock?

I tried putting it in the System/Library/StartUpItems folder. That doesn’t work either. Where do I put it so that it will run in the background all the time?

Thanks =)
Connie

You need to convert your application to background-only, Connie. The easiest way is to download Drop Script Backgrounder. Then, in Finder, drag your app onto Backgrounder’s icon. :slight_smile:

Wow!!! Thanks for that link! The program works like a charm =)

Thanks for the help from everyone. I really appreciate the help that was given me. I would never have been able to start working on it if it hadn’t been for your help =)

Connie