-
Notifications
You must be signed in to change notification settings - Fork 8
/
cherrypick.js
executable file
·197 lines (145 loc) · 5.36 KB
/
cherrypick.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
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
#!/usr/bin/env osascript -l JavaScript
ObjC.import('stdlib')
var iTunesApp;
var iTunesAppWindow;
var appName;
/*
* OSA entry function ¯\_(ツ)_/¯
*
* @param {Object} args The CLI arguments
*/
function run(args) {
if (args.length < 1) {
printHelp();
} else {
openiTunesWithURL(fixUrl(args[0]));
getiTunesAppWindow();
hideiTunesAppWindow();
downloadApplication();
monitorDownload();
quit();
}
}
/*
* Prints nifty lil' help message
*/
var printHelp = function() {
console.log('Cherrypick - Painless .ipa download from iTunes');
console.log('\n\tUsage:');
console.log('\t$ cherrypick.js <App URL on iTunes>');
console.log('\t NOTE: If your url contains the ? character, make sure you enclose your URL in "".');
};
/*
* Convert url from https to itms
*
* @param {String} iTunesURL
*/
var fixUrl = function(url) {
return url.replace('https:', 'itms:');
};
/*
* Fires up iTunes application and directs it to open our app URL in App Store
*
* @param {String} iTunesURL The iTunes URL to open in iTunes. MUST start with "itms://"
*/
var openiTunesWithURL = function(iTunesURL) {
console.log("[+] Firing up iTunes with location: " + iTunesURL);
iTunesApp = Application('iTunes');
iTunesApp.activate();
iTunesApp.openLocation(iTunesURL);
};
/*
* Gets the iTunes application window through the `System Events` framework.
* We need this to mess with low level UI controls because most of them are not
* directly accessible from what Automation/JXA exposes for iTunes app.
*/
var getiTunesAppWindow = function() {
var system = Application('System Events');
iTunesAppWindow = system.processes.byName('iTunes').windows[0];
};
var hideiTunesAppWindow = function() {
iTunesApp = Application('iTunes');
iTunesApp.activate();
var se = Application('System Events')
se.keystroke('h', { using: 'command down' }) // Press Cmd-H
}
/*
* Juicy part. This method keeps trying to find the "Download" button on the app's
* page on iTunes. Every time it bumps the polling interval by one second cause we
* don't want to be very aggressive.
*/
var downloadApplication = function() {
var retries = 0;
var maxRetries = 8;
while(true) {
try {
// Get an array of all UI elements in iTunes window.
uiElems = Application("System Events").applicationProcesses['iTunes'].windows[0].entireContents()
// Find all buttons whose description contains 'Get'.
btns = uiElems.filter(function(element) {
try {
return element.role() == 'AXButton' && (element.description().match(/\bGet\b/) || element.description().match(/\bDownload\b/))
} catch (e) {}
})
// Split the description
let btnDesc = btns[0].description().split(",");
// Get the full app name, and remove the " " in front
var fullAppName = btnDesc[1].slice(1);
//var fullAppName = btnDesc[2].slice(1);
console.log('[+] Found application: "' + fullAppName +'". Downloading...');
// Get the app name (reduced for file monitoring) for later
//appName = fullAppName.split(" ")[0].slice(0, -1);
if (fullAppName.indexOf(":") > -1 ) {
appName = fullAppName.split(":")[0].trim();
} else {
appName = fullAppName.split(" ")[0].trim();
}
// Click on the 1st button found.
btns[0].click()
break;
} catch(e) {
/*
iTunes needs to load the App Store in its embeded littled browser thingie which might take a while.
Unfortunately, fucking JXA methods won't return a proper error but rather throw a runtime exception.
Essentially, we keep retrying by incrementing our delay until we reach the desireable state where the
"download" control can be paresed and the download can be initiated.
*/
var delayThreshold = (retries+1) * 2;
console.log('[!] State not ready yet, retrying in (' + delayThreshold +'s)...');
delay(delayThreshold);
if(++retries === maxRetries) {
console.log('[!] Cannot reach desirable state. Bailing out...');
$.exit(-1);
}
}
}
};
/*
* Steps into ObjC lalaland and checks whether our app was downloaded. The app
* download starts as a *.tmp file in "_/iTunes Media/Downloads". When the download
* is finished, it's copied over to "_/iTunes Media/Mobile Applications". We Poll
* that directory and when our file appears, we're done.
*
*/
var monitorDownload = function() {
var isFileNotFound = true;
var downloadsPath = '~/Music/iTunes/iTunes Media/Mobile Applications';
console.log('Waiting for ' + `${appName}` + ' to finish downloading...');
var listFiles = Application('System Events').folders.byName(downloadsPath).diskItems.name()
while(isFileNotFound) {
var updatedListFiles = Application('System Events').folders.byName(downloadsPath).diskItems.name()
if (updatedListFiles.length > listFiles.length) {
isFileNotFound = false;
console.log('\n\t ✔ Download ' + `${appName}` + ' complete. ');
}
// Poll every one second
delay(2);
}
};
/*
* Pretty self explanatory
*/
var quit = function() {
iTunesApp.quit();
$.exit(0);
};