簡體   English   中英

通過AppleScript構建並運行xcode項目

[英]Build and Run an xcode project via AppleScript

我正在嘗試構建一個xcode項目並通過iPhone模擬器通過AppleScript運行它。 我知道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

...但是build命令似乎不遵守活動的SDK屬性,它總是默認為項目的基本SDK設置(例如iPhoneOS3.0而不是iPhonesimulator3.0)

有沒有辦法告訴build命令使用特定的SDK? 我在雪豹上使用xcode 3.2。

這是訣竅......您必須設置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

另一個需要考慮的選擇是使用Applescript啟動執行xcodebuild程序的shell腳本。 xcodebuild允許你指定像特定目標,配置,sdk等的東西。當我必須SSH到構建服務器並重建項目時,我一直使用它。

iphonesim項目為您提供了iPhone模擬器的命令行啟動器。

如果set active SDK命令無法按預期工作,則解決方法是創建另一個名為“Debug-Simulator”的構建配置(在項目設置中的Xcode中),並將新配置中的基本SDK設置為iphonesimulator3.0 。 這將允許您通過選擇構建配置(如果它在AppleScript中工作)來選擇SDK。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM