Applescript - creating a folder if it doesn't exist

与世无争的帅哥 提交于 2019-12-11 04:01:24

问题


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

回答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



回答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




回答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"


来源:https://stackoverflow.com/questions/18796948/applescript-creating-a-folder-if-it-doesnt-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!