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



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 04-22-2012, 11:16 PM   #1
MacHacker24
Prospect
 
Join Date: Apr 2012
Posts: 4
Exclamation variable button_pressed is not defined

I am making an Applescript to edit the host file on a mac. This is all I have so far, but when I click compile, I get an error: "The variable button_pressed is not defined." Can anyone help me out?

AppleScript:

tell application "Finder"
display dialog "This application will block Homecalling in Adobe Products CS5.5 and Earlier. To continue, please press OK. Otherwise, Press Cancel."
end tell
if button_pressed = "OK" then
tell application "Terminal"
do script " sudo nano /private/etc/hosts"
end tell
end if
if button_pressed = "Cancel" then
tell application "Finder"
display dialog "You Have Canceled The Installation"
end tell
end if
MacHacker24 is offline   Reply With Quote
Old 04-23-2012, 08:49 PM   #2
sojourner
Major Leaguer
 
Join Date: Apr 2010
Posts: 371
If you just run:

Code:
tell application "Finder"
	display dialog "This application will block Homecalling in Adobe Products CS5.5 and Earlier. To continue, please press OK. Otherwise, Press Cancel."
end tell
When you press "OK", the result returned is:

Code:
{button returned:"OK"}
Your script tells Script Editor to get the value of the variable button_pressed, and that variable doesn't exist. Even if you asked for the value of the variable |button returned|, Script Editor would throw an error, because the variable |button returned| doesn't exist.

The correct phrasing for what you want is:

Code:
button returned of result
as in:

Code:
if button returned of result = "OK" then
Note that when you press the "Cancel" button, the result returned is:

Code:
error "Finder got an error: User canceled." number -128
This means that the script was canceled immediately, and no if statements were processed. Because of this, you'll have to script it differently if you really want a message to display if the user presses cancel. In that case, I would suggest looking into try/on error statements. While I could help further with this, I won't be able to do that this week (and maybe not next).
__________________
see a problem; solve a problem.
sojourner is offline   Reply With Quote
Old 05-05-2012, 10:15 PM   #3
MacHacker24
Prospect
 
Join Date: Apr 2012
Posts: 4
Thanks you, I am new to applescript and have lots to learn!
MacHacker24 is offline   Reply With Quote
Old 05-06-2012, 05:07 PM   #4
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,161
Another way to get the script to continue even if the user presses "Cancel" is to spell it differently, as in:

Code:
tell application "Finder"
	display dialog "This application will block Homecalling in Adobe Products CS5.5 and Earlier. To continue, please press OK. Otherwise, Press Cancel." buttons {"OK", " Cancel "}
end tell
if button returned of the result = "OK" then
	tell application "Terminal"
		do script " sudo nano /private/etc/hosts"
	end tell
else
	-- user pressed " Cancel "
	tell application "Finder"
		display dialog "You Have Canceled The Installation" buttons {"OK"} default button 1
	end tell
end if
Notice the spaces around the word "Cancel" in the button name, making the button appear not to be a cancel button.

Notice the buttons {"OK"} default button 1 in the second display dialog, so the user can dismiss the dialog by simply pressing return or enter, rather than having to use the mouse to click on one of the two buttons and wondering what the difference is between them.

But you have other problems. The terminal command is only going to open /private/etc/hosts for editing in a new window. A user who doesn't already know what to do in that window (if they see it at all -- it may be hidden behind other windows) will be mystified what to do next; a user who does know what to do next isn't going to bother running an Apple Script to do something they already know how to do in Terminal.
ganbustein 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 10:54 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.