Go Back   The macosxhints Forums > OS X Help Requests > AppleScript

Thread Tools Rate Thread Display Modes
Old 05-05-2012, 10:12 PM   #1
Join Date: Apr 2012
Posts: 4
Help Typing in Terminal.app

I am building an applescript that edits a macs hosts file. Below is what i have so far. But the part in red and underlined is not working, I Cant get it to type the earlier entered password to enter in terminal when the sudo command asks for a password.

-------Begin Script-------

set example to "Installation Has Been Canceled"

display alert "This installation will block communications between the adobe servers and all your Adobe Products CS5.5 and earlier. This should not be used to pirate Adobe Products. User assumes all blame for any leagal repercussions. To continue, please press OK. Otherwise, Press Cancel." buttons {"Cancel Installation", "I Agree"}
set theAnswer to button returned of the result
if theAnswer is "I Agree" then
tell application "Terminal"
do script " sudo nano /private/etc/hosts"
end tell
display alert "Terminal will need an administrator password to complete the instalation. Would you still like to continue?" buttons {"Cancel Installation", "OK"}
display dialog "Type your password below, and click OK." default answer ""
set thepassword to text returned of the result
tell application "Terminal"
do script keystroke thepassword
end tell
do shell script "/usr/bin/say " & quoted form of example & " &> /dev/null &"
display alert example

end if

------End Script------
MacHacker24 is offline   Reply With Quote
Old 05-06-2012, 06:06 AM   #2
League Commissioner
Join Date: Jan 2002
Posts: 8,517
You won't see your password as you type it in the terminal.
How do you know that the password is not being entered?
Do you get an error as a result?
DeltaMac is offline   Reply With Quote
Old 05-06-2012, 07:01 AM   #3
League Commissioner
Join Date: Sep 2003
Location: Tokyo
Posts: 6,334
Originally Posted by MacHacker24
do script " sudo nano /private/etc/hosts"

You don't script nano - it's an interactive editor.

It appears that all you want to do is add one line to /etc/hosts. You do this with the append function.

And you never bother with flinging around the user's (or admin's) password - let the OS handle it.

do shell script "echo ' adobe.com' >> /private/etc/hosts" with administrator privileges
acme.mail.order is offline   Reply With Quote
Old 05-06-2012, 09:15 AM   #4
Join Date: Apr 2012
Posts: 4
Yes, there was an error saying it cant keystroke the variable "thePassword"
Thanks for the help guys.

Do you know of any good resources to learn applescript?

Last edited by MacHacker24; 05-06-2012 at 09:21 AM.
MacHacker24 is offline   Reply With Quote
Old 05-06-2012, 09:42 AM   #5
League Commissioner
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,145
The error is that do script expects a string. Your line should read:

do script "keystroke " & thepassword

but the other comments apply -- let the system look after that.
17" MBP, OS X; 27" iMac, both OS X 10.9.x (latest)
NovaScotian is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting 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

Forum Jump

All times are GMT -5. The time now is 06:26 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.