i’d say i’m quite a newbie with applescript still. i wrote this script to get the network data in and out from activity monitor and tell me when the total reaches a certain value. everything seems to be going fine except that whenever the script rechecks the values, it will switch to activity monitor window and then to the open script window. i can foresee this being irritating as i’m planning on having the script open almost 24/7. would anyone be kind enough to advise me on what to do? thanks in advance.
this is what i have so far.
script x
activate application "Activity Monitor"
tell application "System Events"
tell process "Activity Monitor"
-- GUI Scripting statements:
click radio button "Network" of tab group 1 of window "Activity Monitor"
get value of static text 8 of tab group 1 of window "Activity Monitor"
copy result as string to data_in_text
get value of static text 4 of tab group 1 of window "Activity Monitor"
copy result as string to data_out_text
end tell
end tell
tell application "System Events"
tell process "Script Editor"
set frontmost to true
end tell
end tell
-- Making data in into a number
set pathdl to " "
set char to ""
set oldlength to 1
set data_in_num to ""
global data_in_num
set revdata_in_num to ""
repeat until char is pathdl
set char to character oldlength of data_in_text
set revdata_in_num to revdata_in_num & char
set oldlength to oldlength + 1
end repeat
set newlength to (length of revdata_in_num) - 1
repeat with j from 1 to newlength by 1
set char to character j of revdata_in_num
set data_in_num to data_in_num & char
end repeat
set data_in_num to data_in_num as number
-- Making data out into a number
set pathdl to " "
set char to ""
set oldlength to 1
set data_out_num to ""
global data_out_num
set revdata_out_num to ""
repeat until char is pathdl
set char to character oldlength of data_out_text
set revdata_out_num to revdata_out_num & char
set oldlength to oldlength + 1
end repeat
set newlength to (length of revdata_out_num) - 1
repeat with j from 1 to newlength by 1
set char to character j of revdata_out_num
set data_out_num to data_out_num & char
end repeat
set data_out_num to data_out_num as number
end script
run x
-- repeat calculations until total reaches desired value
repeat
run x
data_in_num + data_out_num
if result is greater than or equal to 4.5 then
display dialog "Data input = " & data_in_num & " GB" & ¬
return & "Data output = " & data_out_num & " GB" & ¬
return & "Total network traffic = " & (data_in_num + data_out_num) & ¬
" GB" buttons {"Change IP address", "Never mind"} default button 1 with icon 1
copy result as list to {button_returned}
if button_returned is "Change IP address" then
-- change ip in system prefs command
tell application "Finder"
open file "IP change" in folder "Desktop" in folder "faizalfarokh" in folder "Users" in disk 1
end tell
exit repeat
end if
end if
delay 30
end repeat
Model: iBook G4
AppleScript: v2.1 (80)
Browser: Safari v2.0 (412)
Operating System: Mac OS X (10.4)