NSCannotCreateScriptCommandError (10)

Hi,

I am a newbee to Apple Script.

I have a Button to start terminal and do some scripting and then quitting terminal application. Then i am trying to disable the button.
but it gives the following error. But it builds fine, its ia Runtime error i am getting… What am i doing wrong? Any help would be greatly appreciated…

Here is the code…

on clicked theObject
tell application “Terminal”
activate
do shell script “sudo -k”
do shell script “ps -ax” password “userpass” with administrator privileges
do shell script “exit”
do shell script “sudo -k”
end tell
quit application “Terminal”
set enabled of button “startup” to true
end clicked

Sincerely,
Arvind

Where to begin…

You’re posting in the wrong forum (this is an AppleScript Studio question and should be posted the AppleScript Studio forum).

You’re using “do shell script” commands which are from the Standard Additions scripting addition and have nothing to do with the Terminal app and wrapping them in a tell block for that app is useless. If you really want to script the Terminal app, use ‘do script “the_script”’.

Even if it did have something to do with the Terminal app, why did you separate the line to quit the app from the rest of the tell block? That line should be included in the “tell” block for the app and should be wrapped in a “try” block:

tell application "Terminal"
	activate --only if you really need it to be active; "launch" may be more appropriate
	do script "ls" --or whatever
	try
		quit
	end try
end tell

The reference to the button will cause an error because you haven’t given a full reference to the button. Hint: you need to list the entire hierarchy of views until you reach the window object (i.e., ‘set enabled of button “startup” of window “main” to true’ or ‘set enabled of button “startup” of tab view item 1 of tab view 1 of window “main” to true’, etc.).

If you have only one button in your app, then your “on clicked” handler is OK but if you have more than one button that needs to do something when clicked, you need to test for the name of button that was clicked and use a compound “if-then” block to determine what the app should do. If you do only have one button, then since you have clicked that button to kick this off, you already have the complete reference to the button with the theObject parameter and you can just use: ‘set enabled of theObject to true’. Since you are enabling this object, as opposed to disabling it, this leads me to believe that it isn’t the only button because why enable a button that necessarily had to be enabled to kick the code off in the first place?

Using some “log” statements and “try” blocks, you would have been able to isolate the problem yourself:

on clicked theObject
	try
		log 1
		tell application "Terminal"
			activate
			do shell script "sudo -k"
			do shell script "ps -ax" password "userpass" with administrator privileges
			do shell script "exit"
			do shell script "sudo -k"
		end tell
		log 2
		quit application "Terminal"
		log 3
		set enabled of button "startup" to true
		log 4
	on error the_error
		log the_error
	end try
end clicked

This would have logged something like this in your app’s run log (or in the Console if you ran it like a regular app instead of building and running from AS Studio):

It’s “AppleScript”, one word with an inner cap.

And, finally, when posting code to the board, use the “AppleScript” button above and place you code inside the tags that are inserted and the board will not only take care of formatting the code for you, but will also post a link so you can easily open the code in the Script Editor.

Jon

Sorry if this came off too harsh, it’s been a long day…