首页 > 解决方案 > 在 if 语句处发出运行 bash 脚本

问题描述

我刚刚开始使用 AppleScript 并正在测试现有的脚本,看看我是否可以让它在我的计算机上运行。我得到的错误在这里:

应为“,”或“]”,但发现未知标记。

它在我的 if 语句中突出显示了 ~:

path="/Library/Desktop Pictures/Wallpaper-Blue-3D-logolow-2846x1600.jpg"
    
    if [ -d ~/Library/Desktop\ Pictures/ ]
    then
            echo "Desktop Pictures directory already exists"
    else
            mkdir ~/Library/Desktop\ Pictures/
    fi

我错过了什么?谢谢!!

标签: if-statementapplescript

解决方案


为避免对 mkdir shell 命令提供完整的磁盘访问权限,请更好地使用系统事件或 Finder:

try
    -- if the folder doesn't exist, then getting the alias will throw an error
    alias ("" & (path to library folder from user domain) & "Desktop Pictures")
on error
    -- Desktop Pictures of library of home doesn't exist, so create it
    tell application "System Events" -- or, tell to Finder
        make new folder at folder "Library" of home folder with properties {name:"Desktop Pictures"}
    end tell
end try

推荐阅读