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



Reply
 
Thread Tools Rate Thread Display Modes
Old 05-05-2012, 10:12 PM   #1
MacHacker24
Prospect
 
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
else
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
DeltaMac
League Commissioner
 
Join Date: Jan 2002
Posts: 8,450
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 online now   Reply With Quote
Old 05-06-2012, 07:01 AM   #3
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,262
Quote:
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.

Code:
do shell script "echo '127.0.0.1 adobe.com' >> /private/etc/hosts" with administrator privileges
acme.mail.order is offline   Reply With Quote
Old 05-06-2012, 09:15 AM   #4
MacHacker24
Prospect
 
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
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,073
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.2
NovaScotian is offline   Reply With Quote
Reply

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 02:33 PM.


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.