Skip to content

Commit

Permalink
Implement macOS modules using ScriptingBridge (#152)
Browse files Browse the repository at this point in the history
* Implement macOS modules using ScriptingBridge

* Fix reviewed issues
  • Loading branch information
AndreyBelym authored and kirovboris committed May 22, 2018
1 parent b50ce6f commit 8c43436
Show file tree
Hide file tree
Showing 50 changed files with 384 additions and 250 deletions.
Binary file added bin/mac/bring-to-front
Binary file not shown.
Binary file removed bin/mac/bring-to-front.scpt
Binary file not shown.
Binary file added bin/mac/close
Binary file not shown.
Binary file removed bin/mac/close.scpt
Binary file not shown.
Binary file added bin/mac/find-window
Binary file not shown.
Binary file removed bin/mac/find-window-cocoa
Binary file not shown.
Binary file removed bin/mac/find-window.scpt
Binary file not shown.
Binary file modified bin/mac/generate-thumbnail
100644 → 100755
Binary file not shown.
Binary file added bin/mac/get-window-bounds
Binary file not shown.
Binary file removed bin/mac/get-window-bounds.scpt
Binary file not shown.
Binary file modified bin/mac/get-window-max-bounds
Binary file not shown.
Binary file added bin/mac/get-window-size
Binary file not shown.
Binary file removed bin/mac/get-window-size.scpt
Binary file not shown.
Binary file modified bin/mac/open.scpt
Binary file not shown.
Binary file added bin/mac/resize
Binary file not shown.
Binary file removed bin/mac/resize.scpt
Binary file not shown.
Binary file modified bin/mac/screenshot
Binary file not shown.
Binary file added bin/mac/set-window-bounds
Binary file not shown.
Binary file removed bin/mac/set-window-bounds.scpt
Binary file not shown.
2 changes: 1 addition & 1 deletion src/api/bring-to-front.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default async function (windowDescriptor) {
if (OS.win)
bringWindowToFrontArguments = [windowDescription.hwnd];
else if (OS.mac)
bringWindowToFrontArguments = [windowDescription.windowId, windowDescription.bundleId];
bringWindowToFrontArguments = [windowDescription.processId, windowDescription.windowId];
else
return;

Expand Down
2 changes: 1 addition & 1 deletion src/api/close.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default async function (windowDescriptor) {
if (OS.win)
closeWindowArguments = [windowDescription.hwnd];
else if (OS.mac)
closeWindowArguments = [windowDescription.windowId, windowDescription.bundleId];
closeWindowArguments = [windowDescription.processId, windowDescription.windowId];
else if (OS.linux)
closeWindowArguments = [windowDescription.windowId];
else
Expand Down
10 changes: 5 additions & 5 deletions src/api/find-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ export default async function (pageTitle) {

try {
res = await execFile(BINARIES.findWindow, [pageTitle]);

if (OS.mac)
res += await execFile(BINARIES.findWindowCocoa, [pageTitle]);
}
catch (err) {
return null;
Expand All @@ -31,8 +28,11 @@ export default async function (pageTitle) {
if (OS.win)
return { hwnd: windowParams[0], browser: windowParams[1] };

if (OS.mac)
return { bundleId: windowParams[0], windowId: windowParams[1], cocoaId: windowParams[2] };
if (OS.mac) {
windowParams = windowParams.slice(windowParams.length - 4);

return { processId: windowParams[0], cocoaId: windowParams[1], windowId: windowParams[2] };
}

if (OS.linux)
return { windowId: windowParams[0] };
Expand Down
2 changes: 1 addition & 1 deletion src/api/get-window-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function (windowDescription) {
if (OS.win)
getWindowSizeArgs = [windowDescription.hwnd];
else if (OS.mac)
getWindowSizeArgs = [windowDescription.windowId, windowDescription.bundleId];
getWindowSizeArgs = [windowDescription.processId, windowDescription.windowId];
else if (OS.linux)
getWindowSizeArgs = [windowDescription.windowId];
else
Expand Down
16 changes: 8 additions & 8 deletions src/api/maximize.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import { execFile } from '../utils/exec';
import BINARIES from '../binaries';

function getBoundsFromString (boundsString) {
return boundsString.replace(/\n/g, '').split(', ');
return boundsString.split('\n');
}

async function getWindowBounds (windowId, bundleId) {
var boundsString = await execFile(BINARIES.getWindowBounds, [windowId, bundleId]);
async function getWindowBounds (processId, windowId) {
var boundsString = await execFile(BINARIES.getWindowBounds, [processId, windowId]);

return getBoundsFromString(boundsString);
}

async function getWindowMaxBounds (windowId, bundleId) {
var windowBounds = await getWindowBounds(windowId, bundleId);
async function getWindowMaxBounds (processId, windowId) {
var windowBounds = await getWindowBounds(processId, windowId);
var maxBoundsString = await execFile(BINARIES.getWindowMaxBounds, windowBounds);

return getBoundsFromString(maxBoundsString);
}

async function maximizeWindowMac (windowDescription) {
var { windowId, bundleId } = windowDescription;
var windowBounds = await getWindowMaxBounds(windowId, bundleId);
var { processId, windowId } = windowDescription;
var windowBounds = await getWindowMaxBounds(processId, windowId);

await execFile(BINARIES.setWindowBounds, [windowId, bundleId].concat(windowBounds));
await execFile(BINARIES.setWindowBounds, [processId, windowId].concat(windowBounds));
}
/**
* Maximizes the specified browser window.
Expand Down
2 changes: 1 addition & 1 deletion src/api/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default async function (windowDescriptor, currentWidth, currentHeight, wi
if (OS.win)
resizeArguments = [windowDescription.hwnd];
else if (OS.mac)
resizeArguments = [windowDescription.windowId, windowDescription.bundleId];
resizeArguments = [windowDescription.processId, windowDescription.windowId];
else if (OS.linux)
resizeArguments = [windowDescription.windowId];
else
Expand Down
15 changes: 7 additions & 8 deletions src/binaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ if (OS.win) {
else if (OS.mac) {
BINARIES = {
open: toAbsPath('../bin/mac/open.scpt'),
findWindow: toAbsPath('../bin/mac/find-window.scpt'),
findWindowCocoa: toAbsPath('../bin/mac/find-window-cocoa'),
getWindowSize: toAbsPath('../bin/mac/get-window-size.scpt'),
getWindowBounds: toAbsPath('../bin/mac/get-window-bounds.scpt'),
findWindow: toAbsPath('../bin/mac/find-window'),
getWindowSize: toAbsPath('../bin/mac/get-window-size'),
getWindowBounds: toAbsPath('../bin/mac/get-window-bounds'),
getWindowMaxBounds: toAbsPath('../bin/mac/get-window-max-bounds'),
setWindowBounds: toAbsPath('../bin/mac/set-window-bounds.scpt'),
close: toAbsPath('../bin/mac/close.scpt'),
setWindowBounds: toAbsPath('../bin/mac/set-window-bounds'),
close: toAbsPath('../bin/mac/close'),
screenshot: toAbsPath('../bin/mac/screenshot'),
resize: toAbsPath('../bin/mac/resize.scpt'),
resize: toAbsPath('../bin/mac/resize'),
generateThumbnail: toAbsPath('../bin/mac/generate-thumbnail'),
bringToFront: toAbsPath('../bin/mac/bring-to-front.scpt')
bringToFront: toAbsPath('../bin/mac/bring-to-front')
};
}
else if (OS.linux) {
Expand Down
4 changes: 2 additions & 2 deletions src/natives/bring-to-front/mac/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
all: clean build
clean:
rm "${DEST}/bring-to-front.scpt" || true
rm "${DEST}/bring-to-front" || true
build:
mkdir "${DEST}" || true
osacompile -o "${DEST}/bring-to-front.scpt" bring-to-front.applescript
clang -o "${DEST}/bring-to-front" -framework Cocoa -framework ScriptingBridge -framework AppKit bring-to-front.m
15 changes: 0 additions & 15 deletions src/natives/bring-to-front/mac/bring-to-front.applescript

This file was deleted.

36 changes: 36 additions & 0 deletions src/natives/bring-to-front/mac/bring-to-front.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// bring-to-front.m
// Place a window in foreground
//

#import "AppKit/AppKit.h"
#include "../../utils/mac/utils.h"


const int APP_ACTIVATION_DELAY = 100000;

int main (int argc, const char * argv[]) {
if (argc < 3) {
printf("Incorrect arguments\n");
return 1;
}

@autoreleasepool {
NSString *processId = [NSString stringWithUTF8String:argv[1]];
NSString *windowId = [NSString stringWithUTF8String:argv[2]];

@try {
[[NSRunningApplication runningApplicationWithProcessIdentifier: [processId intValue]] activateWithOptions: NSApplicationActivateAllWindows|NSApplicationActivateIgnoringOtherApps];

usleep(APP_ACTIVATION_DELAY);

[getWindowOfProcess(processId, windowId) setIndex: 1];
}
@catch (NSException *exception) {
return 0;
}
}

return 0;
}

4 changes: 2 additions & 2 deletions src/natives/close/mac/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
all: clean build
clean:
rm "${DEST}/close.scpt" || true
rm "${DEST}/close" || true
build:
mkdir "${DEST}" || true
osacompile -o "${DEST}/close.scpt" close.applescript
clang -o "${DEST}/close" -framework Cocoa -framework ScriptingBridge close.m
7 changes: 0 additions & 7 deletions src/natives/close/mac/close.applescript

This file was deleted.

45 changes: 45 additions & 0 deletions src/natives/close/mac/close.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// close.m
// Close a window
//

#include "../../utils/mac/utils.h"

enum CloseOptions {
CloseOptionsYes = 'yes ' /* Save the file. */,
CloseOptionsNo = 'no ' /* Do not save the file. */,
CloseOptionsAsk = 'ask ' /* Ask the user whether or not to save the file. */
};

int main (int argc, const char * argv[]) {
if (argc < 3) {
printf("Incorrect arguments\n");
return 1;
}

@autoreleasepool {
NSString *processId = [NSString stringWithUTF8String:argv[1]];
NSString *windowId = [NSString stringWithUTF8String:argv[2]];

id window = getWindowOfProcess(processId, windowId);

@try {
[window closeSaving:CloseOptionsNo savingIn: nil];
return 0;
}
@catch (NSException *e) {

}

@try {
[window close];
return 0;
}
@catch (NSException *e) {

}
}

return 0;
}

6 changes: 2 additions & 4 deletions src/natives/find-window/mac/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
all: clean build
clean:
rm "${DEST}/find-window.scpt" || true
rm "${DEST}/find-window-cocoa" || true
rm "${DEST}/find-window" || true
build:
mkdir "${DEST}" || true
osacompile -o "${DEST}/find-window.scpt" find-window.applescript
clang -o "${DEST}/find-window-cocoa" -framework Cocoa find-window-cocoa.m
clang -o "${DEST}/find-window" -framework Cocoa -framework ScriptingBridge find-window.m
55 changes: 0 additions & 55 deletions src/natives/find-window/mac/find-window-cocoa.m

This file was deleted.

49 changes: 0 additions & 49 deletions src/natives/find-window/mac/find-window.applescript

This file was deleted.

Loading

0 comments on commit 8c43436

Please sign in to comment.