mac-helper 0.2.0 → 0.3.1

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.
package/lib/dialog.d.ts CHANGED
@@ -1,12 +1,14 @@
1
- import { StandardAdditions } from '@jxa/types';
1
+ import type { StandardAdditions } from '@jxa/types';
2
+ export declare const dialog: {
3
+ prompt: typeof prompt;
4
+ chooseFromList: typeof chooseFromList;
5
+ chooseMultipleFromList: typeof chooseMultipleFromList;
6
+ };
7
+ declare function prompt(label: string, defaultValue?: string): Promise<string>;
2
8
  type ChooseOption = {
3
9
  label: string;
4
10
  value: string;
5
11
  };
6
12
  declare function chooseFromList(list: string[] | ChooseOption[], options?: StandardAdditions.StandardAdditions.ChooseFromListOptionalParameter): Promise<string | undefined>;
7
13
  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
14
  export {};
package/lib/dialog.js CHANGED
@@ -2,6 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dialog = void 0;
4
4
  const run_1 = require("@jxa/run");
5
+ exports.dialog = {
6
+ prompt,
7
+ chooseFromList,
8
+ chooseMultipleFromList,
9
+ };
10
+ async function prompt(label, defaultValue = '') {
11
+ // if cancel dialog, will throw error
12
+ try {
13
+ return ((await (0, run_1.run)((label) => {
14
+ const app = Application.currentApplication();
15
+ app.includeStandardAdditions = true;
16
+ const response = app.displayDialog(label, { defaultAnswer: '' });
17
+ return response.textReturned;
18
+ }, label)) || defaultValue);
19
+ }
20
+ catch {
21
+ return defaultValue;
22
+ }
23
+ }
5
24
  async function runOnCurrentApp(method, ...args) {
6
25
  return await (0, run_1.run)((method, ...args) => {
7
26
  const app = Application.currentApplication();
@@ -59,10 +78,6 @@ async function chooseMultipleFromList(list, options = {}) {
59
78
  return valArr;
60
79
  }
61
80
  }
62
- exports.dialog = {
63
- chooseFromList,
64
- chooseMultipleFromList,
65
- };
66
81
  // dialog.chooseFromList(['hello', 'world']).then((selected) => {
67
82
  // console.log(selected)
68
83
  // })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mac-helper",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "useful helper for macOS",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -14,6 +14,7 @@
14
14
  "test:dev": "vitest --globals --testTimeout=30000",
15
15
  "test-cover": "vitest run --globals --coverage --testTimeout=30000",
16
16
  "dev": "tsc -w --incremental",
17
+ "typecheck": "tsc --noEmit",
17
18
  "build": "rm -rf lib; rm tsconfig.tsbuildinfo; tsc",
18
19
  "prepublishOnly": "pnpm test && pnpm build"
19
20
  },
package/src/dialog.ts CHANGED
@@ -1,5 +1,27 @@
1
1
  import { run } from '@jxa/run'
2
- import { StandardAdditions } from '@jxa/types'
2
+ import type { StandardAdditions } from '@jxa/types'
3
+
4
+ export const dialog = {
5
+ prompt,
6
+ chooseFromList,
7
+ chooseMultipleFromList,
8
+ }
9
+
10
+ async function prompt(label: string, defaultValue = ''): Promise<string> {
11
+ // if cancel dialog, will throw error
12
+ try {
13
+ return (
14
+ (await run((label) => {
15
+ const app = Application.currentApplication()
16
+ app.includeStandardAdditions = true
17
+ const response = app.displayDialog(label, { defaultAnswer: '' })
18
+ return response.textReturned
19
+ }, label)) || defaultValue
20
+ )
21
+ } catch {
22
+ return defaultValue
23
+ }
24
+ }
3
25
 
4
26
  type AppMethod = 'chooseFromList'
5
27
 
@@ -12,7 +34,7 @@ async function runOnCurrentApp(method: AppMethod, ...args: any[]) {
12
34
  return app[method]?.(...args)
13
35
  },
14
36
  method,
15
- ...args
37
+ ...args,
16
38
  )
17
39
  }
18
40
 
@@ -20,7 +42,7 @@ type ChooseOption = { label: string; value: string }
20
42
 
21
43
  async function chooseFromList(
22
44
  list: string[] | ChooseOption[],
23
- options: StandardAdditions.StandardAdditions.ChooseFromListOptionalParameter = {}
45
+ options: StandardAdditions.StandardAdditions.ChooseFromListOptionalParameter = {},
24
46
  ): Promise<string | undefined> {
25
47
  let usingOption = false
26
48
  let showList: string[] = []
@@ -49,7 +71,7 @@ async function chooseFromList(
49
71
 
50
72
  async function chooseMultipleFromList(
51
73
  list: string[] | ChooseOption[],
52
- options: StandardAdditions.StandardAdditions.ChooseFromListOptionalParameter = {}
74
+ options: StandardAdditions.StandardAdditions.ChooseFromListOptionalParameter = {},
53
75
  ): Promise<string[]> {
54
76
  let usingOption = false
55
77
  let showList: string[] = []
@@ -71,17 +93,12 @@ async function chooseMultipleFromList(
71
93
  return selected as string[]
72
94
  } else {
73
95
  const valArr = (selected as string[]).map(
74
- (label) => (list as ChooseOption[]).find((item) => item.label === label)!.value
96
+ (label) => (list as ChooseOption[]).find((item) => item.label === label)!.value,
75
97
  )
76
98
  return valArr
77
99
  }
78
100
  }
79
101
 
80
- export const dialog = {
81
- chooseFromList,
82
- chooseMultipleFromList,
83
- }
84
-
85
102
  // dialog.chooseFromList(['hello', 'world']).then((selected) => {
86
103
  // console.log(selected)
87
104
  // })