Hello. How to write this command correctly on a Macbook, in the script editor, so that I can click the "Run script" button and the script will give the result:
- if there is no folder, then report that there is no folder,
- if there is a folder, then report that the folder exists.
do shell script "test -d 'Users/user/Desktop/New folder'"
Now, if the folder exists, an empty string ("") is returned, if the folder does not exist, the script editor reports that an error has occurred.
In general, my task is to write a script that checks the existence of a folder.
set myFile to choose file -- or try 'choose folder' to select a folder
set theCommand to "(test -d " & quoted form of POSIX path of myFile & " && echo "is a dir") || (set status 0; echo "not a dir") ; 2>&1"
return do shell script theCommand
- choose file returns an alias. If you already have a posix style path to your file, you can also use that
- 'posix path of' converts the alias into a posix style path
- 'quoted form of' quotes the path properly for the command.
- the ' || (set status 0; echo "not a dir")' part is run if the 'test' command fails
- the 'echo "is a dir"' runs if the test command succeeds
- the 2>&1 at the end consolidates any stderr and stdout output, it's optional in this command but sometimes useful if you're debugging a command.
- 'set status 0' clears the error, it's only executed if the part before the || fails.
See the following for more details: Technical Note TN2065: do shell script in AppleScript