【问题标题】:How to run an Automator Workflow/Service from Cocoa app?如何从 Cocoa 应用程序运行 Automator 工作流程/服务?
【发布时间】:2017-03-26 01:53:04
【问题描述】:

所以我尝试学习 Swift 以在其中重新创建我的程序,但效果并不好,而且我也没有走得太远。尝试通过调用函数从 Obj-C++ 源代码运行我的 C++ 函数,但没有成功,并且项目在我第一次关闭后拒绝再次打开。我一开始就觉得面向对象编程不是很直观,所以我想避免使用 Obj-C。

我已经有一个 Automator 独立工作流程和一个服务(做同样的事情),它获取我需要的程序,显示确认,在带有标准输出的终端窗口中运行程序,并在退出前显示通知。这就是按下特定按钮时我需要它做的一切。

那么我将如何将此按钮链接到 Swift 中的 Automator func run() 块?我知道需要使用的命令,但就像我说的那样,我发现面向对象编程不是很直观,所以我需要知道使用该函数的上下文。在实践中,以下块是否足够,还是需要更多规范?

@IBOutlet weak var testButton(NSButton!)
@IBAction testButton(_ sender: AnyObject)
  {
   let guard Bundle.main.path(forName: "test",forType:"workflow")
   else
     {
      print("Could not find 'test.workflow'")
      return
     } 
   let URL="//location of file"
   class func run(at: URL, withInput: nil)
  }

我是否遗漏了有关如何执行此操作的内容,或者以上内容是否足够?其次,有人可以举例说明文件位于“资源”文件夹中的文件 URL 的格式吗? class 还会保留单词类还是我应该指定自定义类?有人可以在实践中给我这个块/概念的真实示例吗?

【问题讨论】:

    标签: swift cocoa service workflow automator


    【解决方案1】:

    这是一个应该可以工作的testButton 函数:

    @IBAction func testButton(_ sender: AnyObject) {
        guard let workflowPath = Bundle.main.path(forResource: "test", ofType: "workflow") else {
            print("Workflow resource not found")
            return
        }
    
        let workflowURL = URL(fileURLWithPath: workflowPath)
        do {
            try AMWorkflow.run(at:workflowURL, withInput: nil)
        } catch {
            print("Error running workflow: \(error)")
        }
    }
    

    注意事项:

    1. 您需要在源文件顶部添加import Automator,以便 Swift 编译器识别 AMWorkflow
    2. 您需要将 test.workflow 添加到您的项目中

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2017-06-04
      相关资源
      最近更新 更多