首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Applescript删除空文件夹?

如何使用Applescript删除空文件夹?
EN

Stack Overflow用户
提问于 2018-08-10 02:49:10
回答 2查看 1.3K关注 0票数 0

我正在编写一个Applescript,它会提示用户选择一个文件夹,然后在该文件夹上运行脚本。该脚本运行,基本上将文件分类到文件夹中。在脚本的末尾,我希望删除空文件夹。

这个脚本运行得很好,直到我想删除最后的空文件夹。

下面是选择该文件夹的内容:

代码语言:javascript
复制
tell application "Finder"
    set packageFolder to (choose folder with prompt "Please choose your 
    logo package folder") as string
end tell

下面是删除空文件夹的代码:

代码语言:javascript
复制
tell application "Finder"
    repeat with oneFolder in (get folders of packageFolder)
        if (count items) of oneFolder is 0 then delete oneFolder
    end repeat
end tell

我得到以下错误:

结果:

错误“无法获取|”“Macintosh HD:Users:michael: Desktop:徽标程序包脚本测试:\”的每个文件夹。“编号-1728,来自“Macintosh HD:Users:michael: Desktop: Logo Package Script Test:”的每个类cfol»

我在这里做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2018-08-10 03:09:59

这对我在最新版本的macOS高塞拉上有效

代码语言:javascript
复制
set packageFolder to (choose folder with prompt "Please choose your 
    logo package folder") as string

tell application "Finder"
    set theFolders to folders of entire contents of folder packageFolder
    repeat with i from 1 to count of theFolders
        set thisFolder to item i of theFolders
        if (count of items of thisFolder) is equal to 0 then
            delete thisFolder
        end if
    end repeat
end tell
票数 0
EN

Stack Overflow用户

发布于 2018-08-10 04:07:53

这就是最终有效的方法:

代码语言:javascript
复制
on killEmpty(fol)

    tell application "Finder"

        repeat with f in (get fol's folders)

            my killEmpty(f)

        end repeat

        if (count items of fol) is 0 then delete fol

    end tell

    end killEmpty



    tell application "Finder"

    repeat with f in (get folders of folder packageFolder)

        my killEmpty(f)

    end repeat

end tell

我在这里找到了这个解决方案:https://discussions.apple.com/message/20188767#message20188767

再次感谢大家。对于措辞不准确的问题,我很抱歉。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51773931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档