Shell Script Commands Work Fine in Terminal But Not Via AppleScript

applescriptcommand lineterminal

I have two commands that I need to run through the Terminal. They are
cd /Users/[my name]/Desktop and curl 'www.google.com'>google.txt.
When running these directly in Terminal they work just fine. However, when running this in AppleScript:

do shell script "cd /Users/Peter1/Desktop"
do shell script "curl 'www.google.com'>google.txt"

It produced the error:

sh: google.txt: Permission denied

Why is this happening? What can I do to fix it?

Thanks

Best Answer

Your script above is running two different scripts. One is changing the working directory to your user desktop folder (~/Desktop). The other script is downloading www.google.com and putting it in a document in your root directory (/), which doesn't have permissions to do so.

You can fix this by reduce it to one statement of do shell script like such:

do shell script "curl www.google.com > ~/Desktop/google.txt"

In short, the script first downloads the contents of the website www.google.com and then creates a new file at the specified directory. You may also notice the ~ character, which is equivalent to /Users/username/.