Apple Technical Note TN2065 -- do shell script in AppleScript 问答集锦

来源:互联网 发布:photoshop6 mac破解版 编辑:程序博客网 时间:2024/06/04 22:47

Q: How do I pass an AppleScript variable to my shell command?

Since the command argument to do shell script is really just a string, you can build the string at run time using the AppleScript concatenation operator &. For example, to pass the variable as a command parameter, you would do this:

set hostname to "www.apple.com" do shell script "ping -c1 " & hostname

Q: How do I simulate standard input?

 Some commands require data to be fed to standard input. do shell script does not directly support this, but you can fake it using echo and a pipe:

set input to "hello"do shell script "echo " & input & " | tr a-z A-Z" -- "HELLO"
 

Q: My command works fine in Terminal, but when I try to use it in do shell script, I get an error about "command not found." What’s going on?

 There are two possibilities. First, do shell script always uses /bin/sh to interpret your command, not your default shell, which Terminal uses. Second, when you use just a command name instead of a complete path, the shell uses a list of directories (known as your PATH) to try and find the complete path to the command. 

  

Q: How can I use more than one command in a single do shell script?

 Each invocation of do shell script uses a new shell process, so state such as changes to variables and the working directory is not saved from one to the next. To do several commands in a single invocation, separate the commands with semicolons like this:

do shell script "cd ~/Documents; ls" -- result: "Welcome.txt"

Q: How do I get administrator privileges for a command?

 Use the administrator privileges, user name and password parameters like this:

do shell script "command" with administrator privileges

Q: How does do shell script get the result?

the result is whatever text was printed to standard output, possibly with some modifications.

  

Q: How does do shell script report errors?

When running in Terminal, standard output and standard error are both sent to the same place, so it’s difficult to tell them apart. do shell script, on the other hand, keeps the two streams separate. If you want to combine them, follow the command with 2>&1 like this:

do shell script "command 2>&1"

 Q: My command doesn’t work right when a parameter has spaces or certain punctuation — parentheses, $, *, etc.

Because the shell separates parameters with spaces, and some punctuation marks have special meanings, you must take special steps to make the shell treat your string as one parameter with literal spaces, parentheses, etc. This is called "quoting," and there are several ways to do it, but the simplest and most effective is to use the quoted form property of strings.

do shell script "echo " & quoted form of s & " >> ~/stuff"

Q: What are the rules for line endings?

 There are two different line ending conventions in Mac OS X: Mac-style (lines end with return: "/r" or ASCII character 13) and Unix-style (lines end with line-feed: "/n" or ASCII character 10). Shell commands typically only handle Unix-style line endings, so giving them Mac-style text will produce less-than-useful results. For example, grep would consider the entire input to have only one line, so you would get at most one match.

If your data is coming from AppleScript, you can transform the line endings there or generate line feeds in the first place — /"//n/" or ASCII character 10 both yield a line feed. If your data is coming from a file, you can make the shell script transform the line endings by using tr. For example, the following will find lines that contain "something" in any plain text file. (The "quoted form of POSIX path of f" idiom is discussed under Dealing with Files.)

set f to choose filedo shell script "tr '//r' '//n' > " & quoted form of POSIX path of f & " | grep something"


Q: I have an AppleScript file or alias object; how do I pass it to a shell command?

choose filedo shell script "ls -l " & quoted form of the POSIX path of the result


Q: How do I control an interactive tool like ftp or telnet with do shell script?

 The short answer is that you don’t. do shell script is designed to start the command and then let it run with no interaction until it completes, much like the backquote operator in Perl and most Unix shells.

 

 

 

原创粉丝点击