mac-helper 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ import { StandardAdditions } from '@jxa/types';
2
+ type ChooseOption = {
3
+ label: string;
4
+ value: string;
5
+ };
6
+ declare function chooseFromList(list: string[] | ChooseOption[], options?: StandardAdditions.StandardAdditions.ChooseFromListOptionalParameter): Promise<string | undefined>;
7
+ declare function chooseMultipleFromList(list: string[] | ChooseOption[], options?: StandardAdditions.StandardAdditions.ChooseFromListOptionalParameter): Promise<string[]>;
8
+ export declare const dialog: {
9
+ chooseFromList: typeof chooseFromList;
10
+ chooseMultipleFromList: typeof chooseMultipleFromList;
11
+ };
12
+ export {};
package/lib/dialog.js ADDED
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dialog = void 0;
4
+ const run_1 = require("@jxa/run");
5
+ async function runOnCurrentApp(method, ...args) {
6
+ return await (0, run_1.run)((method, ...args) => {
7
+ const app = Application.currentApplication();
8
+ app.includeStandardAdditions = true;
9
+ // @ts-ignore
10
+ return app[method]?.(...args);
11
+ }, method, ...args);
12
+ }
13
+ async function chooseFromList(list, options = {}) {
14
+ let usingOption = false;
15
+ let showList = [];
16
+ if (typeof list[0] === 'object') {
17
+ usingOption = true;
18
+ showList = list.map((l) => l.label);
19
+ }
20
+ else {
21
+ showList = list;
22
+ }
23
+ const selected = await runOnCurrentApp('chooseFromList', showList, {
24
+ multipleSelectionsAllowed: false,
25
+ ...options,
26
+ });
27
+ if (!selected)
28
+ return;
29
+ const label = selected[0];
30
+ if (!usingOption) {
31
+ return label;
32
+ }
33
+ else {
34
+ const val = list.find((item) => item.label === label)?.value;
35
+ return val;
36
+ }
37
+ }
38
+ async function chooseMultipleFromList(list, options = {}) {
39
+ let usingOption = false;
40
+ let showList = [];
41
+ if (typeof list[0] === 'object') {
42
+ usingOption = true;
43
+ showList = list.map((l) => l.label);
44
+ }
45
+ else {
46
+ showList = list;
47
+ }
48
+ const selected = await runOnCurrentApp('chooseFromList', showList, {
49
+ multipleSelectionsAllowed: true,
50
+ ...options,
51
+ });
52
+ if (!selected)
53
+ return [];
54
+ if (!usingOption) {
55
+ return selected;
56
+ }
57
+ else {
58
+ const valArr = selected.map((label) => list.find((item) => item.label === label).value);
59
+ return valArr;
60
+ }
61
+ }
62
+ exports.dialog = {
63
+ chooseFromList,
64
+ chooseMultipleFromList,
65
+ };
66
+ // dialog.chooseFromList(['hello', 'world']).then((selected) => {
67
+ // console.log(selected)
68
+ // })
69
+ // dialog
70
+ // .chooseFromList([
71
+ // { value: 'yes', label: '是吧~~~' },
72
+ // { value: 'halo', label: 'Halo 啊洒洒' },
73
+ // ])
74
+ // .then((selected) => {
75
+ // console.log(selected)
76
+ // })
77
+ // dialog
78
+ // .chooseMultipleFromList(
79
+ // [
80
+ // { value: 'yes', label: '是吧~~~' },
81
+ // { value: 'halo', label: 'Halo 啊洒洒' },
82
+ // ],
83
+ // {
84
+ // withTitle: 'title',
85
+ // withPrompt: 'prompt',
86
+ // cancelButtonName: '取消!',
87
+ // // @ts-ignore
88
+ // okButtonName: '酱紫吧~',
89
+ // }
90
+ // )
91
+ // .then((selected) => {
92
+ // console.log(selected)
93
+ // })
@@ -0,0 +1,10 @@
1
+ export declare function getPathFinderSelected(): Promise<string[]>;
2
+ export declare function getFinderSelected(): Promise<string[]>;
3
+ export declare const PathFinder: {
4
+ allSelected(): Promise<string[]>;
5
+ singleSelected(): Promise<string | undefined>;
6
+ };
7
+ export declare const Finder: {
8
+ allSelected(): Promise<string[]>;
9
+ singleSelected(): Promise<string | undefined>;
10
+ };
package/lib/finder.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Finder = exports.PathFinder = exports.getFinderSelected = exports.getPathFinderSelected = void 0;
4
+ const run_1 = require("@jxa/run");
5
+ const url_1 = require("url");
6
+ async function getPathFinderSelected() {
7
+ const filePaths = await (0, run_1.run)(() => {
8
+ const app = Application('Path Finder');
9
+ return (app.selection() || []).map((x) => x.posixPath());
10
+ });
11
+ return filePaths;
12
+ }
13
+ exports.getPathFinderSelected = getPathFinderSelected;
14
+ async function getFinderSelected() {
15
+ const urls = await (0, run_1.run)(() => {
16
+ const app = Application('Finder');
17
+ const selection = app.selection();
18
+ return (selection || []).map((x) => x.url());
19
+ });
20
+ return urls.map((u) => (0, url_1.fileURLToPath)(u));
21
+ }
22
+ exports.getFinderSelected = getFinderSelected;
23
+ exports.PathFinder = {
24
+ async allSelected() {
25
+ return getPathFinderSelected();
26
+ },
27
+ async singleSelected() {
28
+ return (await getPathFinderSelected())[0];
29
+ },
30
+ };
31
+ exports.Finder = {
32
+ async allSelected() {
33
+ return getFinderSelected();
34
+ },
35
+ async singleSelected() {
36
+ return (await getFinderSelected())[0];
37
+ },
38
+ };
39
+ // ;(async () => {
40
+ // console.log(await getPathFinderSelect())
41
+ // console.log(await getPathFinderSelects())
42
+ // })()
package/lib/index.d.ts CHANGED
@@ -1,6 +1,3 @@
1
- export declare function getPathFinderSelect(): Promise<string | null>;
2
- export declare function getPathFinderSelects(): Promise<string[]>;
3
- export declare const PathFinder: {
4
- singleSelected(): Promise<string | null>;
5
- allSelected(): Promise<string[]>;
6
- };
1
+ export * from './finder';
2
+ export * from './iterm';
3
+ export * from './dialog';
package/lib/index.js CHANGED
@@ -1,32 +1,21 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PathFinder = exports.getPathFinderSelects = exports.getPathFinderSelect = void 0;
4
- const run_1 = require("@jxa/run");
5
- async function getPathFinderSelect() {
6
- const filePath = await (0, run_1.run)(() => {
7
- const app = Application('Path Finder');
8
- return app.selection()?.[0]?.posixPath() ?? null;
9
- });
10
- return filePath;
11
- }
12
- exports.getPathFinderSelect = getPathFinderSelect;
13
- async function getPathFinderSelects() {
14
- const filePaths = await (0, run_1.run)(() => {
15
- const app = Application('Path Finder');
16
- return (app.selection() || []).map((x) => x.posixPath());
17
- });
18
- return filePaths;
19
- }
20
- exports.getPathFinderSelects = getPathFinderSelects;
21
- exports.PathFinder = {
22
- singleSelected() {
23
- return getPathFinderSelect();
24
- },
25
- allSelected() {
26
- return getPathFinderSelects();
27
- },
2
+ /// <reference types='@jxa/global-type' />
3
+ /// <reference types='@jxa/types' />
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
28
17
  };
29
- // ;(async () => {
30
- // console.log(await getPathFinderSelect())
31
- // console.log(await getPathFinderSelects())
32
- // })()
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ __exportStar(require("./finder"), exports);
20
+ __exportStar(require("./iterm"), exports);
21
+ __exportStar(require("./dialog"), exports);
package/lib/iterm.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export declare function runInIterm(command: string, reuse?: boolean): Promise<void>;
2
+ export declare const iTerm: {
3
+ run: typeof runInIterm;
4
+ runInIterm: typeof runInIterm;
5
+ };
package/lib/iterm.js ADDED
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.iTerm = exports.runInIterm = void 0;
4
+ const run_1 = require("@jxa/run");
5
+ async function runInIterm(command, reuse = true) {
6
+ await (0, run_1.run)((command, reuse) => {
7
+ const app = Application('iTerm');
8
+ app.includeStandardAdditions = true;
9
+ app.activate();
10
+ let usingSession;
11
+ // reuse when possible
12
+ if (reuse) {
13
+ const session = app.currentWindow().currentSession();
14
+ const isIdle = session.isAtShellPrompt();
15
+ if (isIdle) {
16
+ usingSession = session;
17
+ }
18
+ }
19
+ // create new tab when necessary
20
+ if (!usingSession) {
21
+ const tab = app.createTabWithDefaultProfile(app.currentWindow);
22
+ usingSession = tab.currentSession();
23
+ }
24
+ if (!usingSession) {
25
+ return;
26
+ }
27
+ app.write(usingSession, { text: command });
28
+ }, command, reuse);
29
+ }
30
+ exports.runInIterm = runInIterm;
31
+ exports.iTerm = {
32
+ run: runInIterm,
33
+ runInIterm,
34
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mac-helper",
3
- "version": "0.0.3",
3
+ "version": "0.1.0",
4
4
  "description": "useful helper for macOS",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -14,7 +14,7 @@
14
14
  "test-cover": "nyc --reporter=lcov --reporter=text mocha",
15
15
  "dev": "tsc -w --incremental",
16
16
  "build": "rm -rf lib; rm tsconfig.tsbuildinfo; tsc",
17
- "prepublishOnly": "pnpm build"
17
+ "prepublishOnly": "pnpm test && pnpm build"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
@@ -39,20 +39,22 @@
39
39
  "@jxa/sdef-to-dts": "^1.3.6",
40
40
  "@jxa/types": "^1.3.6",
41
41
  "@magicdawn/prettier-config": "^0.0.2",
42
- "@swc/core": "^1.3.19",
42
+ "@swc/core": "^1.3.40",
43
43
  "@types/fs-extra": "^11.0.1",
44
- "@types/node": "^18.11.9",
45
- "@typescript-eslint/eslint-plugin": "^5.44.0",
46
- "@typescript-eslint/parser": "^5.44.0",
47
- "eslint": "^8.28.0",
48
- "eslint-config-prettier": "^8.5.0",
44
+ "@types/mocha": "^10.0.1",
45
+ "@types/node": "^18.15.3",
46
+ "@typescript-eslint/eslint-plugin": "^5.55.0",
47
+ "@typescript-eslint/parser": "^5.55.0",
48
+ "delay": "^5.0.0",
49
+ "eslint": "^8.36.0",
50
+ "eslint-config-prettier": "^8.7.0",
49
51
  "fs-extra": "^11.1.0",
50
- "husky": "^8.0.2",
52
+ "husky": "^8.0.3",
51
53
  "jxa-common-used": "^0.0.2",
52
- "lint-staged": "^13.0.3",
53
- "mocha": "^10.1.0",
54
+ "lint-staged": "^13.2.0",
55
+ "mocha": "^10.2.0",
54
56
  "nyc": "^15.1.0",
55
- "prettier": "^2.7.1",
57
+ "prettier": "^2.8.4",
56
58
  "should": "^13.2.3",
57
59
  "typescript": "^4.9.5"
58
60
  },
package/src/dialog.ts ADDED
@@ -0,0 +1,114 @@
1
+ import { run } from '@jxa/run'
2
+ import { StandardAdditions } from '@jxa/types'
3
+
4
+ type AppMethod = 'chooseFromList'
5
+
6
+ async function runOnCurrentApp(method: AppMethod, ...args: any[]) {
7
+ return await run(
8
+ (method: AppMethod, ...args: any[]) => {
9
+ const app = Application.currentApplication()
10
+ app.includeStandardAdditions = true
11
+ // @ts-ignore
12
+ return app[method]?.(...args)
13
+ },
14
+ method,
15
+ ...args
16
+ )
17
+ }
18
+
19
+ type ChooseOption = { label: string; value: string }
20
+
21
+ async function chooseFromList(
22
+ list: string[] | ChooseOption[],
23
+ options: StandardAdditions.StandardAdditions.ChooseFromListOptionalParameter = {}
24
+ ): Promise<string | undefined> {
25
+ let usingOption = false
26
+ let showList: string[] = []
27
+ if (typeof list[0] === 'object') {
28
+ usingOption = true
29
+ showList = (list as ChooseOption[]).map((l) => l.label)
30
+ } else {
31
+ showList = list as string[]
32
+ }
33
+
34
+ const selected = await runOnCurrentApp('chooseFromList', showList, {
35
+ multipleSelectionsAllowed: false,
36
+ ...options,
37
+ })
38
+
39
+ if (!selected) return
40
+ const label = selected[0]
41
+
42
+ if (!usingOption) {
43
+ return label
44
+ } else {
45
+ const val = (list as ChooseOption[]).find((item) => item.label === label)?.value
46
+ return val
47
+ }
48
+ }
49
+
50
+ async function chooseMultipleFromList(
51
+ list: string[] | ChooseOption[],
52
+ options: StandardAdditions.StandardAdditions.ChooseFromListOptionalParameter = {}
53
+ ): Promise<string[]> {
54
+ let usingOption = false
55
+ let showList: string[] = []
56
+ if (typeof list[0] === 'object') {
57
+ usingOption = true
58
+ showList = (list as ChooseOption[]).map((l) => l.label)
59
+ } else {
60
+ showList = list as string[]
61
+ }
62
+
63
+ const selected = await runOnCurrentApp('chooseFromList', showList, {
64
+ multipleSelectionsAllowed: true,
65
+ ...options,
66
+ })
67
+
68
+ if (!selected) return []
69
+
70
+ if (!usingOption) {
71
+ return selected as string[]
72
+ } else {
73
+ const valArr = (selected as string[]).map(
74
+ (label) => (list as ChooseOption[]).find((item) => item.label === label)!.value
75
+ )
76
+ return valArr
77
+ }
78
+ }
79
+
80
+ export const dialog = {
81
+ chooseFromList,
82
+ chooseMultipleFromList,
83
+ }
84
+
85
+ // dialog.chooseFromList(['hello', 'world']).then((selected) => {
86
+ // console.log(selected)
87
+ // })
88
+
89
+ // dialog
90
+ // .chooseFromList([
91
+ // { value: 'yes', label: '是吧~~~' },
92
+ // { value: 'halo', label: 'Halo 啊洒洒' },
93
+ // ])
94
+ // .then((selected) => {
95
+ // console.log(selected)
96
+ // })
97
+
98
+ // dialog
99
+ // .chooseMultipleFromList(
100
+ // [
101
+ // { value: 'yes', label: '是吧~~~' },
102
+ // { value: 'halo', label: 'Halo 啊洒洒' },
103
+ // ],
104
+ // {
105
+ // withTitle: 'title',
106
+ // withPrompt: 'prompt',
107
+ // cancelButtonName: '取消!',
108
+ // // @ts-ignore
109
+ // okButtonName: '酱紫吧~',
110
+ // }
111
+ // )
112
+ // .then((selected) => {
113
+ // console.log(selected)
114
+ // })
package/src/finder.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { run as jxaRun } from '@jxa/run'
2
+ import { type PathFinder as PathFinderType } from 'jxa-common-used'
3
+ import { fileURLToPath } from 'url'
4
+
5
+ export async function getPathFinderSelected() {
6
+ const filePaths: string[] = await jxaRun(() => {
7
+ const app = Application<PathFinderType>('Path Finder')
8
+ return (app.selection() || []).map((x) => x.posixPath())
9
+ })
10
+ return filePaths
11
+ }
12
+
13
+ export async function getFinderSelected() {
14
+ const urls: string[] = await jxaRun(() => {
15
+ const app = Application('Finder')
16
+ const selection = app.selection()
17
+ return (selection || []).map((x) => x.url())
18
+ })
19
+ return urls.map((u) => fileURLToPath(u))
20
+ }
21
+
22
+ export const PathFinder = {
23
+ async allSelected() {
24
+ return getPathFinderSelected()
25
+ },
26
+ async singleSelected(): Promise<string | undefined> {
27
+ return (await getPathFinderSelected())[0]
28
+ },
29
+ }
30
+
31
+ export const Finder = {
32
+ async allSelected() {
33
+ return getFinderSelected()
34
+ },
35
+ async singleSelected(): Promise<string | undefined> {
36
+ return (await getFinderSelected())[0]
37
+ },
38
+ }
39
+
40
+ // ;(async () => {
41
+ // console.log(await getPathFinderSelect())
42
+ // console.log(await getPathFinderSelects())
43
+ // })()
package/src/index.ts CHANGED
@@ -1,31 +1,6 @@
1
- import { run as jxaRun } from '@jxa/run'
2
- import { type PathFinder as PathFinderType } from 'jxa-common-used'
1
+ /// <reference types='@jxa/global-type' />
2
+ /// <reference types='@jxa/types' />
3
3
 
4
- export async function getPathFinderSelect() {
5
- const filePath: string | null = await jxaRun(() => {
6
- const app = Application<PathFinderType>('Path Finder')
7
- return app.selection()?.[0]?.posixPath() ?? null
8
- })
9
- return filePath
10
- }
11
-
12
- export async function getPathFinderSelects() {
13
- const filePaths: string[] = await jxaRun(() => {
14
- const app = Application<PathFinderType>('Path Finder')
15
- return (app.selection() || []).map((x) => x.posixPath())
16
- })
17
- return filePaths
18
- }
19
-
20
- export const PathFinder = {
21
- singleSelected() {
22
- return getPathFinderSelect()
23
- },
24
- allSelected() {
25
- return getPathFinderSelects()
26
- },
27
- }
28
- // ;(async () => {
29
- // console.log(await getPathFinderSelect())
30
- // console.log(await getPathFinderSelects())
31
- // })()
4
+ export * from './finder'
5
+ export * from './iterm'
6
+ export * from './dialog'
package/src/iterm.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { run } from '@jxa/run'
2
+ import { iTerm2 } from 'jxa-common-used'
3
+
4
+ export async function runInIterm(command: string, reuse = true) {
5
+ await run(
6
+ (command, reuse) => {
7
+ const app = Application<iTerm2>('iTerm')
8
+ app.includeStandardAdditions = true
9
+ app.activate()
10
+
11
+ let usingSession: iTerm2.Session | undefined
12
+
13
+ // reuse when possible
14
+ if (reuse) {
15
+ const session: iTerm2.Session = app.currentWindow().currentSession()
16
+ const isIdle = session.isAtShellPrompt()
17
+ if (isIdle) {
18
+ usingSession = session
19
+ }
20
+ }
21
+
22
+ // create new tab when necessary
23
+ if (!usingSession) {
24
+ const tab = app.createTabWithDefaultProfile(app.currentWindow)
25
+ usingSession = tab.currentSession()
26
+ }
27
+
28
+ if (!usingSession) {
29
+ return
30
+ }
31
+
32
+ app.write(usingSession, { text: command })
33
+ },
34
+ command,
35
+ reuse
36
+ )
37
+ }
38
+
39
+ export const iTerm = {
40
+ run: runInIterm,
41
+ runInIterm,
42
+ }