通过AppleScript构建和运行Xcode项目

11

我正在尝试通过AppleScript构建Xcode项目并通过iPhone模拟器运行它。我知道xcodebuild,但它不能让你在模拟器中运行应用程序。我已经通过下面的脚本接近了这个目标...

tell application "Xcode"
  set targetProject to project of active project document

  tell targetProject
    set active build configuration type to build configuration type "Debug"
    set active SDK to "iphonesimulator3.0"
  end tell

  if (build targetProject) is equal to "Build succeeded" then
    launch targetProject
  end if
end tell

... 但是构建命令似乎不遵守活动SDK属性,它总是默认为项目的基础SDK设置(例如 iPhoneOS3.0 而不是 iPhonesimulator3.0)。

有没有办法告诉构建命令使用特定的SDK?我正在 Snow Leopard 上使用 Xcode 3.2。


你可以提一下你正在使用哪个Xcode版本。在Xcode 3.2中,我通过AppleScript设置活动SDK没有遇到任何问题。 - Nicholas Riley
我也在使用Xcode 3.2,但它不起作用(除非我在Xcode中将iphonesimulator3.1设置为基本SDK)。 - probablyCorey
看起来这些类型的AppleScripts不再适用于Xcode 4。 - ThomasW
4个回答

13

这里有一个诀窍……你需要设置SDKROOT构建设置。这是我使用的zsh脚本,可以在当前层次结构中找到xcode项目,构建它,并通过xcode运行它。

#!/bin/zsh

BUILD_PATH=$(dirname $0)

while [[ -z $BUILD_FILE && $BUILD_PATH != "/" ]]; do
    BUILD_FILE=$(find $BUILD_PATH -name '*.xcodeproj' -maxdepth 1)
    BUILD_PATH=$(dirname $BUILD_PATH)
done

if [[ -z $BUILD_FILE ]]; then
    echo "Couldn't find an xcode project file in directory"
    exit 1
fi

# Applescript likes's : instead of / (because it's insane)
BUILD_FILE=${BUILD_FILE//\//:}

# Find the latest Simulator SDK
SIMULATOR_SDKS=( /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/*.sdk )

SIMULATOR_SDK=${SIMULATOR_SDKS[-1]} 
SIMULATOR_SDK_STRING=$(basename ${(L)SIMULATOR_SDK%.[a-z]*})

if [[ -z $SIMULATOR_SDK ]]; then
    echo "Couldn't find a simulator SDK"
    exit 1
fi


osascript <<SCRIPT
application "iPhone Simulator" quit
application "iPhone Simulator" activate

tell application "Xcode"
    open "$BUILD_FILE"
    set targetProject to project of active project document

    tell targetProject
        set active build configuration type to build configuration type "Debug"
        set active SDK to "$SIMULATOR_SDK_STRING"
        set value of build setting "SDKROOT" of build configuration "Debug" of active target to "$SIMULATOR_SDK"

        if (build targetProject) is equal to "Build succeeded" then
            launch targetProject
        else
            application "iPhone Simulator" quit
        end if
    end tell
end tell
SCRIPT

如果有可能的话,因为那条评论太“疯狂”了,我会给你更多的赞。 - rein
这并不是疯狂的,而只是在原始的 Mac OS(AppleScript 的起源)中指定路径的方式。请注意,与 Unix 相反,相对路径是使用前导冒号指定的(绝对路径没有前导冒号),父路径是通过空段指定的(例如,“::src” ==“../src”)。 - fraveydank

3
另一个考虑的选项是使用Applescript启动一个执行xcodebuild程序的shell脚本。xcodebuild允许您指定特定的目标、配置、sdk等。当我需要通过SSH登录到构建服务器并重新构建项目时,我经常使用它。

我已经做了那个,但它并没有解决在 iPhone 模拟器中运行应用程序的问题。似乎只有 Xcode 能够做到这一点。 - probablyCorey

2

这个iphonesim项目为iPhone模拟器提供了一个命令行启动器。


1
如果set active SDK命令不按预期工作,则解决方法是在Xcode项目设置中创建另一个构建配置,名为“Debug-Simulator”,并将新配置中的基本SDK设置为iphonesimulator3.0。这将允许您通过选择构建配置(如果在AppleScript中起作用)来选择SDK。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接