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 > Scripting the Terminal and System Preferences

Scripting the Terminal and System Preferences
Thread Tools
wclarkson
Dedicated MacNNer
Join Date: Mar 2001
Location: New Orleans
Status: Offline
Reply With Quote
Jan 24, 2002, 05:04 PM
 
MacNN Developers:

I am Will Clarkson. I released the Airport Software Base Station Tutorial this past week. Although the program has been successful. There are ways in which to improve. I would like to applescript some of it. AS is easy to use, and it is even easier to understand. So, here goes nothing.

I am interested in an AppleScript that would allow me to open the system preferences, and the select a certain tab, network preferably.

I am also interested in learning how to script the terminal. I think I may have to actually open the terminal as I would be issuing multiple commands. I would have to login as root, then open pico. Modify /etc/hostconfig, and then exit. Once I can insert text into the terminal, I should be successful.

I would appreciate your help in this endeavor. Thanks in advance.
G4 Cube Owner :-)
     
absmiths
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Jan 25, 2002, 02:12 PM
 
My sense is that VI would be easier to script than pico. You could do it without having to paste text. You could create the command like this:

echo "interactive-editor-commands" | vi /etc/hostconfig

Pretty nasty. Pico would probably work too as long as it has support to read user commands from stdin the way that vi does. If you have to rely on control codes then I would definitely not use it.
     
OS X Fiend
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Apple.com
Status: Offline
Reply With Quote
Jan 25, 2002, 05:54 PM
 
I have gotten the apps to open.

tell application "Finder"
open application "System Preferences"
end tell

But...I get the following error when I run them.

"System Preferences got an error: Can't Continue open. (-1708)"

How can I block this error. It opens, so I know it works.

Thanks
iBook, iMac, 10.1.2

Apple.com
     
Ibson
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jan 25, 2002, 08:15 PM
 
To open System Prefs,
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>tell application <font color = red>"System Preferences"</font> to activate</font>[/code]
is a lot easier than telling the Finder to open the app. To open a pref pane using AppleScript just use:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>tell application <font color = red>"Finder"</font> to open file <font color = red>"Network.prefPane"</font> of folder <font color = red>"PreferencePanes"</font> of folder <font color = red>"Library"</font> of folder <font color = red>"System"</font> of startup disk</font>[/code]
to open the Network pref pane. If you want to edit /etc/hostconfig using the terminal I'd just use "do shell script..." and use grep to edit it--just read the *long* man page . To learn how to script the Terminal, just open its dictionary, everything is documented in there. However, I think the easiest way to edit /etc/hostconfig is to open it with AppleScript and do any edits using AppleScript.

[ 01-25-2002: Message edited by: Ibson ]
     
wclarkson  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: New Orleans
Status: Offline
Reply With Quote
Jan 25, 2002, 08:30 PM
 
Thanks for your help.

I am trying to get applescript to insert multiple commands into the terminal.

I would like it to do something like

tell application "Terminal"
do script "sudo -s"
display dialog "Enter Administrator Password"
do script of current window ( how do I specify top window?) " pico /etc/hostconfig"
display dialog "Change IPForwarding = -NO- to IPForwarding = -YES-"
do script of current window type control o, return, control x
do script of current window "exit"
do script of current window "exit"

tell application "Terminal"
quit ( or exit)
end tell

end tell

would something like this work. All I would need to do is find out how to specify which window I insert commands into. Like window 1, window top. etc...

Thanks
G4 Cube Owner :-)
     
Ibson
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jan 25, 2002, 08:53 PM
 
If you simply want to change IPForwarding to -YES-, I'd either use grep, or even more simply, open the /etc/hostconfig using AppleScript, create a list from the file using the unix newline character as the delimiter, then search for IPForwarding and change the line to -YES- or -NO-, then re-write the file. It is a lot easier than using the Terminal. Or, another option would be to include a Perl script in your AS application's bundle, and run that by using "do shell script..."
     
wclarkson  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: New Orleans
Status: Offline
Reply With Quote
Jan 25, 2002, 09:05 PM
 
Seeing as how I am a complete novice at AS. I would appreciate your help in this.

If you are unwilling, are there any good sites, tutorials or books that I could use?
G4 Cube Owner :-)
     
wclarkson  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: New Orleans
Status: Offline
Reply With Quote
Jan 25, 2002, 09:21 PM
 
On second thought,

the user can just have dialog boxes that walk them through it.

How do I delay a dialog box until a user inputs information into the terminal. Say, on a return key I activate a new dialog box. Is this possible?
G4 Cube Owner :-)
     
Ibson
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jan 25, 2002, 09:55 PM
 
I'd be happy to help out. I don't know how to display a dialog when a user enters data into the terminal. If you basically outlined what you needed to do, I could see if I could write an AppleScript.
     
wclarkson  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: New Orleans
Status: Offline
Reply With Quote
Jan 25, 2002, 10:08 PM
 
I want the user to be sent to /etc/hostconfig. I am using the line

tell application "Terminal"
do script with command "sudo pico /etc/hostconfig"
display dialog "Enter Admin password into terminal"
end tell

then, I want the user to scroll down to IPForwarding. and then Change -NO- to -YES-. From your previous post, I assume you can do this with a special tool. If you could get it all screamlined it would be greatly appreciated.

Then, the user holds control and o to save, then presses return. Then the user holds control and x to exit.

Lastly, the user types exit 2 times to exit root, and then the terminal.

lastly, the code says

tell terminal........... quit, end tell.

Is this too much to ask?

Thanks a ton!!!
G4 Cube Owner :-)
     
Ibson
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jan 25, 2002, 10:24 PM
 
That would be quite easy to do as you have described it, but I am unclear as to whether you want the user to enter the commands themselves, or to have the file automatically changed. If you want the user to do it, the script you've written should be fine, otherwise if you want it done automatically I could write a simple script like:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set hostConfig to open for access file <font color = red>"hostconfig"</font> of folder <font color = red>"etc"</font> of startup disk with write permission
set eof of hostConfig to <font color = blue>0</font>
set tid to my text item delimiters
set my text item delimiters to ascii character (unix newline # [<font color = blue>27</font> is it?]--I've forgotten)
set theList to text items <font color = blue>1</font> thru <font color = blue>-1</font> of hostConfig
repeat with thisLine from <font color = blue>1</font> to (theList count)
if item thisLine of theList contains <font color = red>"IPForwarding"</font> then
set item thisLine of theList to <font color = red>"IPForwarding=-YES-"</font>
exit repeat
end if
end repeat
set theFinalFile to theList as text
write theFinalFile to hostConfig starting at eof
close access hostConfig</font>[/code]
or as I've said you could just use grep, which I'm sure is simpler--I'm investigating the man pages now. If there's any UNIX geek who knows more please shed light on this. I know BBEdit includes a nice manual on grep. I would most of all be very happy to help with the interface of your program(s) as UI design is my real interest, I find programming is just a natural extension of it--this is quite the opposite of a regular programmer!
     
OS X Fiend
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Apple.com
Status: Offline
Reply With Quote
Jan 25, 2002, 10:37 PM
 
on clicked onObject

set hostconfig to open for access file "hostconfig" of folder "etc" of startup disk with write permission
et of of hostConfig to 0
set tid to my text item delimiters
set my text item delimiters to ascii character (unix newline # [27 is it?]--I've forgotten)
set theList to text items 1 thru -1 of hostConfig
repeat with thisLine from 1 to (theList count)
if item thisLine of theList contains "IPForwarding" then
set item thisLine of theList to "IPForwarding=-YES-"
exit repeat
end if
end repeat
set theFinalFile to theList as text
write theFinalFile to hostConfig starting at eof
close access hostConfig

end tell

end clicked

When I try to compile I get the following error:

terminal3.applescript:3: Expected end of line, etc. but found ? (-2741)

Looks like an incompatibility

Thanks for your help.
iBook, iMac, 10.1.2

Apple.com
     
Ibson
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jan 25, 2002, 10:40 PM
 
It's because of I forgot what the UNIX new line character is. Just use
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>ascii character <font color = blue>27</font></font>[/code]. If that's not right, just replace it with the correct new line character. I'm sure some of the code is a little messy, I just wrote it from memory.
     
wclarkson  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: New Orleans
Status: Offline
Reply With Quote
Jan 25, 2002, 10:44 PM
 
The error occurs on the first line. It expects and end of line. And it can't find it.

It is the first line that gives the error.
G4 Cube Owner :-)
     
Ibson
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jan 25, 2002, 10:59 PM
 
Wrapping the commands in a tell app "Finder" block and putting "file ... of startup disk" in brackets fixes the error, but it returns a new error. I suspect it's because AppleScript *still* can't read invisible files. Ahhh! I thought Apple might have fixed that by now, but I'm sure that's considered a feature. When you run the command, you find a blank "hostconfig" file on the root of the startup disk. Putting
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>file <font color = red>"hostconfig"</font> of folder <font color = red>"etc"</font> of folder <font color = red>"private"</font> of startup disk</font>[/code]
doesn't fix the problem either. It seems we'll have to get acquainted with grep.
     
acur128
Junior Member
Join Date: May 2001
Status: Offline
Reply With Quote
Jan 26, 2002, 02:41 AM
 
Seems AppleScript would rather not deal with invisible files/folders if at all possible... generating an alias from an invisible file or folder will work sometimes - an invisible file can be selected from an open file dialog, but coercing a string for an invisible file to an alias generally fails. And the Finder will never see it.
It usually seems best to use the shell to deal with invisible files...
The text from /etc/hostconfig could be read into the script & edited within the script using "offset"; but writing directly to /etc/hostconfig appears to fail using either read & write commands or shell commands.
What seems to work is writing the edited hostconfig string to a file (anywhere) named "hostconfig" & copying that file to /etc. This looks to work:
(edited to read the hostconfig data with the AppleScript "read" command instead of the shell "cat" command)
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>-- make backup & template dirs... ~/Documents seems a convenient place
do shell script <font color = red>"mkdir ~/Documents/backup_hostconfig;mkdir ~/Documents/backup_hostconfig/original;mkdir ~/Documents/backup_hostconfig/modified"</font>
-- backup original to backup dir
do shell script <font color = red>"cp /etc/hostconfig ~/Documents/backup_hostconfig/original"</font>
-- read hostconfig & generate string for new hostconfig
tell application <font color = red>"Finder"</font> to set hostconfig to (file <font color = red>"hostconfig"</font> of folder <font color = red>"original"</font> of folder <font color = red>"backup_hostconfig"</font> of folder <font color = red>"Documents"</font> of home) as alias
try
close access hostconfig
end try
set hc_ref to open for access hostconfig
set hcData to read hc_ref
close access hc_ref
-- check for <font color = red>"IPFORWARDING"</font> string
set ip_forward to (offset of <font color = red>"IPFORWARDING="</font> in hcData)
if ip_forward &gt; <font color = blue>0</font> then -- if it's there
set forward_state to (offset of <font color = red>"IPFORWARDING=-NO-"</font> in hcData)
if forward_state &gt; <font color = blue>0</font> then
set i to <font color = blue>16</font>
set theBool to <font color = red>"YES"</font>
else
set forward_state to (offset of <font color = red>"IPFORWARDING=-YES-"</font> in hcData)
set i to <font color = blue>17</font>
set theBool to <font color = red>"NO"</font>
end if
if forward_state &gt; <font color = blue>0</font> then
set new_hostconfig to ((text <font color = blue>1</font> thru (ip_forward + <font color = blue>13</font>) of hcData) & theBool & (text (ip_forward + i) thru <font color = blue>-1</font> of hcData)) as text
-- write new hostconfig to backup dir
do shell script (<font color = red>"echo -n '"</font> & new_hostconfig & <font color = red>"' &gt; ~/Documents/backup_hostconfig/modified/hostconfig"</font> as text)
-- copy new hostconfig file to /etc... AppleScript/shell doesn't like to write directly to /etc/hostconfig
do shell script <font color = red>"cp ~/Documents/backup_hostconfig/modified/hostconfig /etc"</font> with administrator privileges
end if
end if</font>[/code]
grep could also be used to create the new hostconfig string:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set new_hostconfig to ((do shell script <font color = red>"cat /etc/hostconfig | grep -B <font color = blue>100</font> IPFORWARDING | grep -v IPFORWARDING"</font>) & return & <font color = red>"IPFORWARDING=-YES-"</font> & return & (do shell script <font color = red>"cat /etc/hostconfig | grep -A <font color = blue>100</font> IPFORWARDING | grep -v IPFORWARDING"</font>)) as text</font>[/code]
Unfortunately, the carriage returns get changed to newlines & when written to a file, the shell won't read it. There must be a simple way to preserve the returns, but it's escaped me right now...
The backups could go anywhere... couldn't really think of a better place than ~/Documents (seems like the convention is creating a ".old" file, but I never really liked doing that)

edit: added code to reverse IPFORWARDING state & changed "cat" commands to AppleScript "read" commands, and removed superfluous "greps"... sorry about the long lines!

[ 01-26-2002: Message edited by: acur128 ]
     
acur128
Junior Member
Join Date: May 2001
Status: Offline
Reply With Quote
Jan 26, 2002, 04:59 PM
 
Well, using "cat" in the script to get the text from the file seems to mess up the carriage returns... & same problem with "pr -t"... it looks fine in a text editor, but the shell can't read it (using "cat"... go figure). To conserve space, I've edited the above script fragment to use the AppleScript "read" command to get the text from the hostconfig file instead of "cat"...
Hmmm... anyway, it appears to work & preserve the text using AppleScript.
     
Ibson
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jan 26, 2002, 07:12 PM
 
Very nice work . Looks like it should work well.
     
absmiths
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Jan 29, 2002, 01:29 PM
 
If you are causing the user to edit the file, why not just open the file with Text Edit? That's a lot better than telling the user to fiddle with the terminal then hit Ctrl-O when he is done. You may even be able to get a notification of when the user closes the file.
     
   
 
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
Top
Privacy Policy
All times are GMT -4. The time now is 05:16 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,