首页 > 解决方案 > 替换字符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

我应该如何解决这个问题?

标签: macosterminalapplescriptshexecutable-jar

解决方案


您需要将标准路径更改为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

一旦您拥有正确的语法,就不需要分隔符等。


推荐阅读