如何在终端命令中为路径创建空格?

时间:2021-12-05 00:06:11

SOLVED

I needed to add two extra "\" so this line looks like this:

我需要添加两个额外的“\”,所以这一行看起来像这样:

  let finalString = removeExtension.stringByReplacingOccurrencesOfString(" ", withString: "\\\\ ")

I have an app written in swift which runs a terminal command (unix) that opens and closes an app.

我有一个用swift编写的应用程序,它运行一个打开和关闭应用程序的终端命令(unix)。

When the app selected is one word like "notes.app", the application works perfectly fine, however whenever it's an app with two words like "App Store.app", my program doesn't seem to be executing the commands correctly.

当选择的应用程序是一个单词,如“notes.app”,应用程序工作得很好,但无论什么时候它是一个有两个单词的应用程序,如“App Store.app”,我的程序似乎没有正确执行命令。

So, this works:

所以,这有效:

 clientUsed = "notes"
 let killCommand = "do shell script \"killall \(clientUsed)\""
 let openCommand = "do shell script \"open --hide --background /Applications/\(clientUsed).app\""

but this doesn't:

但这不是:

 clientUsed = "App\ Store"
 let killCommand = "do shell script \"killall \(clientUsed)\""
 let openCommand = "do shell script \"open --hide --background /Applications/\(clientUsed).app\""

Does anyone with unix, applescript or swift experience know why this is happening? I don't know if it's taking the wrong data in because i've written the script wrong.

有unix,applescript或swift经验的人都知道为什么会这样吗?我不知道它是否正在使用错误的数据,因为我写错了脚本。

For extra info if it helps, the variable clientUsed is determined by a NSOpenPanel() URL. Here is that code:

有关额外信息,如果有帮助,变量clientUsed由NSOpenPanel()URL确定。这是代码:

selectedApp = openPanel.URL!
var lastComponent = selectedApp?.lastPathComponent
var removeOptional : String = lastComponent!
let removeExtension = removeOptional.stringByReplacingOccurrencesOfString(".app", withString: "")
let finalString = removeExtension.stringByReplacingOccurrencesOfString(" ", withString: "\\ ")
self.clientUsed = finalString

2 个解决方案

#1


You don't need to delimit spaces inside a quote (as a string).

您不需要在引号内分隔空格(作为字符串)。

clientUsed = "App Store"
let killCommand = "do shell script \"killall \(clientUsed)\""
let openCommand = "do shell script \"open --hide --background /Applications/\(clientUsed).app\""

#2


Why in the world are you running killall via shell via AppleScript via Swift? Just use [NSTask launchedTaskWithLaunchPath:arguments:] to execute it directly. [1]

为什么在世界上你通过AppleScript通过Swift运行killall?只需使用[NSTask launchTaskWithLaunchPath:arguments:]直接执行它。 [1]

As for launching other applications, use -[NSWorkspace launchApplication:] or -[NSWorkspace launchApplicationAtURL:options:configuration:error:].

至于启动其他应用程序,请使用 - [NSWorkspace launchApplication:]或 - [NSWorkspace launchApplicationAtURL:options:configuration:error:]。

--

[1] Be aware that on OS X you should only use kill/killall to terminate non-GUI or hung processes. The correct way to end a normally working GUI process is to send it a quit Apple event. Annoyingly, doing this directly from ObjC/Swift is a bit of a PITA these days due to NSAppleEventDescriptor's lack of a send method. You could use the AESendMessage function, but that's a legacy Carbon C API, so it may be simpler and safer to use NSAppleScript or just bridge directly into AppleScript via AppleScript-ObjC, which is easy to do from ObjC and also doable (if a bit more work) from Swift.

[1]请注意,在OS X上,您应该只使用kill / killall来终止非GUI或挂起的进程。结束正常工作的GUI过程的正确方法是向它发送退出Apple事件。令人讨厌的是,由于NSAppleEventDescriptor没有发送方法,直接从ObjC / Swift直接执行此操作现在有点像PITA。您可以使用AESendMessage函数,但这是一个传统的Carbon C API,因此使用NSAppleScript可能更简单,更安全,或者只是通过AppleScript-ObjC直接桥接到AppleScript,这很容易从ObjC做起,也可以做到(如果有的话)更多工作)来自Swift。

#1


You don't need to delimit spaces inside a quote (as a string).

您不需要在引号内分隔空格(作为字符串)。

clientUsed = "App Store"
let killCommand = "do shell script \"killall \(clientUsed)\""
let openCommand = "do shell script \"open --hide --background /Applications/\(clientUsed).app\""

#2


Why in the world are you running killall via shell via AppleScript via Swift? Just use [NSTask launchedTaskWithLaunchPath:arguments:] to execute it directly. [1]

为什么在世界上你通过AppleScript通过Swift运行killall?只需使用[NSTask launchTaskWithLaunchPath:arguments:]直接执行它。 [1]

As for launching other applications, use -[NSWorkspace launchApplication:] or -[NSWorkspace launchApplicationAtURL:options:configuration:error:].

至于启动其他应用程序,请使用 - [NSWorkspace launchApplication:]或 - [NSWorkspace launchApplicationAtURL:options:configuration:error:]。

--

[1] Be aware that on OS X you should only use kill/killall to terminate non-GUI or hung processes. The correct way to end a normally working GUI process is to send it a quit Apple event. Annoyingly, doing this directly from ObjC/Swift is a bit of a PITA these days due to NSAppleEventDescriptor's lack of a send method. You could use the AESendMessage function, but that's a legacy Carbon C API, so it may be simpler and safer to use NSAppleScript or just bridge directly into AppleScript via AppleScript-ObjC, which is easy to do from ObjC and also doable (if a bit more work) from Swift.

[1]请注意,在OS X上,您应该只使用kill / killall来终止非GUI或挂起的进程。结束正常工作的GUI过程的正确方法是向它发送退出Apple事件。令人讨厌的是,由于NSAppleEventDescriptor没有发送方法,直接从ObjC / Swift直接执行此操作现在有点像PITA。您可以使用AESendMessage函数,但这是一个传统的Carbon C API,因此使用NSAppleScript可能更简单,更安全,或者只是通过AppleScript-ObjC直接桥接到AppleScript,这很容易从ObjC做起,也可以做到(如果有的话)更多工作)来自Swift。