Applescripting Xcode
Have you wanted to automate your Xcode workflow, but couldn’t figure out exactly how to work the AppleScript commands? Well, here’s a whole object-oriented library of handlers for ya!
(Sorry I haven’t figured out a better way to format AppleScript code in a blog; any ideas, please let me know)
on target_object(the_target)
script target_obj
property target_ref : ""
to add_source_file(the_file)
tell application "Xcode"
add the_file to (get compile sources phase of target_ref)
end tell
end add_source_file
on set_header_search_paths_for_all_configurations(the_path)
tell application "Xcode"
(* configuration is Debug or Release *)
set value of build setting "HEADER_SEARCH_PATHS" of build configurations of target_ref to the_path
end tell
end set_header_search_paths_for_all_configurations
to add_run_script_phase(the_name, the_script)
tell application "Xcode"
tell target_ref
return make new run script phase with properties {name:the_name, shell path:"/bin/sh", shell script:the_script}
end tell
end tell
end add_run_script_phase
end script
tell application "Xcode"
set target_obj's target_ref to the_target
end tell
return target_obj
end target_object
on group_object(the_group)
script group_obj
property group_ref : ""
to add_file(the_path, the_file_name)
tell application "Xcode"
tell group_ref
return make new file reference with properties {full path:the_path, name:the_file_name}
end tell
end tell
end add_file
end script
set group_obj's group_ref to the_group
return group_obj
end group_object
on active_project()
script project_obj
property project_ref : ""
to add_group(group_name)
tell application "Xcode"
tell project_ref
-- Get project directory
set project_dir to project directory
-- Create new folder for group's files
tell application "Finder"
set file_lib to load script alias "Macintosh HD:Users:sean:Library:Scripts:My Library:Files.scpt"
make new folder at (file_lib's posix_string_to_hfs_file(project_dir)) with properties {name:group_name}
end tell
-- Create new group
tell root group
return my group_object(make new group with properties {name:group_name, path type:project relative, path:group_name} at beginning)
end tell
end tell
end tell
end add_group
to make_new_shell_tool_target(target_name)
tell application "Xcode"
tell project_ref
set unit_test_template to target template "BSD/Shell Tool"
return my target_object(make new target at end of targets with data unit_test_template with properties {name:target_name})
end tell
end tell
end make_new_shell_tool_target
end script
tell application "Xcode"
set project_obj's project_ref to project of active project document
end tell
return project_obj
end active_project
You can also download the scpt file here.
And here’s a script that uses the handlers.
Could you give an example of how to use this script?
Thank you
@Esteban Fortunately, it’s been a few years since I’ve used Xcode. Unfortunately, that means I don’t remember too much about this script :). Here’s some code (on Github) that uses the library, so you can see it in action.
HTH,
Sean