3

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
Jason
  • 1,612
  • 16
  • 23
meeble
  • 93
  • 1
  • 2
  • 11

3 Answers3

3

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

Kaydell
  • 484
  • 1
  • 4
  • 14
2

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
adayzdone
  • 11,120
  • 2
  • 20
  • 37
1

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"
Jason
  • 1,612
  • 16
  • 23