通过ScriptingBridge获取Xcode 最近打开工程文件

如题,实现步骤如下:

1.sdef命令获取生成Xcode的头文件 Xcode.h

/*
 * Xcode.h -- extracted using command:
 * sdef /Applications/Xcode.app | sdp -fh --basename Xcode
 */

#import <AppKit/AppKit.h>
#import <ScriptingBridge/ScriptingBridge.h>


@class XcodeApplication, XcodeDocument, XcodeWindow, XcodeFileDocument, XcodeTextDocument, XcodeSourceDocument, XcodeWorkspaceDocument, XcodeSchemeActionResult, XcodeSchemeActionIssue, XcodeBuildError, XcodeBuildWarning, XcodeAnalyzerIssue, XcodeTestFailure, XcodeScheme, XcodeRunDestination, XcodeDevice, XcodeBuildConfiguration, XcodeProject, XcodeBuildSetting, XcodeResolvedBuildSetting, XcodeTarget;

enum XcodeSaveOptions {
    XcodeSaveOptionsYes = 'yes ' /* Save the file. */,
    XcodeSaveOptionsNo = 'no  ' /* Do not save the file. */,
    XcodeSaveOptionsAsk = 'ask ' /* Ask the user whether or not to save the file. */
};
typedef enum XcodeSaveOptions XcodeSaveOptions;

// The status of a scheme action result object.
enum XcodeSchemeActionResultStatus {
    XcodeSchemeActionResultStatusNotYetStarted = 'srsn' /* The action has not yet started. */,
    XcodeSchemeActionResultStatusRunning = 'srsr' /* The action is in progress. */,
    XcodeSchemeActionResultStatusCancelled = 'srsc' /* The action was cancelled. */,
    XcodeSchemeActionResultStatusFailed = 'srsf' /* The action ran but did not complete successfully. */,
    XcodeSchemeActionResultStatusErrorOccurred = 'srse' /* The action was not able to run due to an error. */,
    XcodeSchemeActionResultStatusSucceeded = 'srss' /* The action succeeded. */
};
typedef enum XcodeSchemeActionResultStatus XcodeSchemeActionResultStatus;

@protocol XcodeGenericMethods

- (void) closeSaving:(XcodeSaveOptions)saving savingIn:(NSURL *)savingIn;  // Close a document.
- (void) delete;  // Delete an object.
- (void) moveTo:(SBObject *)to;  // Move an object to a new location.
- (XcodeSchemeActionResult *) build;  // Invoke the "build" scheme action. This command should be sent to a workspace document. The build will be performed using the workspace document's current active scheme and active run destination. This command does not wait for the action to complete; its progress can be tracked with the returned scheme action result.
- (XcodeSchemeActionResult *) clean;  // Invoke the "clean" scheme action. This command should be sent to a workspace document. The clean will be performed using the workspace document's current active scheme and active run destination. This command does not wait for the action to complete; its progress can be tracked with the returned scheme action result.
- (void) stop;  // Stop the active scheme action, if one is running. This command should be sent to a workspace document. This command does not wait for the action to stop.
- (XcodeSchemeActionResult *) runWithCommandLineArguments:(id)withCommandLineArguments withEnvironmentVariables:(id)withEnvironmentVariables;  // Invoke the "run" scheme action. This command should be sent to a workspace document. The run action will be performed using the workspace document's current active scheme and active run destination. This command does not wait for the action to complete; its progress can be tracked with the returned scheme action result.
- (XcodeSchemeActionResult *) testWithCommandLineArguments:(id)withCommandLineArguments withEnvironmentVariables:(id)withEnvironmentVariables;  // Invoke the "test" scheme action. This command should be sent to a workspace document. The test action will be performed using the workspace document's current active scheme and active run destination. This command does not wait for the action to complete; its progress can be tracked with the returned scheme action result.

@end



/*
 * Standard Suite
 */

// The application's top-level scripting object.
@interface XcodeApplication : SBApplication

- (SBElementArray<XcodeDocument *> *) documents;
- (SBElementArray<XcodeWindow *> *) windows;

@property (copy, readonly) NSString *name;  // The name of the application.
@property (readonly) BOOL frontmost;  // Is this the active application?
@property (copy, readonly) NSString *version;  // The version number of the application.

- (id) open:(id)x;  // Open a document.
- (void) quitSaving:(XcodeSaveOptions)saving;  // Quit the application.
- (BOOL) exists:(id)x;  // Verify that an object exists.

@end

// A document.
@interface XcodeDocument : SBObject <XcodeGenericMethods>

@property (copy, readonly) NSString *name;  // Its name.
@property (readonly) BOOL modified;  // Has it been modified since the last save?
@property (copy, readonly) NSURL *file;  // Its location on disk, if it has one.


@end

// A window.
@interface XcodeWindow : SBObject <XcodeGenericMethods>

@property (copy, readonly) NSString *name;  // The title of the window.
- (NSInteger) id;  // The unique identifier of the window.
@property NSInteger index;  // The index of the window, ordered front to back.
@property NSRect bounds;  // The bounding rectangle of the window.
@property (readonly) BOOL closeable;  // Does the window have a close button?
@property (readonly) BOOL miniaturizable;  // Does the window have a minimize button?
@property BOOL miniaturized;  // Is the window minimized right now?
@property (readonly) BOOL resizable;  // Can the window be resized?
@property BOOL visible;  // Is the window visible right now?
@property (readonly) BOOL zoomable;  // Does the window have a zoom button?
@property BOOL zoomed;  // Is the window zoomed right now?
@property (copy, readonly) XcodeDocument *document;  // The document whose contents are displayed in the window.


@end



/*
 * Xcode Application Suite
 */

// The Xcode application.
@interface XcodeApplication (XcodeApplicationSuite)

- (SBElementArray<XcodeFileDocument *> *) fileDocuments;
- (SBElementArray<XcodeSourceDocument *> *) sourceDocuments;
- (SBElementArray<XcodeWorkspaceDocument *> *) workspaceDocuments;

@property (copy) XcodeWorkspaceDocument *activeWorkspaceDocument;  // The active workspace document in Xcode.

@end



/*
 * Xcode Document Suite
 */

// An Xcode-compatible document.
@interface XcodeDocument (XcodeDocumentSuite)

@property (copy) NSString *path;  // The document's path.

@end

// A document that represents a file on disk. It also provides access to the window it appears in.
@interface XcodeFileDocument : XcodeDocument


@end

// A document that represents a text file on disk. It also provides access to the window it appears in.
@interface XcodeTextDocument : XcodeFileDocument

@property (copy) NSArray<NSNumber *> *selectedCharacterRange;  // The first and last character positions in the selection.
@property (copy) NSArray<NSNumber *> *selectedParagraphRange;  // The first and last paragraph positions that contain the selection.
@property (copy) NSString *text;  // The text of the text file referenced.
@property BOOL notifiesWhenClosing;  // Should Xcode notify other apps when this document is closed?


@end

// A document that represents a source file on disk. It also provides access to the window it appears in.
@interface XcodeSourceDocument : XcodeTextDocument


@end

// A document that represents a workspace on disk. Workspaces are the top-level container for almost all objects and commands in Xcode.
@interface XcodeWorkspaceDocument : XcodeDocument

- (SBElementArray<XcodeProject *> *) projects;
- (SBElementArray<XcodeScheme *> *) schemes;
- (SBElementArray<XcodeRunDestination *> *) runDestinations;

@property BOOL loaded;  // Whether the workspace document has finsished loading after being opened. Messages sent to a workspace document before it has loaded will result in errors.
@property (copy) XcodeScheme *activeScheme;  // The workspace's scheme that will be used for scheme actions.
@property (copy) XcodeRunDestination *activeRunDestination;  // The workspace's run destination that will be used for scheme actions.
@property (copy) XcodeSchemeActionResult *lastSchemeActionResult;  // The scheme action result for the last scheme action command issued to the workspace document.
@property (copy, readonly) NSURL *file;  // The workspace document's location on disk, if it has one.


@end



/*
 * Xcode Scheme Suite
 */

// An object describing the result of performing a scheme action command.
@interface XcodeSchemeActionResult : SBObject <XcodeGenericMethods>

- (SBElementArray<XcodeBuildError *> *) buildErrors;
- (SBElementArray<XcodeBuildWarning *> *) buildWarnings;
- (SBElementArray<XcodeAnalyzerIssue *> *) analyzerIssues;
- (SBElementArray<XcodeTestFailure *> *) testFailures;

- (NSString *) id;  // The unique identifier for the scheme.
@property (readonly) BOOL completed;  // Whether this scheme action has completed (sucessfully or otherwise) or not.
@property XcodeSchemeActionResultStatus status;  // Indicates the status of the scheme action.
@property (copy) NSString *errorMessage;  // If the result's status is "error occurred", this will be the error message; otherwise, this will be "missing value".
@property (copy) NSString *buildLog;  // If this scheme action performed a build, this will be the text of the build log.


@end

// An issue (like an error or warning) generated by a scheme action.
@interface XcodeSchemeActionIssue : SBObject <XcodeGenericMethods>

@property (copy) NSString *message;  // The text of the issue.
@property (copy) NSString *filePath;  // The file path where the issue occurred. This may be 'missing value' if the issue is not associated with a specific source file.
@property NSInteger startingLineNumber;  // The starting line number in the file where the issue occurred. This may be 'missing value' if the issue is not associated with a specific source file.
@property NSInteger endingLineNumber;  // The ending line number in the file where the issue occurred. This may be 'missing value' if the issue is not associated with a specific source file.
@property NSInteger startingColumnNumber;  // The starting column number in the file where the issue occurred. This may be 'missing value' if the issue is not associated with a specific source file.
@property NSInteger endingColumnNumber;  // The ending column number in the file where the issue occurred. This may be 'missing value' if the issue is not associated with a specific source file.


@end

// An error generated by a build.
@interface XcodeBuildError : XcodeSchemeActionIssue


@end

// A warning generated by a build.
@interface XcodeBuildWarning : XcodeSchemeActionIssue


@end

// A warning generated by the static analyzer.
@interface XcodeAnalyzerIssue : XcodeSchemeActionIssue


@end

// A failure from a test.
@interface XcodeTestFailure : XcodeSchemeActionIssue


@end

// A set of parameters for building, testing, launching or distributing the products of a workspace.
@interface XcodeScheme : SBObject <XcodeGenericMethods>

@property (copy, readonly) NSString *name;  // The name of the scheme.
- (NSString *) id;  // The unique identifier for the scheme.


@end

// An object which specifies parameters such as the device and architecture for which to perform a scheme action.
@interface XcodeRunDestination : SBObject <XcodeGenericMethods>

@property (copy, readonly) NSString *name;  // The name of the run destination, as displayed in Xcode's interface.
@property (copy, readonly) NSString *architecture;  // The architecture for which this run destination results in execution.
@property (copy, readonly) NSString *platform;  // The identifier of the platform which this run destination targets, such as "macosx", "iphoneos", "iphonesimulator", etc .
@property (copy, readonly) XcodeDevice *device;  // The physical or virtual device which this run destination targets.
@property (copy, readonly) XcodeDevice *companionDevice;  // If the run destination's device has a companion (e.g. a paired watch for a phone) which it will use, this is that device.


@end

// A device which can be used as the target for a scheme action, as part of a run destination.
@interface XcodeDevice : SBObject <XcodeGenericMethods>

@property (copy, readonly) NSString *name;  // The name of the device.
@property (copy, readonly) NSString *deviceIdentifier;  // A stable identifier for the device, as shown in Xcode's "Devices" window.
@property (copy, readonly) NSString *operatingSystemVersion;  // The version of the operating system installed on the device which this run destination targets.
@property (copy, readonly) NSString *deviceModel;  // The model of device (e.g. "iPad Air") which this run destination targets.
@property (readonly) BOOL generic;  // Whether this run destination is generic instead of representing a specific device. Most destinations are not generic, but a generic destination (such as "Generic iOS Device") will be available for some platforms if no physical devices are connected.


@end



/*
 * Xcode Project Suite
 */

// A set of build settings for a target or project. Each target in a project has the same named build configurations as the project.
@interface XcodeBuildConfiguration : SBObject <XcodeGenericMethods>

- (SBElementArray<XcodeBuildSetting *> *) buildSettings;
- (SBElementArray<XcodeResolvedBuildSetting *> *) resolvedBuildSettings;

- (NSString *) id;  // The unique identifier for the build configuration.
@property (copy, readonly) NSString *name;  // The name of the build configuration.


@end

// An Xcode project. Projects represent project files on disk and are always open in the context of a workspace document.
@interface XcodeProject : SBObject <XcodeGenericMethods>

- (SBElementArray<XcodeBuildConfiguration *> *) buildConfigurations;
- (SBElementArray<XcodeTarget *> *) targets;

@property (copy, readonly) NSString *name;  // The name of the project
- (NSString *) id;  // The unique identifier for the project.


@end

// A setting that controls how products are built.
@interface XcodeBuildSetting : SBObject <XcodeGenericMethods>

@property (copy) NSString *name;  // The unlocalized build setting name (e.g. DSTROOT).
@property (copy) NSString *value;  // A string value for the build setting.


@end

// An object that represents a resolved value for a build setting.
@interface XcodeResolvedBuildSetting : SBObject <XcodeGenericMethods>

@property (copy) NSString *name;  // The unlocalized build setting name (e.g. DSTROOT).
@property (copy) NSString *value;  // A string value for the build setting.


@end

// A target is a blueprint for building a product. Targets inherit build settings from their project if not overridden in the target.
@interface XcodeTarget : SBObject <XcodeGenericMethods>

- (SBElementArray<XcodeBuildConfiguration *> *) buildConfigurations;

@property (copy) NSString *name;  // The name of this target.
- (NSString *) id;  // The unique identifier for the target.
@property (copy, readonly) XcodeProject *project;  // The project that contains this target


@end

2. ScriptingBridge获取Xcode document

在工程中引入Xcode.h头文件和ScriptingBridge:

#import "Xcode.h"
#import <ScriptingBridge/ScriptingBridge.h>

使用如下方法即可获取到Xcode最近文档:

 XcodeApplication *xcode =[SBApplication applicationWithBundleIdentifier:@"com.apple.dt.Xcode"];

    NSArray * xcodeDoc = [xcode documents];
    [xcodeDoc enumerateObjectsUsingBlock:^(XcodeDocument*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSURL *fileURL =[obj file];
        NSLog(@"%@",fileURL);
    }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342