如何使用AppleScript检索Xcode产品信息?

时间:2011-12-07 14:41:17

标签: xcode applescript

我正在尝试创建一个自定义通用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)

4 个答案:

答案 0 :(得分: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应用程序执行此操作。在文件菜单chosse"打开字典"并选择申请。字典打开后,使用搜索字段很有用。例如,搜索"项目"然后突出显示对应于"类"的结果。这将向您展示您可以从项目中获得的内容。

所以你看我们有几种方法可以找出我们可以从应用程序中获得哪些信息。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

您还可以在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

答案 3 :(得分:0)

以下获取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/.* = //'"