Originally posted by 3R1C:
<STRONG>I have installed the new dev tools and have been playing with applescript studio. I am working on a scriptlet that will show the user who is connected to their machine and allow the user to boot them. as it is i have the scriptlet do a ps -au | grep -v grep | grep tcsh
and display the output in the window. the user can then select cut and past the pid of the user to be booted into a text field. then when the boot button is pressed, the desired user is "booted"
heres where i need help:
1) how do i make it so that the ps -au | grep -v grep | grep tcsh command is done every minute so that the display of connected users is "fresh"? right now i have button to "refresh" the display
2)how do i route the separate elements from the output of ps -au | grep -v grep | grep tcsh into a table?
3) how do i then allow the user to select rows in the table so that when the "boot" button is clicked, i can kill their pid?</STRONG>
I've put together a Studio project that does most everything that you are looking for. If you'd like me to email it to you let me know here in the forum.
To answer your questions then:
1. Add an 'idle' event handler to the application object ("File's Owner" in the 'MainMenu.nib'. Then in your 'idle' event handler call your handler to do the processing and then do a 'return 60' which will cause the idle handler to be called every 60 seconds.
2. I went with a strategy of just breaking up the return for 'do shell script' into separate strings by doing a 'every paragraph' of the result of calling 'do shell script' and then putting each line into a separate data row in the data source for the table view.
3. You can call 'selected rows of table view 1 of scroll view 1 of window 1' to get the indexes of the selected rows. Then you can get the data row with the given index from the data source. I then get the contents of the cell and ask it for the second word.
One thing of note, I didn't attempt to parse the output of 'do shell script' into it's individual elements. I leave that as an exercise.
Here is the script for the 'Booter' project: (Hopefully it looks correct when I post this....)
<blockquote><font size=1 face="Geneva, Verdana, Arial">code:</font><hr><pre>& amp;lt;font size=1 face=courier>
-- Booter.applescript
(* ==== Properties ==== *)
property processDataSource : null
(* ==== Event Handlers ==== *)
on clicked theObject
if name of theObject is "refresh" then
-- Update the processes
updateProcesses()
else if name of theObject is "boot" then
-- Display an alert if there are any rows selected
if (count of selected rows of table view "process" of scroll view "process" of window of theObject) > 0 then
display alert "Boot Users" as critical message "Are you sure you want to boot the seleced users?" default button "Boot" alternate button "Cancel" attached to (window of theObject)
end if
end if
end clicked
on will open theObject
-- Set up the processDataSource so that the rest will be simpler
set processDataSource to data source of table view "process" of scroll view "process" of theObject
-- Here we will add the data columns to the data source of the process table view
tell processDataSource
make new data column at the end of the data columns with properties {name:"process"}
end tell
end will open
on idle theObject
-- Update the processes table
updateProcesses()
-- Update the processes every 60 seconds
return 60
end idle
on alert ended theObject with reply withReply
if button returned of withReply is "Boot" then
killProcesses()
end if
end alert ended
(* ==== Handlers ==== *)
on killProcesses()
-- Get a list of the selected rows (indicies)
set theRows to selected rows of table view "process" of scroll view "process" of window "main"
-- Go through each process, killing it
repeat with theIndex in theRows
-- Get the data row from the data source
set theRow to data row theIndex of processDataSource
-- Get the pid of the process
set theProcess to contents of data cell "process" of theRow
set pid to second word of theProcess
-- Kill the process
do shell script ("kill -1 " & pid)
delay 2
end repeat
-- Update the processes
updateProcesses()
end killProcesses
on updateProcesses()
-- Remove all of the current rows in the data source
tell processDataSource to delete every data row
try
-- Get the output of the 'ps' as separate lines
set processLines to every paragraph of (do shell script "ps -au | grep -v grep | grep tcsh")
-- Add each of the lines of the output to the data source
repeat with theLine in processLines
-- Make a new data row
set theRow to make new data row at the end of the data rows of processDataSource
set contents of data cell "process" of theRow to theLine
end repeat
end try
end updateProcesses
</font></pre><hr></blockquote>[/QB][/QUOTE]