I am a newbie to AppleScript. I have been writing a piece of code to
read the IP address from a saved network configuration. It works for
the first few times and then throws error -10011 (In Transaction).
After a restart it works good again. Can someone help me with this? I
need to automate some stuff and can’t afford to restart every few
minutes.
TIA,
Espan
Model: iMac G4
AppleScript: 1.6
Browser: Internet Explorer 5.0
Operating System: Mac OS 9.2.x
The basic sequence is: open the database, get the information you want, close the database. While the database is open, it’s best to use a ‘try’ block so that any errors won’t stop the script before the database is closed again.
tell application "Network Setup Scripting"
open database
try
set TCPIPconfig to the first TCPIP v4 configuration whose active is true
set IPaddress to TCPIPconfig's IP address
end try
close database
end tell
ipAddress --> "123.456.78.90"
How does that compare with what you’re doing already?
I cant believe what is happening but since this morning my script has been behaving good. Since the problem is not consistent I am not able to figure
out the reason. This is my script.
tell application "Network Setup Scripting"
activate
set res to ""
try
try
open database
end try
begin transaction
set res to IP address of TCPIP v4 configuration "MyConfig"
end transaction
on error errmsg number errnum
tell application "Finder"
display dialog ("Error : " & errmsg & " " & errnum);
end tell
return res
try
close database
end try
end try
tell application "Finder"
activate (beep)
display dialog (res)
end tell
end tell
You don’t need a transaction if you’re only reading the values. When you do use one, you should arrange the ‘try’ blocks to ensure that the transaction’s ended in the event of an error. Also, it probably doesn’t make any difference here, but it’s generally considered a good idea not to nest ‘tell’ blocks to different applications, if it can be avoided.
I don’t know why your script doesn’t always work, but the version below is (hopefully) a bit more robust. If anything does go wrong, it should tidy up enough afterwards to save you having to restart.
tell application "Network Setup Scripting"
launch
set res to ""
try
open database
set res to IP address of TCPIP v4 configuration "MyConfig"
on error errmsg number errnum
my displayMessage("Error : " & errmsg & " " & errnum)
end try
try
close database
end try
quit
end tell
displayMessage(res)
-- A handler to display a dialog in whatever app is frontmost at the time.
-- There's no "Cancel" button, to ensure that the script doesn't stop before tidying up.
on displayMessage(msg)
tell application (path to frontmost application as string)
display dialog msg buttons {"OK"} default button 1
end tell
end displayMessage