swift 脚本_Swift脚本和命令行参数

swift 脚本

In this tutorial, we’ll discuss how to create Scripts in Swift. Yes, Swift is a scripting language.
We’ll build swift script and run them from the command line while also passing arguments.

在本教程中,我们将讨论如何在Swift中创建脚本。 是的,Swift是一种脚本语言。
我们将构建swift脚本并从命令行运行它们,同时传递参数。

迅捷脚本 (Swift Scripts)

Apple has restricted Swift to for only building apps or to use it only on XCode. We can use Swift to run scripts from the terminal. We can also write a swift file outside XCode and compile it using the terminal.

Apple已将Swift限制为仅用于构建应用程序或仅在XCode上使用。 我们可以使用Swift从终端运行脚本。 我们还可以在XCode外部编写一个swift文件,并使用终端对其进行编译。

A script is just a code that is interpreted on the fly whereas compiled codes are converted into an executable binary to run.
脚本只是即时解释的代码,而编译后的代码将转换为可执行的二进制文件以运行。

创建一个Swift脚本 (Creating a Swift Script)

Note: You can write the following code using any editor and save it with the .swift extension.
We’re using XCode Command Line Project just to use the autocompletion features in our code.

注意 :您可以使用任何编辑器编写以下代码,并将其保存为.swift扩展名。
我们使用XCode命令行项目只是为了在代码中使用自动完成功能。

Set the project name from the next screen. You’re main.swift file is ready with Foundation Framework set in the import statement. We’ll use Cocoa Framework in our code since it contains the Application Kit alongside Foundation framework too.(Our cool scripts would have something to do with applications).

在下一个屏幕上设置项目名称。 您已经准备好main.swift文件,并在import语句中设置了Foundation Framework。 我们将在代码中使用Cocoa Framework,因为它也包含Application Kit和Foundation Framework。(我们的漂亮脚本与应用程序有关)。

To make a Swift Script our first line should be shebang as shown below.

要制作Swift脚本,我们的第一行应该是shebang ,如下所示。

#!/usr/bin/env xcrun swift

shebang syntax begins with a # in scripts and is used to invoke our environment(Swift in this case) and to get the script running on the Linux Operating System.

shebang语法以脚本中的#开头,用于调用我们的环境(在本例中为Swift)并使脚本在Linux操作系统上运行。

(Code)

The below script is used to launch the application specified.

以下脚本用于启动指定的应用程序。

#!/usr/bin/env xcrun swift

import Cocoa

var myWorkspace = NSWorkspace.shared
myWorkspace.launchApplication("Android Studio")

NSWorkspace.shared returns an instance of the current workspace on your macbook.
launchApplication is used to launch the application specified.

NSWorkspace.shared返回NSWorkspace.shared上当前工作空间的实例。
launchApplication用于启动指定的应用程序。

运行脚本 (Running the Script)

To run the script we need to first make it executable.
Fire up your Terminal and run the chmod +x command on the Swift file.

要运行脚本,我们需要首先使其可执行。
启动您的终端并在Swift文件上运行chmod + x命令。

chmod +x /path/to/your/file/main.swift

Now to run the script just run the file from the path.

现在要运行脚本,只需从路径运行文件。

/path/to/your/file/main.swift

Since we’d specified Android Studio, in our case Android Studio gets launched as shown below.

由于我们指定了Android Studio,因此在我们的情况下,如下所示启动了Android Studio。

处理命令行参数 (Handling Command Line Arguments)

We can pass the commands after the main.swift.
main.swift is considered as the first argument
To get the arguments in our code, we use the following methods

我们可以在main.swift之后传递命令。
main.swift被认为是第一个参数
要在我们的代码中获取参数,我们使用以下方法

CommandLine.argc

This returns the count of arguments. An Integer.
The below command returns the String array of arguments entered.

这将返回参数计数。 整数。
以下命令返回输入的参数的String数组。

CommandLine.arguments //Array of Strings

CommandLine.arguments[0] //main.swift is the first argument.

从脚本启动多个应用程序 (Launching Multiple Applications From the Script)

Now let’s improve our script such that it launches multiple applications.
A different set of applications would be launched based on the argument name entered.
In our following swift script, we’ll create two Swift Arrays.
The first would contain a custom set of applications that are work related.
The second array would be read related.
We’ll store them in a Swift Dictionary with the keys as work and read respectively.

现在,让我们改进脚本,以启动多个应用程序。
根据输入的参数名称,将启动一组不同的应用程序。
在下面的swift脚本中,我们将创建两个Swift Arrays
第一个将包含一组与work相关的自定义应用程序。
第二个数组将被read相关。
我们将它们存储在Swift字典中,并将键分别作为workread

Our main.swift file code is given below.

我们的main.swift文件代码如下。

#!/usr/bin/env xcrun swift

import Cocoa

var count = CommandLine.argc

print("Number of arguments is \(count)")

var workMode : Array = ["Android Studio", "XCode"]
var readMode : Array = ["Google Chrome", "Notes","iBooks","A"]
var myDictionary = [String: Array<String>]()

myDictionary["work"] = workMode
myDictionary["read"] = readMode


if count > 1
{
    
let key = CommandLine.arguments[1]
    
let apps = myDictionary[key]
if apps?.count != nil{
let myWorkspace = NSWorkspace.shared
    for s in apps! {
myWorkspace.launchApplication(s)
}
}
else{
    print("Incorrect command")
    }
}
else{
   print("Missing command")
}

Our output returned when the script is run on the terminal is given below:

swift script command line arguments

下面是在终端上运行脚本时返回的输出:

Notice that the workMode array applications are launched. Android Studio and XCode.

注意,将启动workMode数组应用程序。 Android Studio和XCode。

That’s all for a short introduction of Swift scripting tool.

这就是对Swift脚本工具的简短介绍。

翻译自: https://www.journaldev.com/19593/swift-script-command-line-arguments

swift 脚本

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值