macos - 在 AppleScript 中的文本替换中使用反斜杠
问题描述
我目前正在尝试创建一个 AppleScript 应用程序,它将一个文件夹从其“内容”文件夹复制到用户的桌面。我现在的代码是:
set theFile to path to me as string
set hfsme to theFile & "Contents:Folder"
set posixPath to POSIX path of hfsme
set contentfolder to posixPath
set AppleScript's text item delimiters to " "
set textItem to text items of contentfolder
set AppleScript's text item delimiters to "\\ "
set contentfolder to textItem as string
set AppleScript's text item delimiters to {""}
do shell script "cp -a " & contentfolder & " $HOME/Desktop"
但是,我在运行时收到以下错误:
“cp:/Users/myusername/Desktop/Test Application.app/Contents/Folder:没有这样的文件或目录”
我认为这是因为我希望复制位置是:
/Users/myusername/Desktop/Test\ Application.app/Contents/Folder
但是脚本未能在 shell 命令所需的空格之前添加反斜杠。
我的问题是:如何用 "\" 替换变量中的空格?
PS 在建议简单地将应用程序重命名为不带空格的名称之前,拥有带空格的应用程序名称对我来说非常重要。我也尝试过使用 AppleScript 的“重复”命令,但无法让它工作,但我愿意接受建议!(请记住,我希望这个脚本也可以在其他人的计算机上工作,这意味着我不能假设我已经知道用户的用户名。
预先感谢您的任何帮助!
亲切的问候,汤姆
解决方案
只需添加quoted form of
,它以非常智能(且可靠)的方式处理报价。
set theFile to POSIX path of (path to me)
set contentfolder to theFile & "Contents/Folder"
do shell script "cp -a " & quoted form of contentfolder & " $HOME/Desktop"
如果do shell script
不支持$HOME
写
do shell script "cp -a " & quoted form of contentfolder & space & quoted from of POSIX path of (path to desktop)
推荐阅读
- jquery - 如何将 JSON 对象数组组合在一起
- python - 在 Numpy 数组中查找子列表的索引
- python - 时间不定,为什么?
- c# - 在 Click 事件上更改按钮的 BackColor 会导致悬停颜色发生变化
- php - 如何输入提示价格?
- json - Apache Spark:Kafka 以自定义格式写入
- javascript - 在Javascript中的P元素中添加字符串
- python - 如何通过 Python 中的 Google Sheets API v4 调用电子表格中的特定工作表
- gcc - Binutils 和 GDB 无法在 x86_64 架构的 macOS Mojave 上构建
- java - 如何在 Java 中覆盖 CountDownLatch 的 await() 方法?