如何以编程方式在Mac OS X上设置应用程序包以在用户登录时运行?
基本上,相当于HKCU\Software\Microsoft\Windows\CurrentVersion\Run
Windows 中的注册表项.
我不知道为什么这个方法返回一个空字符串:
- (NSString *)installedGitLocation {
NSString *launchPath = @"/usr/bin/which";
// Set up the task
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:launchPath];
NSArray *args = [NSArray arrayWithObject:@"git"];
[task setArguments:args];
// Set the output pipe.
NSPipe *outPipe = [[NSPipe alloc] init];
[task setStandardOutput:outPipe];
[task launch];
NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile];
NSString *path = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
return path;
}
Run Code Online (Sandbox Code Playgroud)
如果不是@"git"
作为参数传递,我传递@"which"
我/usr/bin/which
按预期返回.所以至少原理是有效的.
从终端
$ which which
$ /usr/bin/which
$
$ which git
$ /usr/local/git/bin/git
Run Code Online (Sandbox Code Playgroud)
所以它在那里工作. …
我正在尝试创建一个菜单栏应用程序来隐藏桌面图标和希望其他各种东西,主要是为了了解有关 Swift 的更多信息,但由于某种原因我无法让它工作。当我运行此程序并单击其中一个菜单项时,没有任何反应,并且在控制台中收到此警告:
killall: warning: kill -TERM 15175: Operation not permitted
Run Code Online (Sandbox Code Playgroud)
其他命令有效,但我在“killall”上尝试的任何变体都会吐出类似上面的内容。目前我的代码如下所示:
@discardableResult
func killStuff(_ args: String...) -> Int32 {
let task = Process()
let pipe = Pipe()
task.launchPath = "/usr/bin/killall"
task.arguments = args
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8) {
print(output)
}
return task.terminationStatus
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了此处、此处以及我在 Google 上找到的已接受解决方案的多种变体,但我一直收到相同的“不允许操作”。当我在 Xcode Playground 中运行相同的代码时,它工作得很好。
提前致谢!
复制自如何在 swift 脚本中运行终端命令?(例如xcodebuild):
import Foundation
@discardableResult
func shell(_ args: String...) -> Int32 {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
shell("ls")
shell("xcodebuild", "-workspace", "myApp.xcworkspace")
Run Code Online (Sandbox Code Playgroud)
这看起来很整洁。我只是想知道如何$PWD
为进程设置环境变量(task
在此处命名...)。
我尝试了以下方法:
import Foundation
@discardableResult
func execCommand(_ args: String...) -> Int32 {
let process = Process()
process.launchPath = "/usr/bin/env"
process.environment = ["PWD": "/Users"]
if let env = process.environment {
print(env["PWD"] ?? "Unknown")
} else {
print("Environment not available!")
}
process.arguments = args …
Run Code Online (Sandbox Code Playgroud) 我有一个用 swift 编写的命令行工具,可以编辑 xcode 项目设置。
我需要能够从 .entitlements 文件中删除“密钥”。
具体情况是我有一些项目不支持关联域但我需要删除整个
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:xxx.page.link</string>
</array>
Run Code Online (Sandbox Code Playgroud)
以优雅的方式从文件中删除,因为仅将数组留空意味着项目指示使用关联的域,但事实并非如此。
简而言之,使用 swift 从 plist 中删除一个键。