Search code examples
applescriptfindercreate-directory

Applescript/Finder - how to check if a variable folder exists within a fixed folder


I'm trying to get the script to check if there is a variable folder name, eg. "folder x", present from within a folder that has folders a-z for example?

The rest of the script works fine but the problem is that no matter what I've done so far I only get "yes" as the result of this line if exists folder theTitleCV of alias "Comics:" then.

My script so far:

set theTitleCV to "Folder X"
set theTitle to "Folder X-52"

--Creating the folder if it doesn't exists
tell application "Finder"
    if exists folder theTitleCV of alias "Comics:" then
        if theTitleCV is equal to theTitle then
            make new folder of alias "Comics:" with properties {name:theTitleCV}
        else
            set theTitleResult to ((display dialog "Title Options" buttons {theTitleCV, theTitle, "Cancel"} default button 2))
            set theButton to button returned of theTitleResult
            make new folder of alias "Comics:" with properties {name:theButton}
        end if
    else
        return "Yes"
    end if
end tell

Thanks in advance for any help that is offered.

P.S. If this type of question has been asked and answered before, please just point me in the direction as to what to search for. Thanks :)


Solution

  • thanks to everyone who helped and especially to vadian for spotting the obvious mistake.

    I have now fixed the script and have added a display dialog to go with it.

    set theTitleCV to "Folder X"
    set theTitle to "Folder X-52"
    
    
    --Creating the folder if it doesn't exist
    tell application "Finder"
        if not (exists folder theTitleCV of disk "Comics") then
            if theTitleCV is equal to theTitle then
                make new folder of disk "Comics" with properties {name:theTitleCV}
            else
                set theTitleResult to ((display dialog "Title Options" buttons {theTitleCV, theTitle} default button 2 with icon ":System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:GenericFolderIcon.icns" as alias))
                set theButton to button returned of theTitleResult
                make new folder of disk "Comics" with properties {name:theButton}
            end if
        else
            tell application "Finder" to activate
            return display dialog "" & return & "Thank you for checking," & return & "but this folder already exists." with title "Folder Already Exists" buttons {"OK"} default button 1 with icon ":System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:AlertNoteIcon.icns" as alias
        end if
    end tell