if-statement - 在 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
我错过了什么?谢谢!!
解决方案
为避免对 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
推荐阅读
- powershell - 我可以防止在 PowerShell 中创建的 Windows 事件观察程序随着服务器注销而消失吗?
- javascript - 如何使用 Firebase signInWithCredentials() 设置用户的显示名称和电子邮件
- typescript - 用于比较(和更新)Typescript 中对象的某些字段的类型定义
- kotlin - 单次构建运行中多个目标的 Kotlin 交叉编译
- c# - 我如何只构建与机器上安装的项目兼容的项目?
- linux-kernel - 从 linux 内核模块移动文件
- python - Python:循环可变数量的输入
- apache-kafka - Kafka Connect 能否使用来自单独的 Kerberized Kafka 实例的数据,然后路由到 Splunk?
- php - 在子域上托管 php 站点的旧部分?
- javascript - 我可以将数组转换为 RegEx 形式吗?