Repository navigation
Expand file tree
/
Copy pathautoScreenRecordJXA.applescript
More file actions
229 lines (211 loc) · 7.35 KB
/
Copy pathautoScreenRecordJXA.applescript
File metadata and controls
229 lines (211 loc) · 7.35 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/********************************************************
Summary:
Using the screen recording function of QuickTime Player on MacOS
JavaScript for Automation(JXA) to record the screen at a specified time
Verified Version:
macOS Ventura(version 13.6.1)
QuickTime Player(version 10.5)
Usage:
1.QuickTime Player settings
Select 'Record Selection' and set the area you want to record and
Set the area you want to record and the location where
you want to save the file (Options¨Destination).
2.Set the recording time
Enter the recording time in the dialog that appears after startup.
ex)3600(seconds) = 60(minutes) = 1(hour)
Recording time depends on the process and
may deviate by a few seconds to a few tens of seconds.
Set the recording time to a value larger than the desired recording time.
reference
1. JavaScript for Automation Release Notes
https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html#//apple_ref/doc/uid/TP40014508-CH111-SW1
2. Mac : JavaScript for Automation (JXA) Example Dictionary
http://www.openspc2.org/reibun/JXA/Yosemite/
3. JavaScript for Automation (JXA)
http://tonbi.jp/AppleScript/JavaScript/
4. JavaScript for Automation Cookbook
https://github.com/JXA-Cookbook/JXA-Cookbook/wiki/
********************************************************/
/********************************************************
Function Name:
ask_recording_time():
Function to display a dialog box to request the input of recording time and return the input value
Arguments:
None
Return value:
response.textReturned: recording time
Description:
Recorded time is processed only for numerical values.
********************************************************/
function ask_recording_time(){
var app = Application.currentApplication()
app.includeStandardAdditions = true
var str_check = true;
// Non-numeric repetition
while(str_check == true){
var response = app.displayDialog("Please enter the recording time(sec)", {
defaultAnswer: "",
withIcon: "note",
buttons: ["Cancel", "OK"],
defaultButton: "OK"
})
if(!(isNaN(response.textReturned))){
// If it's a number, it exits the loop
str_check = false;
}
if(response.buttonReturned == "Cancel"){
app.displayAlert('Pushed cancel button. Quit script.');
app.quit()
}
}
console.log('[ask_recording_time()]Recording time%isec', response.textReturned);
return response.textReturned
}
/********************************************************
Function Name:
quicktimeplayer_runnning_check():
Function to determine if QuickTime Player is running
Arguments:
None
Return value:
true:QuickTime Player is running
false:QuickTime Player is not running
Description:
Returns true if the QuickTime Player is runnning, false otherwise
********************************************************/
function quicktimeplayer_runnning_check(){
var qtRunningFlag = false;
var qt = Application('QuickTime Player')
if (qt.running()){
qtRunningFlag = true;
}
else{
qtRunningFlag = false;
}
return qtRunningFlag;
}
/********************************************************
Function Name:
quicktimeplayer_play_multiscreens()
Function to determine if QuickTime Player playback process has been executed
Arguments:
None
Return value:
true:QuickTime Player playback processing was executed
false: QuickTime Player playback processing could not be executed
Description:
Obtain the number of elements from the document class and
if the number of elements is not zero, execute the playback process.
********************************************************/
function quicktimeplayer_play_multiscreens(){
var qt = Application('QuickTime Player')
var qtActiveScreenFlag = false;
qt.activate();
var screen_num = qt.documents.length;
console.log('[quicktimeplayer_play_multiscreens()]Number of screens:%i',screen_num);
if (screen_num != 0){
qtActiveScreenFlag = true
// Enable loop
qt.document.looping = true;
qt.document.play();
}
return qtActiveScreenFlag;
}
/********************************************************
Function name:
screen_recording(input_time):
Screen recording processing function
Arguments
input_time: time to record
Return value:
None
Description:
A shortcut key input from System Events launches the screenshot toolbar and starts recording.
The elapsed time to be recorded is obtained from the data object calculated, and set.
After the recording time has elapsed,
recording is stopped by inputting a shortcut key in the same way as for the start of recording.
********************************************************/
function screen_recording(input_time){
var sys = Application('System Events');
// command+shift+5
sys.keyCode('23', { using: ['command down', 'shift down']});
delay(0.5);
// return
sys.keyCode('36');
console.log('[screen_recording()]Recording start')
const startTime = Date.now();
delay(input_time);
const elapsedTime = (Date.now() - startTime) / 1000;
// command+control+esc
sys.keyCode('53', { using: ['command down', 'control down']});
console.log('[screen_recording()]Recording end:Elapsed time%isec', elapsedTime);
return
}
/********************************************************
Function Name:
quit_quicktimeplayer():
Function to quit QuickTime Player
Arguments:
None
Return value:
None
Description:
Exit QuickTime Player with the quit command
********************************************************/
function quit_quicktimeplayer(){
var qt = Application('QuickTime Player')
console.log('[quit_quicktimeplayer()]QuickTime Player quit');
qt.quit()
return
}
/********************************************************
Function Name:
stop_googlechrome():
Function to stop Google Chrome processes
Arguments:
None
Return value:
None
Note:
Use kill -9 with caution, as it kills the process and may result in the loss of any work being performed.
When executing commands in the terminal, be careful not to accidentally kill other important processes.
Please work with extreme caution.
********************************************************/
function stop_googlechrome() {
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.doShellScript("kill -9 $(pgrep 'Google Chrome')");
}
/********************************************************
Function Name:
main():
main function that executes screen recording with QuickTime Player
Arguments:
None
Return value:
None
Description:
Allow screen recording process only while QuickTime Player is running
If there are screens that can be recorded, recording is performed for the specified time (start to stop).
If there are no screens available for recording or If there is no screen available for recording and
QuickTime Player is not running QuickTime Player is terminated and an error dialog is displayed.
********************************************************/
function main(){
var input_num = ask_recording_time()
var app = Application.currentApplication();
app.includeStandardAdditions = true;
if(input_num > 0){
console.log(input_num)
screen_recording(input_num);
quit_quicktimeplayer();
app.displayAlert('Screen recording is complete. Quit script.');
//stop_googlechrome()
console.log('[main()]Script end')
}
else{
app.displayAlert('Please enter a positive integer for the elapsed time.€n Quit script.');
}
return
}
//main function call
main()