eanno eanno - 3 months ago 10
AppleScript Question

Applescript: make new folder using a simple repeat/end repeat to check if name exists

I am trying to create a script which creates a new client folder. No real drama there but I'm running aground when I try to add a bit of functionality which checks to see whether the folder exists. If false, it will go ahead and create the folder and if true, it will repeat the initial question (and check) until the user enters a client name that does not exist. The problem I've got is I can get it to check and/or create a folder after it has checked but I can't get the loop to stop once it has created a folder.

Here's what I've managed thus far

tell application "Finder"
activate
repeat
set newClientName to text returned of (display dialog "Enter New Client Name" default answer "")
set thePath to "MATRIX:Designs:Digital:Clients:"
set theFolder to thePath & newClientName

if (theFolder exists) = true then
display dialog "There is already a Client with that name"

else
make new folder at thePath with properties {name:newClientName}

end if

end repeat
end tell


Any direction would be greatly appreciated

Answer

Just exit repeat the loop on success.

You should check for a folder rather than a literal string path

set thePath to "MATRIX:Designs:Digital:Clients:"
tell application "Finder"
    activate
    repeat
        set newClientName to text returned of (display dialog "Enter New Client Name" default answer "")
        if exists folder newClientName of folder thePath then
            display dialog "There is already a Client with that name"
        else
            make new folder at folder thePath with properties {name:newClientName}
            exit repeat
        end if
    end repeat
end tell
Comments