forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiming-start-timer.js
executable file
·70 lines (60 loc) · 2.13 KB
/
timing-start-timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/osascript -l JavaScript
// Note: Timing.app is required with an Expert or Connect subscription
// Learn more and install: https://timingapp.com
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Start Timer
// @raycast.mode silent
// Optional parameters:
// @raycast.icon images/timing-logo.png
// @raycast.argument1 { "type": "text", "placeholder": "Project", "optional": true }
// @raycast.argument2 { "type": "text", "placeholder": "Title", "optional": true }
// @raycast.argument3 { "type": "text", "placeholder": "Duration", "optional": true }
// @raycast.packageName Timing
// Documentation:
// @raycast.description Start a timer
// @raycast.author Landen Danyluk
// @raycast.authorURL https://github.com/landendanyluk
function run(argv) {
const timing = Application('TimingHelper');
// throw if AppleScript support is not enabled
if (!timing.scriptingSupportAvailable()) {
console.log('Scripting support requires an Expert or Connect subscription');
throw new Error();
}
// throw if both project and title are empty
if (!(argv[0] || argv[1])) {
console.log("Please enter a project name or title");
throw new Error();
}
let project;
if (argv[0] !== "") {
// check if project exists
const projects = timing.projects.whose({ name: argv[0] });
if (!projects.length) {
console.log("Project does not exist");
throw new Error();
} else {
// if there's more than one project with the same name, choose the first one in the list
project = projects[0];
}
}
const withTitle = argv[1];
const forAbout = Number(argv[2]) * 60;
if (isNaN(forAbout)) {
console.log("Please enter a valid duration");
throw new Error();
}
// JXA will complain if a key is present but undefined, so we have to add them like this
const options = {};
if (project) {
options.project = project;
}
if (withTitle) {
options.withTitle = withTitle;
}
if (forAbout) {
options.forAbout = forAbout;
}
timing.startTimer(options);
}