0
votes

I want to write a script using Automator that opens a folder in a particular location, after receiving user input. Doesn't have to be Applescript.

So the steps would be:
Dialog asking for Folder name
Verifying the folder exists
If exists, open folder in new finder window
if not exist, display message

Any help would be greatly appreciated.

2

2 Answers

0
votes

instead of asking user to type folder name into a dialog box, why not use the standard "choose folder" which provide usual file/folder selection graphic interface ? on top of that, it will also make sure the folder selected exists !

Also it is possible to user instruction "if My_Folder exists then ..." to check if folder (or file) exists

Example of direct user selection : 5 first lines are asking folder selection in folder Documents and detect user cancellation. Next lines are just example to display result

try
set SelectFolder to choose folder with prompt "choose folder" default location "/Users/My_User/Documents"
on error number -128
set SelectFolder to ""
end try


if SelectFolder is "" then
display alert "user did not select a folder"
else
display alert "User selection is " & SelectFolder
end if
0
votes

The following script does exactly what you're asking for.

on run
    set thisFolder to (choose folder with prompt "Choose a folder...")
    if folderExists(thisFolder) then
        -- display dialog "The folder exists." buttons {"OK"} default button 1
        tell application "Finder" to open thisFolder
    else
        display dialog "The folder has been deleted" buttons {"OK"} default button 1
    end if
end run

on folderExists(theFolder)
    tell application "Finder"
        try
            set thisFolder to the POSIX path of theFolder
            set thisFolder to (POSIX file thisFolder) as text
            --set thisFolder to theFolder as alias
            return true
        on error err
            return false
        end try
    end tell
end folderExists

That said, note that a folder that has been selected using AppleScript's Choose Folder command must always exist. It can't be selected if it doesn't exist. Therefore, my first thought is that you don't need this, but if you need to check whether a folder exist for a different reason, then the folderExists function will do the job.