【问题标题】:How can I retrieve Xcode product informations using AppleScript?如何使用 AppleScript 检索 Xcode 产品信息?
【发布时间】:2011-12-07 14:41:16
【问题描述】:

我正在尝试创建一个自定义的通用 Xcode 行为,一旦被调用,它将启动一个脚本来生成与在 ide 中打开的当前项目相关的 AppleDoc 文档。

我最初问了这个问题: Xcode 4: custom behavior does not execute my sh script? 帮助我了解了我所缺少的内容:不可能在外部脚本中使用 Xcode variables 所以,我的新问题是:如何使用 AppleScript 来检索我需要的信息(项目名称、公司、路径...)? 我很确定这是可能的,因为 Xcode 是高度“可编写脚本的”,但到目前为止我只能检索到 xcode 项目文件的名称:

tell application "Xcode"
    tell active project document
        name
    end tell
end tell

上面的代码返回“MyProject.xcodeproj”。 在 AppleScript 方面,我是一个完全新手,我很难理解 Xcode.sdef 中的引用(层次结构看起来很简单,但我无法创建正确的脚本来检索我想要什么)。

你能给我一些提示或指向一个关于 AppleScript + Xcode 的新手证明教程吗?

(对于 Finder 示例,我只找到“hello world”:P)

【问题讨论】:

    标签: xcode applescript


    【解决方案1】:

    最简单的方法是获取项目的属性。所以例如运行这个来获取最前面项目的属性。

    tell application "Xcode"
        tell first project
            return properties
        end tell
    end tell
    

    您会看到属性是“记录”。要访问记录的项目,您可以通过它们的名称获取它们。比如可以获取项目的“真实路径”...

    tell application "Xcode"
        tell first project
            return real path
        end tell
    end tell
    

    最后,当您查看 sdef 文件时,您可以使用 Applescript Editor 应用程序进行操作。在文件菜单下选择“打开字典”并选择应用程序。字典打开后,使用搜索字段很有用。例如搜索“项目”,然后突出显示与“类”对应的结果。这将向您展示您可以从项目中获得什么。

    所以您知道我们有几种方法可以找出我们可以从应用程序中获取哪些信息。

    【讨论】:

    • 正如我写的完全一样的东西:)。您还可以将tell app "Xcode" ... end tellfirst project's propertiesfirst project's real path 之间的代码压缩
    • 太棒了!!!这确实是一种更好的方法......现在作为最后一步,我必须学习如何从创建的 AppleScript 中调用我的 sh 脚本:)
    • 调用 shell 脚本...执行 shell script "/path/to/script"。如果需要,您还可以像其他 shell 命令一样传递参数。
    • 从Xcode 5开始需要使用tell active workspace document才能访问项目。
    【解决方案2】:

    经过几个小时的测试,我通过这种方式获得了我正在寻找的信息:

    tell application "Xcode"
        tell active workspace document
            set firstProject to (get first project)
            set projectID to (get id of firstProject)
            set organizationName to (get organization name of firstProject)
            set projectDirectory to (get project directory of firstProject)
            set firstTarget to (get first target of firstProject)
            set targetName to (get name of firstTarget)
            ("id: " & projectID & "organizationName: " & organizationName & " directory: " & projectDirectory & " targetName: " & targetName)
    
        end tell
    end tell
    

    【讨论】:

    • Xcode 8 删除了 organization nameproject directory。要获取路径,只需在工作区文档上使用 path
    【解决方案3】:

    您还可以在 Applescript 中使用 BASH 脚本从 XCODE 获取信息。例如:

    #get the product name from Xcode
    set {product_name} to words of (do shell script "printf \"%s\", $PRODUCT_NAME")
    

    使用以下位置的 Build Setting 预定义词获取 Xcode 环境变量非常方便:

    https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html

    【讨论】:

      【解决方案4】:

      以下获取Xcode 9-12中的项目路径:

      tell application "Xcode"
        tell active workspace document
          set my_project to first project
          set my_build_configuration to item 1 of build configuration of my_project
          set PROJECT_DIR to value of resolved build setting "PROJECT_DIR" of my_build_configuration
          PROJECT_DIR
        end tell
      end tell
      

      对于某些构建设置,例如SDKROOT,它只返回“iphone”,因此调用xcodebuild -project Project.xcodeproj -showBuildSettings 可能更准确:

      set working_directory to "/path/to/project"
      set project_file to "Project.xcodeproj"
      set build_setting to "SDKROOT"
      
      do shell script "cd " & quoted form of working_directory & " && xcodebuild -project " & quoted form of project_file & " -showBuildSettings | grep " & build_setting & " | sed 's/.* = //'"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-09
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 2011-04-21
        • 1970-01-01
        • 1970-01-01
        • 2021-11-19
        相关资源
        最近更新 更多