0

I'm trying to create a custom generic Xcode behaviour, that once invoked will launch a script to generate AppleDoc documentation related to the current project opened in the ide.

I initially asked this question: Xcode 4: custom behavior does not execute my sh script? which helped me to understand what I was missing: it's not possible to use Xcode variables in external scripts! So, my new question is: how can I use AppleScript to retrieve the informations I need (Project name, company, paths...)? I'm pretty sure that this is possible, since Xcode is highly "scriptable", but so far I was able to retrieve only the name of the xcode project file:

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

The code above returns "MyProject.xcodeproj". I'm a totally newbie when it comes to AppleScript and it's hard for me to understand the reference in Xcode.sdef (the hierarchy seems pretty simple, but I'm unable to create the right script to retrieve what I want).

Can you give some hints or point me to a newbie-proof tutorial about AppleScript + Xcode?

(I didn't find nothing but "hello world" for Finder examples :P)

Community
  • 1
  • 1
daveoncode
  • 18,900
  • 15
  • 104
  • 159

4 Answers4

1

The easiest thing to do is get properties of an item. So for example run this to get the properties of the frontmost project.

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

You will see the properties are a "record". To access the items of a record you get them by their name. For example you can get the "real path" of the project...

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

Finally, when you look at the sdef file you do that using Applescript Editor application. Under the file menu chosse "open dictionary" and choose the application. Once the dictionary is open it's useful to use the search field. For example search for "project" and then highlight the result corresponding to the "class". This will show you what you can get from a project.

So you see we have a few ways to find out what information we can get from an application.

regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Just as I was writing exactly the same thing :). You can also condense the code between the `tell app "Xcode" ... end tell` with `first project's properties` or `first project's real path` – Kassym Dorsel Dec 07 '11 at 16:12
  • Great!!! This is indeed a better approach... now as a final step I have to learn how to invoke my sh script from the AppleScript created :) – daveoncode Dec 07 '11 at 16:34
  • To invoke a shell script... do shell script "/path/to/script". You can also pass parameters like other shell commands if needed. – regulus6633 Dec 07 '11 at 20:00
  • Since Xcode 5 you need to use `tell active workspace document` before accessing the project. – Dave Oct 22 '20 at 16:26
1

After some hours of tests, I obtained the info I was looking for in this way:

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
daveoncode
  • 18,900
  • 15
  • 104
  • 159
  • 1
    Xcode 8 removed `organization name` and `project directory`. To get the path just use `path` on the workspace document. – Dave Oct 22 '20 at 16:25
0

You can also use BASH scripting in Applescript to get information from XCODE. For instance:

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

Handy for getting at Xcode environment variables using the Build Setting predefined words found at:

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

P.M.
  • 436
  • 6
  • 12
0

The following gets the project path in 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

For some build settings such as SDKROOT it only returned "iphone", so calling xcodebuild -project Project.xcodeproj -showBuildSettings might be more accurate:

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/.* = //'"
Dave
  • 479
  • 3
  • 13