Simple script for Terminal commands

Hi!
I am a noob at scripting and I tried to make a script wich opens terminal, types a command of my choice and closes terminal.
I have just managed to have my script open terminal.
anyone has any idea on how to solve it?
any help is really apreciated!
Regards,
Wonkah

Hi Wonkah,

AppleScript code like follows should do the trick:


tell application "Terminal"
	activate
	do script "ls -a"
	delay 2
	quit
end tell

You can also consider to use the «do shell script» command, so you don’t need to open the Terminal at all:


set output to do shell script "ls -a"

thanks very much.
one more question though,
If i want to do anything with root privilegies (sudo) i need to type in my password. is it possible to have the script doing that aswell?
thanks!

The «do shell script» can execute a shell script or command with administrator privileges:


set output to (do shell script "ls -a" user name "wonkah" password "¢¢¢¢¢¢¢¢" with administrator privileges)

Awesome! thank you!

Btw, is there any templates for dynamic scripts (scripts using customizable settings so that the user can change them without having to go into script edit)?
for example I want a script to change my airport MAC Adress so when i run the script i get a dialogue box saying; “MAC-Adress:” and i am able o type in the adress I want and then press confirm and the script runs according to what i type.
Is it very complicated?
Thanks in advance!
/Wonkah

No, in AppleScript this is quite easy:


property mytitle : "MAC-O-Address"

on run
	try
		set macaddress to my getmacaddress()
		if macaddress is missing value then return
		set command to "phantasy command " & macaddress
		set output to (do shell script command user name "martin" password "newton42" with administrator privileges)
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop with title mytitle giving up after 25
		end tell
	end try
end run

on getmacaddress()
	try
		tell me
			activate
			display dialog "Please enter a MAC address:" default answer "" buttons {"Cancel", "Enter"} default button 2 with title mytitle
		end tell
		set dlgresult to result
	on error
		return missing value
	end try
	return (text returned of dlgresult)
end getmacaddress

That’s really awesome!
Just one final request.

i would like for the script to first look up my current mac-adress using “command2”
and set the reply to default answer (without “ether” in it, only the numbers)

Thank you so much for all the help!

there are several options to do that, here is one


.
set currentMACaddress to primary Ethernet address of (system info)
display dialog "Please enter a MAC address:" default answer currentMACaddress buttons {"Cancel", "Enter"} default button 2 with title mytitle
.

thanks for the reply,

but what im looking for is that “default answer” is set to the result (reply) i get from “ifconfig en1 | grep ether”
so the defalt answer is always the current MAC-adress. Of wi-fi, not tcp/ip.

i suppose something like this (cant get it to work though)


set Lookup to result of (do shell script "ifconfig en1 | grep ether")

...

display dialog "Choose a MAC-Address for your Wi-Fi:" default answer lookup buttons {"Cancel", "Choose"} default button 2 with title mytitle



I just get "cant get result of “ether XX:XX:XX:XX:XX:XX”

I would just like to have that as lookup without the “ether”.

thanks in advance.


.
set wirelessMACaddress to do shell script "/sbin/ifconfig en1 | /usr/bin/awk '/ether / {print $2}'"
display dialog "Please enter a MAC address:" default answer wirelessMACaddress buttons {"Cancel", "Enter"} default button 2 with title mytitle
.

Thank you so much for all your help!

I have a couple more questions though:

  1. i would like to create a “preferences” page which can be accessible through the top menu (beside the apple logo in the top left. as you usually do on normal applications.
    there i would like to make it possible for the user to type in their account and password, which is needed by sudo (so that they can use the program properly).
set output to (do shell script command user name "USER" password "PASS" with administrator privileges)

set command to "sudo ifconfig en1 ether "

  1. I would also like to have an image in the main page, to the left of the text (like in the error page, but i want a custom logo not “stop”)

is it easily done?

i would appreciate any help.

Also, is there a database of all the basic commands that apple script uses? with explanations and examples.

Thanks in advance!
Regards,
Wonkah