2009-10-03 27 views
10

我試圖構建一個xcode項目並通過iPhone模擬器通過applescript運行它。我知道xcodebuild但它不會讓你在模擬器中運行應用程序。我已經得到相當接近與下面的腳本...通過AppleScript構建和運行xcode項目

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。

+0

您可能會提及您正在使用的Xcode版本。我在Xcode 3.2中通過AppleScript設置活動SDK沒有問題。 – 2009-10-04 16:38:02

+0

我也使用xcode 3.2,但它不起作用(除非我將iphonesimulator3.1設置爲xcode中的基本SDK) – probablyCorey 2009-10-04 23:20:39

+0

看來,這些類型的AppleScripts不再適用於Xcode 4. – ThomasW 2012-02-06 10:13:34

回答

12

這是一個竅門......你必須設置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 
+0

如果我可以因爲「瘋狂」的評論,我會投票給你。 – rein 2011-11-06 00:57:45

+0

這不是瘋狂的,它只是如何在原始Mac OS中指定路徑(AppleScript源於此)。請注意,與Unix相反,相對路徑指定_with_前導冒號(絕對路徑沒有)和父路徑由空段指定(_e.g._,「:: src」==「../src」 )。 – fraveydank 2014-03-27 15:35:50

1

如果set active SDK命令不能按預期工作,解決方法是創建另一個名爲「Debug-Simulator」的構建配置(在項目設置中的Xcode中),並將新配置中的基礎SDK設置爲iphonesimulator3 0.0。這將允許您通過選擇構建配置來選擇SDK(如果它適用於AppleScript)。

3

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

+0

我已經完成那,但它不能解決在iPhone模擬器中運行應用程序的問題。看來只有xcode能夠做到這一點。 – probablyCorey 2009-10-04 18:03:28