Applescript - creating a folder if it doesn't exist

11,499

Solution 1

Here is another approach: mkdir -p will create a new folder if one does not exist. From the man page:

-p Create intermediate directories as required. If this option is not specified, the full path prefix of each operand must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists.

on open droppedItems
    repeat with anItem in droppedItems
        set outputFolder to POSIX path of ((anItem as text) & "::") & "converted"
        do shell script "mkdir -p " & quoted form of outputFolder
    end repeat
end open

EDIT

on open droppedItems
    repeat with anItem in droppedItems
        set outputFolder to POSIX path of ((anItem as text) & "::") & "converted"
        do shell script "mkdir -p " & quoted form of outputFolder

        --If you need to reference the folder by alias instead of posix path
        set outputFolderAlias to (POSIX file outputFolder) as alias
    end repeat
end open

Solution 2

I got the following version to work. I left in the "say" commands. Using say commands is a good debugging technique.

on open droppedItems
    say "on open"
    tell application "Finder"
        set inputFolder to (container of first item of droppedItems) as Unicode text
        set convertedFolderPath to inputFolder & "converted:"
        if (exists (folder convertedFolderPath)) then
            say "converted folder exists"
            set outputFolder to (inputFolder & "/converted/") as text
        else
            say "converted folder does not exist"
            make new folder at inputFolder with properties {name:"converted"}
            set outputFolder to the result as text
        end if
    end tell
    say "end open"
end open

---Edit---

Oh, this is tagged with an "Automator" tag. If your code is within an Automator action of "Run AppleScript", then it shouldn't have the "on open droppedItems". In Automator, the script should look like the following:

on run {input, parameters}
    -- Enter your scripting here (without the "on open droppedItems" part)
    return input
end run

---Edit 2---

OK. I understand that the path was part HFS and part POSIX. The funny thing is that it did work on my computer for both creating a new folder and for detecting that a folder already existed, but here is my code that is fixed to have an HFS path without any part ofis being a POSIX pah:

on open droppedItems
    say "on open"
    tell application "Finder"
        set inputFolder to (container of first item of droppedItems) as Unicode text
        set convertedFolderPath to inputFolder & "converted:" ---- changed this ----
        if (exists (folder convertedFolderPath)) then
            say "converted folder exists"
            set outputFolder to convertedFolderPath
        else
            say "converted folder does not exist"
            make new folder at inputFolder with properties {name:"converted"}
            set outputFolder to the result as text
            say "created folder"
        end if
    end tell
    say "end open"
end open

Mac Pathnames

Solution 3

Don't convert the inputFolder to Unicode Text. The Finder doesn't know what to do with text in the subsequent statements.

set inputFolder to (container of first item of droppedItems)

Then later, convert just the inputFolder to text.

set outputFolder to (inputFolder as text) & "converted"
Share:
11,499
meeble
Author by

meeble

Updated on June 04, 2022

Comments

  • meeble
    meeble almost 2 years

    Can someone please point out why this bit of Applescript isn't working? Thanks!

    on open droppedItems
        tell application "Finder"
            set inputFolder to (container of first item of droppedItems) as Unicode text
            if (exists folder ("converted") of inputFolder) then
                set outputFolder to (inputFolder & "/converted/") as text
            else
                make new folder at inputFolder with properties {name:"converted"}
                set outputFolder to the result as text
            end if
        end tell
    end
    
  • adayzdone
    adayzdone over 10 years
    You are mixing hfs paths with posix paths. outPut folder would equal Mac OS X:Users:Keydell:Desktop:/converted/ in your script.
  • meeble
    meeble over 10 years
    Your version works if the converted folder does not exist - but if it already exists, the script fails.
  • adayzdone
    adayzdone over 10 years
    It fails because he is mixing posix paths with hfs paths.