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.

Replies

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.