macos - 替换字符applescript时出错
问题描述
我正在尝试制作一个以applescript应用程序形式执行可运行Jar文件的脚本。我在applescript方面不是很有经验,所以我大部分时间都在使用在线片段。我遇到的第一个问题是目录自动格式化为使用“:”而不是斜线(例如“usr:bin”而不是“usr/bin”)所以我发现应该用一个替换: /。但是,这每次都会产生错误。
我几乎没有找到关于如何解决我的错误的信息,而且我发现的内容非常针对这些情况,对我来说没有多大意义。
这是我的代码:
tell application "Finder"
set current_path to container of (path to me) as alias
end tell
set AppleScript's text item delimiters to ":"
set currPath to text items of current_path
set AppleScript's text item delimiters to "/"
set current_path to currPath as string
set AppleScript's text item delimiters to {""}
currPath
tell application "Terminal"
activate
set currentTab to do script ("cd " & current_path)
set currentTab to do script ("java -jar Inoisulcnoc Pre-Alpha 2")
end tell
我每次得到的错误说:
错误“无法获取别名 \"Macintosh HD:Users:knotsnappy:Desktop:Inoisulcnoc Pre-Alpha 2:\"的每个文本项。” 别名“Macintosh HD:Users:knotsnappy:Desktop:Inoisulcnoc Pre-Alpha 2:”的每个 文本项中的编号 -1728
我应该如何解决这个问题?
解决方案
您需要将标准路径更改为POSIX
路径。
tell application "Finder"
set current_path to (POSIX path of (container of (path to me) as alias))
end tell
tell application "Terminal"
activate
set currentTab to do script ("cd " & current_path)
set currentTab to do script ("java -jar Inoisulcnoc Pre-Alpha 2")
end tell
一旦您拥有正确的语法,就不需要分隔符等。
推荐阅读
- c# - 在网络编程中如何最好地处理图像和泛型类型
- php - 在 Laravel ui auth forgot password 表单中更改输入名称
- java - 带有片段的空指针异常 - 尝试调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)”
- c - gdb 运行程序后自动退出
- r - 删除字符串中的游程重复数字和 NA
- android - 使用 Fetch 库 Android 为组下载显示单个 ProgressBar
- javascript - 如何将按钮放在输入字段中?
- python - 如何在 python 中加载 Electron + EEL
- wpf - 自定义 WPF DataGrid 从 ViewModel 中手动选择一行或多行
- html - 如何迭代对象以在 Angular 中打印这种类型的 UI?