AppleScript, Do Shell. How do I write the "test" command to the "script editor"?

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:

  1. if there is no folder, then report that there is no folder,
  2. 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.

Answered by DTS Engineer in 790264022

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

  1. choose file returns an alias. If you already have a posix style path to your file, you can also use that
  2. 'posix path of' converts the alias into a posix style path
  3. 'quoted form of' quotes the path properly for the command.
  4. the ' || (set status 0; echo "not a dir")' part is run if the 'test' command fails
  5. the 'echo "is a dir"' runs if the test command succeeds
  6. 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.
  7. '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

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.

Yep, that’s what I’d expect. The do shell script command checks the status of the invoked tool. If the tool exits with status 0, do shell script returns its output. Otherwise do shell script throws an AppleScript exception.

Consider:

% mkdir "MyDir"
% test -d "MyDir" ; echo $?
0
% test -d "NotMyDir" ; echo $?
1

Rather than shelling out for stuff like this, you can use AppleScript native constructs. Consider:

set pathOK to "/Users/quinn/Test/MyDir"
set posixOK to POSIX file pathOK
tell application "System Events"
	folder (posixOK as string)
end tell

set pathNG to "/Users/quinn/Test/NotMyDir"
set posixNG to POSIX file pathNG
tell application "System Events"
	folder (posixNG as string)      -- throws
end tell

There’s an old doc that explains this pretty well. You should also search the ’net, because there’s lots of AppleScript help out there.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

eskimo, I copied this line to the Script Editor, the Script Editor returned an error: An unknown marker, and highlighted the "%" sign.

% test -d "MyDir" ; echo $?

Right. I’m using the % syntax to indicate a shell command (because % is the prompt displayed by macOS’s default shell). Try running these commands in a shell in Terminal.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

eskimo, But I need the code specifically for the Script Editor, because I plan to write scripts that will consist of several lines, not one. In the first message, I asked a question, can you give an answer to it, taking into account the Script Editor.

You need to break this problem down into parts:

  • How do you get the output from a shell script?

    set scriptResult to do shell script "echo hello cruel world"
    scriptResult -- -> hello cruel world
    
  • How do you run a multi-line shell script?

    set myScript to "echo hello
    echo cruel
    echo world
    "
    do shell script myScript
    
  • How do you handle a shell script failing?

    try
        do shell script "false"
        set didSucceed to "yay"
    on error
        set didSucceed to "boo"
    end try
    didSucceed -- -> boo
    
  • On so on

Once you have all the parts working, you can then start assembling them into a whole.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

eskimo, can you write what it will look like for the "test" command? I asked a specific question in the first message, not a general one.

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

  1. choose file returns an alias. If you already have a posix style path to your file, you can also use that
  2. 'posix path of' converts the alias into a posix style path
  3. 'quoted form of' quotes the path properly for the command.
  4. the ' || (set status 0; echo "not a dir")' part is run if the 'test' command fails
  5. the 'echo "is a dir"' runs if the test command succeeds
  6. 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.
  7. '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

Checked the solution above on macOS Sonoma 14.5 (23F79).

DTS Engineer, I inserted your code into the Script Editor and an error occurred. I have High Sierra (10.13.6).

This is one of those situations where learning some basics instead of having others do everything helps.

The posted script just has some quoting issues - either escape the double quotes for use in the string, or use single quotes since the shell also understands those. The corrected script would be:

set myFile to choose file -- choose a package or 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

Sorry about the error. I found my backslashes were removed when posting the AppleScript inside of regular text.

The backslash characters in my original post seem to be disappeared by the formatting engine here. Sorry about that - I wasn't expecting that to happen. Here is the same with some formatting to allow the backslashes to be seen:

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"

In this case, your solution of using single quotes is a good one. Keep in mind, though, in shell commands using single quotes isn't always the first choice because they turn off variable interpolation.

AppleScript, Do Shell. How do I write the "test" command to the "script editor"?
 
 
Q