Re: What is wrong with this script?
Re: What is wrong with this script?
- Subject: Re: What is wrong with this script?
- From: "John W. Baxter" <email@hidden>
- Date: Sat, 29 Nov 2003 12:12:01 -0800
- Envelope-to: email@hidden
On 11/28/2003 21:05, "Matthew Smith" <email@hidden> wrote:
>
on 29/11/2003 14:44, Michelle Steiner at email@hidden wrote:
>
>
> set effect to (button returned of (display dialog "Which Dock effect do
>
> you want?" buttons {"Scale", "Genie", " Suck"}))
>
>
>
> do shell script "defaults write com.apple.dock mineffect " & effect &
>
> return & "killall Dock"
>
>
>
> --> "2003-11-28 20:43:34.216 defaults[1019] Unexpected argument Dock;
>
> leaving defaults unchanged."
>
>
Shouldn't that be:
>
>
do shell script "defaults write com.apple.dock mineffect " & effect &
>
";killall Dock"
>
>
The ";" allows you to put 2 commands on the online. The second command oinly
>
executes if the first one succeeds.
>
The shell will happily execute the command to the right of the ; even if the
command to the left fails. First, let's go into the shell and create a
command which fails:
[john@Zeus john]$cd /tmp
[john@Zeus tmp]$cat > fail.sh
#!/bin/sh
exit 1
^D
[john@Zeus tmp]$cat fail.sh
#!/bin/sh
exit 1
[john@Zeus tmp]$chmod u+x fail.sh
Now, we can exercise this either from the shell (left as an exercise) or
from Applescript:
do shell script "/tmp/fail.sh; echo yes"
--> "yes"
A very common way to do this in shell scripting would be to write the shell
part as:
do shell script "/tmp/fail.sh && echo yes"
Now Script Editor reports an error of type 1. Why? The && means "and". If
the first command succeeds, the second is executed; if the first command
fails the result is a failure, with no need to execute the second command.
And now this, which is also a common idiom:
do shell script "/tmp/fail.sh || echo no"
--> "no"
The command on the left fails. The || means "or"; since the left command
failed, the right-hand command is run. Had the left command succeeded, the
right-hand command would not have run.
For those who care, the special shell variable named ? contains the exit
status from the prior command:
do shell script "echo 'you should see this'; echo $?"
--> "you should see this
0"
do shell script "/tmp/fail.sh; echo $?"
-->"1"
--John
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.