通過ScriptingBridge獲取Xcode 最近打開工程文件

如題继找,實(shí)現(xiàn)步驟如下:

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);
    }];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蕊连,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子州胳,更是在濱河造成了極大的恐慌蓖议,老刑警劉巖听想,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件仲闽,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡喻奥,警方通過查閱死者的電腦和手機(jī)席纽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來撞蚕,“玉大人润梯,你說我怎么就攤上這事∩茫” “怎么了纺铭?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)刀疙。 經(jīng)常有香客問我舶赔,道長(zhǎng),這世上最難降的妖魔是什么谦秧? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任竟纳,我火速辦了婚禮,結(jié)果婚禮上疚鲤,老公的妹妹穿的比我還像新娘锥累。我一直安慰自己,他們只是感情好集歇,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布桶略。 她就那樣靜靜地躺著,像睡著了一般诲宇。 火紅的嫁衣襯著肌膚如雪际歼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天焕窝,我揣著相機(jī)與錄音蹬挺,去河邊找鬼维贺。 笑死它掂,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播虐秋,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼榕茧,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了客给?” 一聲冷哼從身側(cè)響起用押,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎靶剑,沒想到半個(gè)月后蜻拨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡桩引,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年缎讼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坑匠。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡血崭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出厘灼,到底是詐尸還是另有隱情夹纫,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布设凹,位于F島的核電站舰讹,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏围来。R本人自食惡果不足惜跺涤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望监透。 院中可真熱鬧桶错,春花似錦、人聲如沸胀蛮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽粪狼。三九已至退腥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間再榄,已是汗流浹背狡刘。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留困鸥,地道東北人嗅蔬。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓剑按,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親澜术。 傳聞我的和親對(duì)象是個(gè)殘疾皇子艺蝴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353