Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > help with my first as-s scriptlet.

help with my first as-s scriptlet.
Thread Tools
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Dec 14, 2001, 04:49 AM
 
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?
3R1C
     
Fresh-Faced Recruit
Join Date: Dec 2001
Status: Offline
Reply With Quote
Dec 15, 2001, 02:27 AM
 
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....)

&lt;blockquote&gt;&lt;font size=1 face="Geneva, Verdana, Arial"&gt;code:&lt;/font&gt;&lt;hr&gt;&lt;pre&gt;& amp;lt;font size=1 face=courier&gt;
-- 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) &gt; 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

&lt;/font&gt;&lt;/pre&gt;&lt;hr&gt;&lt;/blockquote&gt;[/QB][/QUOTE]
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Dec 15, 2001, 03:18 AM
 
cool! ill have to chew on what you've given me for a while. I plan to have columns in my table for

user tty from login@ pid %cpu %mem idle what

i plan to get this information from the 'w' command and from 'ps au | grep -v grep | grep tcsh'

eventually i want to have the 'write' command send a message to a selected user.

i've been doing all of this via shell scripts previous to now, but as-s has opened a new vista for me. tweaking this dumb little scriptlet is FUN

i propose that we open source booter, or as i call it 'das_boot' here as a way for me and other newbies to work on a tangible ongoing work in progress.

anyway, after i understand what you have given me, ill post back my progress.

p.s. could you send me that project please? i need to catch up...

xaos01@vibepusher.com

[ 12-15-2001: Message edited by: 3R1C ]
3R1C
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Dec 15, 2001, 03:43 AM
 
how do include a 'padlock' on das_boot/booter? as it is i am doing this for my kill pid:
do shell script "echo 'the password' | sudo -S kill -9 "

this sucks for obvious reasons.

best case scenario-
there is a little padlock button on booter. if it is clicked, a sheet slides down and asks for password.
3R1C
     
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Dec 15, 2001, 07:50 AM
 
let me just add two things to this, i've done pretty much the same thing in cocoa and i ran across two problems i'd like to save you:

when using ps, don't use "-au", but something with "ww", i use "aulwwx", if you don't use the "ww", process names will be truncated at a (non existing) line width.

i have no clue about tables in applescript, in cocoa i ran across the problem that i separated the columns by spaces (obvious), but that lead to the problem that process names also got split, i.e. "internet explorer" is separated by space, only "internet" ended up in the correct column. just keep on glueing the last entries in your array until the number of items is equal to the number of columns you see in the terminal.

i hope you get what i mean.

if you're interested in the cocoa thing, just mail me at post@sebastian-krauss.de.
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Dec 18, 2001, 05:55 PM
 
Das boot is coming along nicely. I understand all the information you have given me tim.

How do i cause a specific row in my generated table to be selected? i know the user can click a row and select it, but i want to have a specific row default to selected even if the user hasnt selected one yet. this will allow me to select a given row after i refresh my table. you have been very helpfull tim here, and on the applescript studio list. you are what causes me to use an apple.
3R1C
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:04 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2