-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathRe-import Lossless Tracks.applescript
More file actions
375 lines (300 loc) · 12.6 KB
/
Re-import Lossless Tracks.applescript
File metadata and controls
375 lines (300 loc) · 12.6 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
(*
"Re-import Lossless Tracks"
Dov Frankel, 2013
http://dovfrankel.com
v1.0 March 9, 2013 (iTunes 11.0.2, Toast 11.0.3)
Requires iTunes and Toast Titanium
The Upgrade Tracks script has the side effect of leaving a track's Sound Check level the same as the original tracks, which can sometimes be a problem when upgrading, leaving a song playing back too loudly (or softly) with Sound Check enabled.
There's a way around it (for lossless files), but it involves using Toast, and specifically non-scriptable features of Toast. Because this script involves UI scripting, it cannot be called from the iTunes Script menu.
This automates the process of taking a lossless m4a iTunes track, burning it to a virtual audio CD, and then using iTunes's ability to re-import a song from its original CD, adjusts the volume level. You're ending up with a different file, but since it started out lossless* the resulting re-imported file should have the same audio data.
*You can use this script with lossy files, but it is not recommended, and the script will warn you if any non-lossless files are selected
*)
-- Initialize variables shared between applications
set originalPlaylist to {}
set originalSelection to {}
set fileAliases to {}
set albumTitle to ""
set trackInfo to {}
set discImagePath to ""
property originalEncoder : ""
-- Make sure UI scripting is enabled
tell application "System Events"
if UI elements enabled is false then
display dialog "This script requires that you enable 'UI Scripting' support for AppleScript. You will be prompted to authorize this change by the system. If you do not wish to authorize this, click Cancel."
-- Automaticaly prompts the user
set UI elements enabled to true
end if
end tell
-- Pull out info from iTunes about selected tracks
tell application "iTunes"
-- Save the original playlist and selection
set originalPlaylist to view of front window
set originalSelection to selection
if selection is not {} then
copy selection as list to selectedTracks
set nonLosslessTracks to ""
set nextTrackNumber to 1
set trackCount to 0
repeat with theTrack in selectedTracks
tell application "iTunes"
set theTrack to first item of (tracks whose database ID = theTrack's database ID as integer)
-- Make sure the selection is complete, and sorted properly
if theTrack's track number ≠ nextTrackNumber then
display dialog "Please make sure the selection contains all of the album's tracks, sorted by track number. It looks like you skipped track " & nextTrackNumber & ".
Please correct the selection and try again." with icon 2 buttons {"OK"} default button 1
return
end if
set nextTrackNumber to nextTrackNumber + 1
-- Make a list of tracks whose files are not lossless
if (theTrack's kind as text) does not contain "lossless" then
set nonLosslessTracks to nonLosslessTracks & "
" & "\"" & theTrack's name & "\""
end if
-- Get the highest track count value from the selection
if theTrack's track count > trackCount then set trackCount to theTrack's track count
-- Fill two lists of track info to use later
copy theTrack's location to the end of fileAliases
copy {name:theTrack's name as text, albumName:theTrack's album as text} to the end of trackInfo
if albumTitle = "" then set albumTitle to theTrack's album as text
end tell
end repeat
-- Warn if any tracks are not lossless, give the opportunity to quit
if nonLosslessTracks ≠ "" then
display dialog "The following tracks are not lossless. Running this script may degrade their quality. Do you wish to continue?
" & nonLosslessTracks buttons {"Yes", "No"} default button 2 with icon 2
if the button returned of the result is "No" then return
end if
-- If track count is specified, check that the number of tracks selected matches
set highTrackNumber to nextTrackNumber - 1
if trackCount > 0 and highTrackNumber ≠ trackCount then
display dialog "This album looks like it should have " & trackCount & " tracks, but the highest selected track is number " & highTrackNumber & ". Do you wish to continue?" buttons {"Yes", "No"} default button 2 with icon 2
if the button returned of the result is "No" then return
end if
else -- Selection is empty
display dialog "There are no tracks selected. Please select the tracks you wish to re-import." buttons {"Cancel"} default button 1 with icon 0
return "No tracks selected"
end if
end tell
-- Create a file path for the disc image's location
tell application "Finder"
set discImagePath to POSIX path of ((path to downloads folder as text) & albumTitle & ".Sd2f")
end tell
-- Check if the file already exists
set outputFileReference to null
set discImageFileExists to true
try
set outputFileReference to POSIX file (discImagePath as text)
tell application "Finder" to set discImageFileExists to exists outputFileReference
on error
set discImageFileExists to false
end try
-- Burn and mount the virtual disc, only if it doesn't already exist
if not discImageFileExists then
-- Create a new audio disc with the selected tracks' files
tell application "Toast Titanium"
activate
set theDisc to make new Audio disc with properties {name:albumTitle}
add to theDisc items fileAliases
end tell
-- Save the audio disc to a disc image using GUI scripting
tell application "Toast Titanium" to activate
tell application "System Events"
tell process "Toast Titanium"
click menu item "Save As Disc Image…" of menu "File" of menu bar 1
set windowTitle to "Save Disc Image as:"
repeat until exists window windowTitle
delay 0.5
end repeat
tell window windowTitle
keystroke "g" using {command down, shift down}
repeat until exists sheet 1
delay 0.5
end repeat
tell sheet 1
-- Save clipboard
set oldClipboard to the clipboard
-- Copy/paste the path, instead of using 'keystroke' on the string, so it handles international characters correctly
set the clipboard to discImagePath
keystroke "a" using {command down}
keystroke "v" using {command down}
click button "Go"
-- Restore the clipboard
set the clipboard to oldClipboard
end tell
click button "Save"
end tell
repeat until exists UI element 21 of UI element 1 of UI element 2 of UI element 1 of UI element 7 of window "Toast 11 Titanium"
delay 0.5
end repeat
end tell
end tell
-- Mount the disc image
set outputFileReference to POSIX file (discImagePath as text)
tell application "Toast Titanium" to mount image outputFileReference
-- If multiple CDDB results are returned in iTunes, dismiss the dialog
delay 5
tell application "iTunes" to activate
repeat 10 times
tell application "System Events"
tell process "iTunes"
if exists window "CD Lookup Results" then
click button "OK" of window "CD Lookup Results"
exit repeat
end if
end tell
end tell
delay 0.5
end repeat
end if -- If CD image file already exists
-- Import the tracks from the disc image
tell application "iTunes"
set cdSource to null
repeat while cdSource is null
try
set cdSource to first source whose kind is audio CD
end try
end repeat
set discPlaylist to audio CD playlist 1 of cdSource
set trackCount to count discPlaylist's tracks
-- Make sure each tracks' info is correct. Specifically, artist, album, and track title need to match for each track number
repeat with i from 1 to trackCount
set discTrack to track i of discPlaylist
set originalTrack to item i of originalSelection
set tryCount to 0
set tryFailed to true
repeat until tryFailed = false
try
set album of discTrack to (album of originalTrack as text)
set artist of discTrack to (artist of originalTrack as text)
set name of discTrack to (name of originalTrack as text)
set disc number of discTrack to (disc number of originalTrack as text)
set disc count of discTrack to (disc count of originalTrack as text)
set tryFailed to false
on error message
set tryCount to tryCount + 1
if tryCount = 10 then
display dialog "Failure to copy track info: " & message
my cleanup()
return
end if
set cdSource to (first source whose kind is audio CD)
set discPlaylist to audio CD playlist 1 of cdSource
set discTrack to track i of discPlaylist
set originalTrack to item i of originalSelection
delay 0.5
end try
end repeat
end repeat
-- Bring the CD "playlist" to front, in case it wasn't already
activate
set tryCount to 0
set tryFailed to true
repeat until tryFailed = false
try
set view of front browser window to discPlaylist
set tryFailed to false
on error message
set tryCount to tryCount + 1
if tryCount = 5 then
display dialog "Failure to get to the virtual CD's playlist: " & message
my cleanup()
return
end if
delay 0.5
set cdSource to first source whose kind is audio CD
set discPlaylist to audio CD playlist 1 of cdSource
end try
end repeat
-- Save encoder for putting it back later
set originalEncoder to current encoder
(* I'd loooove to allow the script to select only certain songs for importing, and have a lot of that code written below, but it doesn't look like (a) "convert" allows you to tell it to replace an existing file, and (b) "add" allows you to add a track from a CD to the library
-- Have the user select which tracks to re-import
set trackNamesToImport to (choose from list trackNameList with prompt "Select the track tags you wish to re-import (unselected tracks will be ignored):" default items trackNameList with multiple selections allowed without empty selection allowed)
if trackNamesToImport is false then
return
end if
try
set newEncoder to some encoder whose name is "Lossless Encoder"
set current encoder to newEncoder
set tracksToImport to {}
-- Make a list of the selected tracks
repeat with i from 1 to trackCount
set discTrack to track i of discPlaylist
if trackNamesToImport contains discTrack's name then copy discTrack to the end of tracksToImport
end repeat
-- Import the tracks to a new playlist
set importsPlaylist to (make new playlist with properties {name:albumTitle})
repeat with trackToImport in tracksToImport
log trackToImport's name as text
log (location of trackToImport) as text
add {location of trackToImport as alias} to importsPlaylist
end repeat
my cleanup()
on error errStr number errorNumber
log "Error!! : " & errStr
my cleanup()
end try
*)
end tell
try
-- Use iTunes GUI scripting to import the CD
tell application "iTunes" to activate
tell application "System Events"
tell process "iTunes"
click button "Import CD" of splitter group 1 of window "iTunes"
set attemptCount to 15
repeat until exists window "Import Settings"
delay 0.5
-- Don't try more than 15 times
set attemptCount to attemptCount - 1
if attemptCount = 0 then error "Unable to click 'Import CD' button"
end repeat
set importSettingsWindow to window "Import Settings"
click pop up button 1 of importSettingsWindow
set popupMenu to menu 1 of pop up button 1 of importSettingsWindow
repeat with menuItem in menu items of popupMenu
if (name of menuItem as text) = "Apple Lossless Encoder" then
click menuItem
exit repeat
end if
end repeat
set errorCorrectionCheckbox to checkbox "Use error correction when reading Audio CDs" of importSettingsWindow
if (get value of errorCorrectionCheckbox as boolean) then click errorCorrectionCheckbox
click button "OK" of importSettingsWindow
repeat until exists button "Replace Existing" of window 1
delay 0.5
end repeat
click button "Replace Existing" of window 1
delay 5
repeat until (exists button "Import CD" of splitter group 1 of window "iTunes")
delay 0.5
end repeat
end tell
end tell
my cleanup()
on error errStr number errorNumber
log "Error!! : " & errStr
my cleanup()
end try
-- Eject the disc
tell application "System Events"
tell application "iTunes" to activate
keystroke "e" using {command down}
end tell
-- Trash the CD image
tell application "Finder" to move outputFileReference to trash
-- Quit Toast
quit application "Toast Titanium" without saving
-- Finish up
tell application "iTunes"
activate
-- Go back to the original playlist with the same selection, or at least the first track
set view of front window to originalPlaylist
reveal first item of originalSelection
display dialog "All done! Your tracks have been re-imported." with icon 1 buttons {"OK"} default button 1
end tell
on cleanup()
if originalEncoder is not null then
tell application "iTunes" to set current encoder to originalEncoder
end if
end cleanup