-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMerge.applescript
More file actions
137 lines (117 loc) · 3.35 KB
/
Merge.applescript
File metadata and controls
137 lines (117 loc) · 3.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
property my_title : "Merge"
to split(s, d)
set o to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set l to s's every text item
set AppleScript's text item delimiters to o
return l
end
to sort(l)
repeat with i from 1 to (count of l) - 1
repeat with j from i + 1 to count of l
set i_track to (item i of l)'s track number
set j_track to (item j of l)'s track number
if i_track > j_track then
set temp to item i of l
set item i of l to item j of l
set item j of l to temp
end
end
end
end
to dupes(l)
repeat with i from 1 to (count of l) - 1
repeat with j from i + 1 to count of l
set i_track to (item i of l)'s track number
set j_track to (item j of l)'s track number
if i_track = j_track
my cancel("Duplicate track number: " & i_track)
end
end
end
end
to trash_file(l)
do shell script "mv " & quoted form of POSIX path of (l as string) & " " & quoted form of POSIX path of (path to trash as string)
end
to cancel(m)
display dialog m
error number -128
end
tell application "Music"
set n to {}
set o to {}
repeat with t in selection
set p to t's location as string
set f to my split(p, ":")'s last item
set i to my split(f, ".")'s first item as number
if i ≥ 9172 and i ≤ 19961
copy t to o's end
else
copy t to n's end
end
end
if n's length ≠ o's length
my cancel("Item count mismatch: " & n's length & " ≠ " & o's length)
end
if n's length = 0
my cancel("Selection is empty.")
end
my sort(n)
my sort(o)
my dupes(n)
my dupes(o)
set message to ""
repeat with i from 1 to count of n
set old to item i of o
set new to item i of n
set old_track to old's track number
set new_track to new's track number
if old_track ≠ new_track
my cancel("Mismatched track numbers: " & old_track & " ≠ " & new_track)
end
set old_name to old's name
set new_name to new's name
set message to message & old_track & ". " & old_name & "\n"
if new_name ≠ old_name
set message to message & " " & new_name & "\n"
end
end
display dialog message
set normal to {}
repeat with p in playlists
if p's visible and p's special kind = none and not p's genius and not p's smart and index of last track of p < 1500
set normal to normal & {p}
end
end
repeat with i from 1 to count of n
set old to item i of o
set new to item i of n
set oldid to old's persistent ID
repeat with j from 1 to count of normal
set p to item j of normal
set ts to tracks of p
repeat with k from 1 to count of ts
set t to item k of ts
if t's persistent ID = oldid
delete t
duplicate new to p
end
end
end
end
set old_database_ids to {}
repeat with i from 1 to count of n
set old to item i of o
set new to item i of n
set old_plays to old's «class pPlC»
set new_plays to new's «class pPlC»
set old_dbid to old's database ID
set new's «class pPlC» to old_plays + new_plays
set old's «class pPlC» to 0
my trash_file(location of old)
set end of old_database_ids to old_dbid
end
repeat with old_database_id in old_database_ids
delete (some track of library playlist 1 whose database ID is old_database_id)
end
end