-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiTuneMyWalkman.applescript
executable file
·1451 lines (1416 loc) · 70.2 KB
/
iTuneMyWalkman.applescript
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- iTuneMyWalkman.applescript
-- iTuneMyWalkman
(*
List of handlers in this script
-- EVENT HANDLERS
on launched
on should open untitled theObject
on should quit after last window closed theObject
on idle theObject
on clicked theObject
on choose menu item theObject
on change cell value theObject row theRow table column tableColumn value theValue
on selection changed theObject
-- OWN SUBROUTINES
on startsync()
on movepics(myprocess)
on getsongs()
on deleteolds()
on putsongs()
on copynext()
on cleanup()
-- HELPER FUNCTIONS
on getpath(path)
on strip(text, encoding)
on convertText(text, encoding)
on tcl(tclscript, arglist)
on shellcmd(cmd)
on whattodo(filetype, bitrate)
on updateballs()
on scriptsinstalled()
on fainstalled()
on showprefs()
on initprefs()
on checkforold()
*)
property debugging : false
property stage : "init"
property itmwversion : "0.951"
-- This should be updated whenever the iTunes scripts or the folder action script change.
-- At startup the value of this property is compared to the value stored in the preferences file.
-- If it has changed, we have newer scripts and they are installed at startup.
-- Conversion of characters 127 to 194 from unicode to ISO-Latin 1
property ISOConversionList : {196, 197, 199, 201, 209, 214, 220, 225, 224, 226, 228, 227, 229, 231, 233, 232, 234, 235, 237, 236, 238, 239, 241, 243, 242, 244, 246, 245, 250, 249, 251, 252, 134, 176, 162, 163, 167, 149, 182, 223, 174, 169, 153, 180, 168, 149, 198, 216, 149, 177, 149, 149, 165, 181, 149, 149, 149, 149, 149, 170, 186, 149, 230, 248, 191, 161}
-- EVENT HANDLERS
-- This is called first every time the application starts.
on launched
global copying
set copying to false
initprefs()
set debugging to contents of default entry "debugging" of user defaults
checkforold()
end launched
-- This handler checks whether iTuneMyWalkman is the frontmost application.
-- If yes, the main window is shown as the user probably opened the application directly.
-- If not, the call probably came from the iTunes script, so the main window should not be shown.
on should open untitled theObject
if name of (info for (path to frontmost application)) = name of me & ".app" then
considering numeric strings
using terms from application "System Events"
if system version of (system info) < "10.4" then
display alert (localized string "Mac OS X 10.4") buttons {localized string "Quit"} default button 1
quit
end if
tell application "Finder" to set hasitunes to exists application file id "com.apple.iTunes"
if hasitunes then tell application "Finder" to set itunes to name of application file id "com.apple.iTunes"
if not hasitunes or version of application itunes as text < "7" then
display alert (localized string "iTunes 7") buttons {localized string "Quit"} default button 1
quit
end if
end using terms from
end considering
updateballs()
try
tell application "Finder" to set itunesicon to POSIX path of ((application file id "com.apple.iTunes" as string) & ":Contents:Resources:iTunes.icns")
tell application "Finder" to set fasicon to POSIX path of ((application file id "com.apple.FolderActionsSetup" as string) & ":Contents:Resources:Folder Actions Setup.icns")
set image of image view "itunes" of window "main" to load image itunesicon
set image of image view "fas" of window "main" to load image fasicon
end try
show window "main"
end if
end should open untitled
-- Quits the application when the last window is closed
on should quit after last window closed theObject
if stage = "init" or stage = "done" then return true
return false
end should quit after last window closed
-- If the copying of files where done within a single method, the application would be unresponsive to user interaction during that time.
-- Instead, the stage property is set to copy, and files are copied individually from the idle handler.
on idle theObject
set workdone to 0
repeat while stage = "copy" and workdone < 100
set workdone to workdone + 1 + 10 * (copynext())
end repeat
return 1
end idle
on clicked theObject
global musicpath
if name of window of theObject = "main" then
if name of theObject = "paypal" then
open location "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=ilari%2escheinin%40helsinki%2efi&item_name=iTuneMyWalkman&no_shipping=1&no_note=1&tax=0¤cy_code=EUR&bn=PP%2dDonationsBF&charset=UTF%2d8"
else if name of theObject = "sync" then
startsync()
else if name of theObject = "prefs" then
showprefs()
else if name of theObject = "installscripts" then
set scriptdir to POSIX path of ((path to library folder from user domain as string) & "iTunes:Scripts:")
shellcmd("/bin/mkdir -p " & quoted form of scriptdir)
set mydir to (path to me) & "Contents:Resources:Scripts:" as string
shellcmd("/bin/cp " & (quoted form of POSIX path of mydir) & "iTMW*.scpt" & " " & quoted form of scriptdir)
shellcmd("/bin/rm -f " & quoted form of (scriptdir & "iTMW - Detect Phone.scpt"))
updateballs()
else if name of theObject = "removescripts" then
set scriptdir to POSIX path of ((path to library folder from user domain as string) & "iTunes:Scripts:")
shellcmd("/bin/rm -f " & (quoted form of scriptdir) & "iTMW*.scpt")
updateballs()
else if name of theObject = "installfa" then
set fadir to path to Folder Action scripts from user domain as string with folder creation
set mydir to (path to me) & "Contents:Resources:Scripts:" as string
shellcmd("/bin/cp " & quoted form of POSIX path of (mydir & "iTMW - Detect Phone.scpt") & " " & quoted form of POSIX path of fadir)
try
tell application "System Events"
set folder actions enabled to true
attach action to folder "Volumes" of startup disk using (fadir & "iTMW - Detect Phone.scpt")
end tell
end try
updateballs()
else if name of theObject = "removefa" then
try
tell application "System Events" to remove action from folder "Volumes" of startup disk using action name "iTMW - Detect Phone.scpt"
end try
set fadir to path to Folder Action scripts from user domain as string with folder creation
shellcmd("/bin/rm -f " & quoted form of POSIX path of (fadir & "iTMW - Detect Phone.scpt"))
updateballs()
end if
else if name of window of theObject = "progress" then
if name of theObject = "stop" then
set stage to "stop"
cleanup()
end if
else if name of window of theObject = "prefs" then
if name of theObject = "changemusic" then
set dir to quoted form of POSIX path of (choose folder "Choose the music folder of the phone:")
set contents of text field "musicpath" of box "music" of tab view item "general" of tab view "prefs" of window "prefs" to dir
else if name of theObject = "changemove" then
set dir to quoted form of POSIX path of (choose folder "Choose the target folder for pictures and videos:")
set contents of text field "movepath" of box "move" of tab view item "camera" of tab view "prefs" of window "prefs" to dir
else if name of theObject = "changeimage" then
set dir to quoted form of POSIX path of (choose folder "Choose the image folder of the phone:")
set contents of text field "imagepath" of box "image" of tab view item "camera" of tab view "prefs" of window "prefs" to dir
else if name of theObject = "changevideo" then
set dir to quoted form of POSIX path of (choose folder "Choose the camera video folder of the phone:")
set contents of text field "videopath" of box "video" of tab view item "camera" of tab view "prefs" of window "prefs" to dir
else if name of theObject = "newitem" then
set contentlist to {}
set contentlist to contents of table view "filetypes" of scroll view "filetypes" of tab view item "encode" of tab view "prefs" of window "prefs"
copy {".suffix", 0} to end of contentlist
set contents of table view "filetypes" of scroll view "filetypes" of tab view item "encode" of tab view "prefs" of window "prefs" to contentlist
else if name of theObject = "removeitem" then
set contentlist to {}
set theRow to selected row of table view "filetypes" of scroll view "filetypes" of tab view item "encode" of tab view "prefs" of window "prefs"
if theRow > 0 then
set contentlist to contents of table view "filetypes" of scroll view "filetypes" of tab view item "encode" of tab view "prefs" of window "prefs"
set newcontentlist to {}
repeat with i from 1 to count contentlist
if i ≠ theRow then copy item i of contentlist to end of newcontentlist
end repeat
set contents of table view "filetypes" of scroll view "filetypes" of tab view item "encode" of tab view "prefs" of window "prefs" to newcontentlist
end if
set enabled of button "removeitem" of tab view item "encode" of tab view "prefs" of window "prefs" to false
else if name of theObject = "save" then
tell tab view "prefs" of window "prefs"
tell tab view item "general"
set contents of default entry "musicPath" of user defaults of me to contents of text field "musicpath" of box "music"
set contents of default entry "phoneDetected" of user defaults of me to tag of current menu item of popup button "phonedetected"
set contents of default entry "askForConfirmation" of user defaults of me to content of button "confirmation"
set contents of default entry "SUCheckAtStartup" of user defaults of me to content of button "updates"
set contents of default entry "synchronizationComplete" of user defaults of me to tag of current menu item of popup button "endsync"
set contents of default entry "sizeLimit" of user defaults of me to (contents of text field "sizelimit" as number) * 1024 * 1024
end tell
tell tab view item "playlists"
set contents of default entry "syncMusic" of user defaults of me to content of button "syncmusic"
set contents of default entry "syncPodcasts" of user defaults of me to content of button "syncpodcasts"
set contents of default entry "syncAudiobooks" of user defaults of me to content of button "syncaudiobooks"
set contents of default entry "whichPlaylists" of user defaults of me to current row of matrix "whichlists"
set contents of default entry "writeM3UPlaylists" of user defaults of me to content of button "writem3uplaylists"
set contents of default entry "M3UEncoding" of user defaults of me to tag of current menu item of popup button "m3uencoding"
set contents of default entry "M3UPathPrefix" of user defaults of me to content of text field "m3upathprefix"
set contents of default entry "M3UPathSeparator" of user defaults of me to content of text field "m3upathseparator"
set chosenlists to {}
set checkedlist to contents of table view "playlists" of scroll view "playlists"
repeat with x in checkedlist
if ischecked of x then copy listname of x to end of chosenlists
end repeat
set contents of default entry "chosenPlaylists" of user defaults of me to chosenlists
end tell
tell tab view item "folders"
set contents of default entry "numberOfDirectoryLevels" of user defaults of me to tag of current menu item of popup button "dirlevel"
set contents of default entry "directoryStructure" of user defaults of me to tag of current menu item of popup button "dirstructure"
set contents of default entry "playlistFolder" of user defaults of me to content of text field "playlistfolder"
set contents of default entry "fixedPodcastFolder" of user defaults of me to content of button "fixedpodcastfolder"
set contents of default entry "podcastFolder" of user defaults of me to content of text field "podcastfolder"
set contents of default entry "fixedAudiobookFolder" of user defaults of me to content of button "fixedaudiobookfolder"
set contents of default entry "audiobookFolder" of user defaults of me to content of text field "audiobookfolder"
set od to text item delimiters of AppleScript
set text item delimiters of AppleScript to {":"}
set excludelist to contents of text field "excludelist"
set contents of default entry "dontTouch" of user defaults of me to every text item of excludelist
set text item delimiters of AppleScript to od
end tell
tell tab view item "playcount"
set contents of default entry "incrementPlayCountOnSync" of user defaults of me to contents of text field "ionsync"
set contents of default entry "incrementPlayCountOnCopy" of user defaults of me to contents of text field "ioncopy"
end tell
tell tab view item "encode"
set contents of default entry "reencoder" of user defaults of me to tag of current menu item of popup button "encoder" of box "encoderbox"
set contents of default entry "renameM4BToM4A" of user defaults of me to content of button "renamem4btom4a" of box "encoderbox"
set filetypelist to {}
set bitratelist to {}
set contentlist to {}
set contentlist to contents of table view "filetypes" of scroll view "filetypes"
repeat with x in contentlist
copy filetype of x to end of filetypelist
copy bitrate of x to end of bitratelist
end repeat
set contents of default entry "fileTypes" of user defaults of me to filetypelist
set contents of default entry "fileBitRateLimits" of user defaults of me to bitratelist
end tell
tell tab view item "camera"
set contents of default entry "processCameraImages" of user defaults of me to tag of current menu item of popup button "movepics"
set contents of default entry "moveImagesTo" of user defaults of me to contents of text field "movepath" of box "move"
set contents of default entry "cameraImagePath" of user defaults of me to contents of text field "imagepath" of box "image"
set contents of default entry "cameraVideoPath" of user defaults of me to contents of text field "videopath" of box "video"
set contents of default entry "handleS60Thumbnails" of user defaults of me to content of button "s60thumbs"
end tell
end tell
hide window "prefs"
else if name of theObject = "cancel" then
hide window "prefs"
end if
end if
end clicked
on choose menu item theObject
if name of theObject = "prefs" then tell button "prefs" of window "main" to perform action
end choose menu item
on change cell value theObject row theRow table column tableColumn value theValue
if identifier of tableColumn = "filetype" then
if character 1 of theValue = "." then
return theValue
else
return "." & theValue
end if
else if identifier of tableColumn = "bitrate" then
try
set x to theValue as number
on error
return false
end try
return x
end if
end change cell value
on selection changed theObject
if name of theObject is "filetypes" then
set theRow to selected row of theObject
if theRow = 0 then
set enabled of button "removeitem" of tab view item "encode" of tab view "prefs" of window "prefs" to false
else
set enabled of button "removeitem" of tab view item "encode" of tab view "prefs" of window "prefs" to true
end if
end if
end selection changed
-- OWN SUBROUTINES
on startsync()
if debugging then log "startsync begins"
global musicpath
set stage to "initsync"
set mymusicpath to contents of default entry "musicPath" of user defaults
repeat
set musicpath to getpath(mymusicpath)
if musicpath = "notfound" then
using terms from application "System Events"
set relocate to display alert (localized string "phone not found") message mymusicpath buttons {localized string "Cancel", localized string "Locate..."} default button 2
end using terms from
if button returned of relocate = (localized string "Cancel") then
set stage to "done"
return
end if
set mymusicpath to quoted form of POSIX path of (choose folder (localized string "Choose the music folder of the phone:"))
set contents of default entry "musicPath" of user defaults of me to mymusicpath
else if musicpath = "ambiguous" then
using terms from application "System Events"
display alert (localized string "ambiguous path") message mymusicpath buttons {localized string "OK"} default button 1
end using terms from
return
else
exit repeat
end if
end repeat
if (contents of default entry "askForConfirmation" of user defaults) then
using terms from application "System Events"
set confirmation to button returned of (display alert (localized string "confirmation") message musicpath & return & return & (localized string "confirmation2") buttons {localized string "Cancel", localized string "Continue"} default button 1)
end using terms from
if confirmation = (localized string "Cancel") then return
end if
global starttime, myencoder, oldenc, mybitrate, mysuffix, mydirlevel, mydirstruct, myincsync, myinccopy, myfiletypelimits, copied, copiedsize, notcopied, total
set starttime to current date
tell window "progress"
set content of text field "status" to "Initializing"
set content of text field "song" to ""
start progress indicator "progressbar"
update
show
end tell
set myprocess to contents of default entry "processCameraImages" of user defaults
if (myprocess) > 1 then movepics(myprocess)
set myencoder to contents of default entry "reencoder" of user defaults
set mydirlevel to contents of default entry "numberOfDirectoryLevels" of user defaults
set mydirstruct to contents of default entry "directoryStructure" of user defaults
set myincsync to contents of default entry "incrementPlayCountOnSync" of user defaults
set myinccopy to contents of default entry "incrementPlayCountOnCopy" of user defaults
set filetypelist to contents of default entry "fileTypes" of user defaults of me
set bitratelist to contents of default entry "fileBitRateLimits" of user defaults of me
set myfiletypelimits to {}
repeat with i from 1 to count filetypelist
copy {item i of filetypelist, item i of bitratelist} to end of myfiletypelimits
end repeat
if myencoder ≠ 1 then
set emptytrack to (path to me) & "Contents:Resources:empty.m4a" as string
tell application "iTunes"
set oldenc to current encoder
try
if myencoder = 3 then
set current encoder to encoder "MP3 encoder"
set mysuffix to ".mp3"
else
set current encoder to encoder "AAC encoder"
set mysuffix to ".m4a"
end if
set convertedtrack to convert alias emptytrack
set mybitrate to bit rate of item 1 of convertedtrack
delete item 1 of convertedtrack
my shellcmd("/bin/rm -f " & quoted form of POSIX path of (location of item 1 of convertedtrack))
on error msg
tell me to log msg
end try
end tell
end if
set total to 0
set copied to 0
set copiedsize to 0
set notcopied to 0
getsongs()
deleteolds()
putsongs()
if debugging then log "startsync ends"
end startsync
on movepics(myprocess)
if debugging then log "movepics begins"
tell window "progress"
set content of text field "status" to "Moving camera pictures and videos"
update
end tell
global theexcludestring
set fils to {}
set importlist to {}
set myexclude to contents of default entry "dontTouch" of user defaults
set s60thumbs to contents of default entry "handleS60Thumbnails" of user defaults
set excludestring to {}
repeat with x in myexclude
if x as string ≠ "" then copy " -not -iname " & (quoted form of x) & " -not -ipath " & quoted form of ("*/" & x & "/*") to end of excludestring
end repeat
set myimagepath to getpath(contents of default entry "cameraImagePath" of user defaults)
if myimagepath ≠ "notfound" and myimagepath ≠ "ambiguous" then
if s60thumbs then
set imgs to shellcmd("/usr/bin/find " & (quoted form of myimagepath) & " -type f -iname '*.jpg' -not -name '.*'" & excludestring)
else
set imgs to shellcmd("/usr/bin/find " & (quoted form of myimagepath) & " -type f -not -name '.*'" & excludestring)
end if
repeat with x in every paragraph of imgs
if x as string = "" then exit repeat
copy x as string to end of fils
copy alias (POSIX file (x as string) as string) to end of importlist
end repeat
end if
set myvideopath to getpath(contents of default entry "cameraVideoPath" of user defaults)
if myvideopath ≠ "notfound" and myvideopath ≠ "ambiguous" then
set vids to shellcmd("/usr/bin/find " & (quoted form of myvideopath) & " -type f -not -name '.*'" & excludestring)
repeat with x in every paragraph of vids
if x as string = "" then exit repeat
copy x as string to end of fils
copy alias (POSIX file (x as string) as string) to end of importlist
end repeat
end if
if fils = {} then return
if myprocess = 3 then -- iPhoto
-- Using a try block to see if that allows the program to run on a computer without iPhoto.
-- I doubt it will help, so we might need to use this kind of syntax to mask iPhoto:
-- do shell script "osascript -e 'tell application \"iPhoto\" ... '"
try
tell application "Finder" to set hasiphoto to exists application file id "com.apple.iPhoto"
if hasiphoto then
tell application "Finder" to set iphoto to name of application file id "com.apple.iPhoto"
considering numeric strings
if not hasiphoto or version of application iphoto < "6" then
using terms from application "System Events"
display alert (localized string "iPhoto 6") buttons {localized string "OK"} default button 1
end using terms from
return
end if
end considering
using terms from application "iPhoto"
tell application iphoto
import from importlist
repeat while importing
delay 1
end repeat
set imported to count photos of last rolls album
end tell
end using terms from
set num to count importlist
if imported ≠ num then
set sure to display dialog "iTuneMyWalkman tried to import " & num & " items to iPhoto, but your last rolls album contains " & imported & " pictures. Should iTuneMyWalkman delete the files from the memory stick or not?" buttons {localized string "Delete", localized string "Keep Files"}
if button returned of sure = (localized string "Keep Files") then return
end if
repeat with x in fils
if s60thumbs then
shellcmd("/bin/rm -rf " & (quoted form of x) & "*")
else
shellcmd("/bin/rm -rf " & quoted form of x)
end if
end repeat
end if
on error msg
log "Error trying to import to iPhoto: " & msg
end try
else
set mymovepath to contents of default entry "moveImagesTo" of user defaults
if mymovepath ≠ "notfound" and mymovepath ≠ "ambiguous" then
set newdir to shellcmd("/bin/date '+%y-%m-%d\\ %H.%M.%S'")
shellcmd("/bin/mkdir -p " & mymovepath & newdir)
repeat with x in fils
if x as string = "" then exit repeat
try
shellcmd("/bin/cp " & (quoted form of x) & " " & mymovepath & newdir)
if s60thumbs then
shellcmd("/bin/rm " & (quoted form of x) & "*")
else
shellcmd("/bin/rm " & quoted form of x)
end if
on error msg
using terms from application "System Events"
display alert (localized string "picture error") message msg buttons {localized string "OK"} default button 1
end using terms from
end try
end repeat
end if
end if
if debugging then log "movepics ends"
end movepics
on getsongs()
if debugging then log "getsongs begins"
tell window "progress"
set content of text field "status" to "Collecting data from iTunes"
update
end tell
global musicpath, mydirlevel, mydirstruct, myincsync, myinccopy, myencoder, mybitrate, mysuffix, totalsize
global filelist, filelistSrc, songlist, songlistSrc, dirlist, targetlist, targetlistSrc, tracklist, tracklistSrc, encodelist, encodelistSrc, existinglist, existinglistSrc
set blocksize to 8192
set filelistSrc to {}
set filelist to a reference to filelistSrc
set songlist to {}
set targetlistSrc to {}
set targetlist to a reference to targetlistSrc
set existinglistSrc to {}
set existinglist to a reference to existinglistSrc
set tracklistSrc to {}
set tracklist to a reference to tracklistSrc
set encodelistSrc to {}
set encodelist to a reference to encodelistSrc
set m3u to {}
set totalsize to 0
set mysizelimit to contents of default entry "sizeLimit" of user defaults
if mysizelimit ≤ 0 then
tell application "Finder" to set mysizelimit to (free space of disk of folder (POSIX file musicpath as string)) + mysizelimit
if debugging then tell me to log "free space on disk: " & mysizelimit
-- Use the du (disk usage) command to subtract the potential size the music files to be removed.
set myexclude to contents of default entry "dontTouch" of user defaults
-- Build up the options for du to ignore the folders to not to touch.
set excludestring to {}
repeat with x in myexclude
if x as string ≠ "" then copy "-I " & (quoted form of x) & " " to end of excludestring
end repeat
-- Get the size which would be freed if the music folder gets cleaned.
set musicpathsize to last paragraph of shellcmd("/usr/bin/du -k -c " & excludestring & quoted form of musicpath)
set musicpathsize to ((first word of musicpathsize as integer) * 1024)
if debugging then tell me to log "space occupied by " & musicpath & ": " & musicpathsize
-- Add that size to the available size limit.
set mysizelimit to mysizelimit + musicpathsize
tell window "progress"
set content of progress indicator "progressbar" to 0
set maximum value of progress indicator "progressbar" to mysizelimit
set indeterminate of progress indicator "progressbar" to false
update
end tell
end if
if debugging then tell me to log "size limit: " & mysizelimit
copy contents of default entry "renameM4BToM4A" of user defaults to renamem4btom4a
copy contents of default entry "syncMusic" of user defaults to syncmusic
copy contents of default entry "syncPodcasts" of user defaults to syncpodcasts
copy contents of default entry "syncAudiobooks" of user defaults to syncaudiobooks
copy contents of default entry "whichPlaylists" of user defaults to whichlists
copy contents of default entry "writeM3UPlaylists" of user defaults to writem3uplaylists
copy contents of default entry "M3UEncoding" of user defaults to m3uencoding
copy contents of default entry "M3UPathPrefix" of user defaults to m3upathprefix
copy contents of default entry "M3UPathSeparator" of user defaults to m3upathseparator
copy my strip(contents of default entry "playlistfolder" of user defaults, m3uencoding) to playlistfolder
copy contents of default entry "fixedPodcastFolder" of user defaults to fixedpodcastfolder
copy my strip(contents of default entry "podcastFolder" of user defaults, m3uencoding) to podcastfolder
copy contents of default entry "fixedAudiobookFolder" of user defaults to fixedaudiobookfolder
copy my strip(contents of default entry "audiobookFolder" of user defaults, m3uencoding) to audiobookfolder
-- "E:/music/" and "E:/music", otherwise deleteolds() will delete "E:/music"
set dirlist to {musicpath, text 1 thru -2 of musicpath}
if writem3uplaylists then
copy musicpath & playlistfolder to end of dirlist
shellcmd("/bin/mkdir -p " & quoted form of (musicpath & playlistfolder))
shellcmd("/usr/bin/find " & quoted form of (musicpath & playlistfolder) & " -iname " & quoted form of "*.m3u" & " -exec /bin/rm -f {} " & quoted form of ";")
end if
if fixedpodcastfolder then copy musicpath & podcastfolder to end of dirlist
if fixedaudiobookfolder then copy musicpath & audiobookfolder to end of dirlist
if m3upathseparator = "" then set m3upathseparartor to "/"
if m3upathprefix ≠ "" and text ((count m3upathprefix) - (count m3upathseparator) + 1) thru end of m3upathprefix ≠ m3upathseparator then
-- "" and "/" => ""
-- "E:" and "/" => "E:/"
-- "E:/" and "/" => "E:/"
set m3upathprefix to m3upathprefix & m3upathseparator
end if
if debugging then tell me to log "m3upathprefix: " & m3upathprefix
if syncpodcasts then
tell application "iTunes"
set plists to a reference to (every playlist whose special kind = Podcasts)
repeat with plist in plists
set m3u to {}
try
if fixedpodcastfolder then
set playlistname to podcastfolder
else
set playlistname to my strip(name of plist as string, m3uencoding)
end if
set filetracks to (a reference to (every file track of plist whose enabled = true))
repeat with x in filetracks
try
copy x to currenttrack
copy location of x to songfile
if songfile ≠ missing value and songfile is not in filelist then
tell application "Finder"
set songname to name of songfile as string
set suffix to "." & name extension of songfile as string
if renamem4btom4a and suffix = ".m4b" then
set songname to text 1 thru (-1 - (count suffix)) of songname
set suffix to ".m4a"
set songname to songname & suffix
end if
end tell
set howto to my whattodo(suffix, bit rate of x)
if howto > -1 then
if howto = 1 then
set filesize to (duration of x) * mybitrate * 125 -- 125 = 1000 / 8
set songname to text 1 thru (-1 - (count suffix)) of songname & mysuffix
set encoded to true
else
set filesize to contents of size of x
set encoded to false
end if
if mysizelimit = 0 or (totalsize + filesize ≤ mysizelimit) then
if myincsync > 0 then
set played count of x to (played count of x) + myincsync
set played date of x to current date
end if
set artistname to playlistname
if artistname = "" then set artistname to "Podcast"
if mydirstruct = 1 then -- artist/album/
set albumname to my strip(album of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(album artist of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(artist of x as string, m3uencoding)
if albumname = "" then set albumname to artistname
else if mydirstruct = 2 then -- Music/playlist/
set albumname to my strip(album of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(album artist of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(artist of x as string, m3uencoding)
if albumname = "" then set albumname to artistname
else -- iTuneMyWalkman/genre/
set albumname to my strip(album of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(album artist of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(artist of x as string, m3uencoding)
if albumname = "" then set albumname to artistname
end if
if mydirlevel = 2 then -- two levels of folders
copy m3upathprefix & artistname & m3upathseparator & albumname & m3upathseparator to end of m3u
copy musicpath & artistname & "/" & albumname & "/" & songname to end of targetlist
copy musicpath & artistname to end of dirlist
copy musicpath & artistname & "/" & albumname to end of dirlist
else if mydirlevel = 1 then -- one folder level
copy m3upathprefix & albumname & m3upathseparator to end of m3u
copy musicpath & albumname & "/" & songname to end of targetlist
copy musicpath & albumname to end of dirlist
else -- no folders
copy m3upathprefix to end of m3u
copy musicpath & songname to end of targetlist
end if
-- copy songname & return & linefeed to end of m3u
-- the "linefeed" constant came with Leopard and is not available in Tiger, so we use this instead:
copy songname & return & (ASCII character 10) to end of m3u
copy songname to end of songlist
copy songfile to end of filelist
copy encoded to end of encodelist
copy currenttrack to end of tracklist
set totalsize to totalsize + (filesize + blocksize - 1) div blocksize * blocksize
tell window "progress" of application "iTuneMyWalkman"
set content of progress indicator "progressbar" to totalsize
update
end tell
else
if debugging then tell me to log "no space left for " & songfile & " (needs " & filesize & ", only " & mysizelimit - totalsize & " available)"
if mysizelimit - totalsize ≤ 3 * 1024 * 1024 then exit repeat
end if
end if
end if
on error msg
log msg
end try
end repeat
if writem3uplaylists then
tell me
set m3u to "#EXTM3U" & return & (ASCII character 10) & m3u as string -- adding the #EXTM3U header
set m3u to convertText(m3u, m3uencoding)
set fp to open for access (POSIX file (musicpath & playlistfolder & "/" & playlistname & ".m3u")) with write permission
set eof fp to 0
if m3uencoding = 1 then
write m3u to fp as «class utf8»
else
write m3u to fp
end if
close access fp
end tell
end if
on error msg
log msg
end try
end repeat
end tell
end if
if syncaudiobooks then
tell application "iTunes"
set plists to a reference to (every playlist whose special kind = Audiobooks)
repeat with plist in plists
set m3u to {"#EXTM3U" & return & (ASCII character 10)}
try
if fixedaudiobookfolder then
set playlistname to audiobookfolder
else
set playlistname to my strip(name of plist as string, m3uencoding)
end if
set filetracks to (a reference to (every file track of plist whose enabled = true))
repeat with x in filetracks
try
copy x to currenttrack
copy location of x to songfile
if songfile ≠ missing value and songfile is not in filelist then
tell application "Finder"
set songname to name of songfile as string
set suffix to "." & name extension of songfile as string
if renamem4btom4a and suffix = ".m4b" then
set songname to text 1 thru (-1 - (count suffix)) of songname
set suffix to ".m4a"
set songname to songname & suffix
end if
end tell
set howto to my whattodo(suffix, bit rate of x)
if howto > -1 then
if howto = 1 then
set filesize to (duration of x) * mybitrate * 125 -- 125 = 1000 / 8
set songname to text 1 thru (-1 - (count suffix)) of songname & mysuffix
set encoded to true
else
set filesize to contents of size of x
set encoded to false
end if
if mysizelimit = 0 or (totalsize + filesize ≤ mysizelimit) then
if myincsync > 0 then
set played count of x to (played count of x) + myincsync
set played date of x to current date
end if
set artistname to playlistname
if artistname = "" then set artistname to "Audiobooks"
if mydirstruct = 1 then -- artist/album/
set albumname to my strip(album of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(album artist of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(artist of x as string, m3uencoding)
if albumname = "" then set albumname to artistname
else if mydirstruct = 2 then -- Music/playlist/
set albumname to my strip(album of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(album artist of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(artist of x as string, m3uencoding)
if albumname = "" then set albumname to artistname
else -- iTuneMyWalkman/genre/
set albumname to my strip(album of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(album artist of x as string, m3uencoding)
if albumname = "" then set albumname to my strip(artist of x as string, m3uencoding)
if albumname = "" then set albumname to artistname
end if
if mydirlevel = 2 then -- two levels of folders
copy m3upathprefix & artistname & m3upathseparator & albumname & m3upathseparator to end of m3u
copy musicpath & artistname & "/" & albumname & "/" & songname to end of targetlist
copy musicpath & artistname to end of dirlist
copy musicpath & artistname & "/" & albumname to end of dirlist
else if mydirlevel = 1 then -- one folder level
copy m3upathprefix & albumname & m3upathseparator to end of m3u
copy musicpath & albumname & "/" & songname to end of targetlist
copy musicpath & albumname to end of dirlist
else -- no folders
copy m3upathprefix to end of m3u
copy musicpath & songname to end of targetlist
end if
-- copy songname & return & linefeed to end of m3u
-- the "linefeed" constant came with Leopard and is not available in Tiger, so we use this instead:
copy songname & return & (ASCII character 10) to end of m3u
copy songname to end of songlist
copy songfile to end of filelist
copy encoded to end of encodelist
copy currenttrack to end of tracklist
set totalsize to totalsize + (filesize + blocksize - 1) div blocksize * blocksize
tell window "progress" of application "iTuneMyWalkman"
set content of progress indicator "progressbar" to totalsize
update
end tell
else
if debugging then tell me to log "no space left for " & songfile & " (needs " & filesize & ", only " & mysizelimit - totalsize & " available)"
if mysizelimit - totalsize ≤ 3 * 1024 * 1024 then exit repeat
end if
end if
end if
on error msg
log msg
end try
end repeat
if writem3uplaylists then
tell me
set m3u to "#EXTM3U" & return & (ASCII character 10) & m3u as string -- adding the #EXTM3U header
set m3u to convertText(m3u, m3uencoding)
set fp to open for access (POSIX file (musicpath & playlistfolder & "/" & playlistname & ".m3u")) with write permission
set eof fp to 0
if m3uencoding = 1 then
write m3u to fp as «class utf8»
else
write m3u to fp
end if
close access fp
end tell
end if
on error msg
log msg
end try
end repeat
end tell
end if
if syncmusic then
if whichlists = 1 then
set plists to {}
tell application "iTunes"
set userplaylists to a reference to (every user playlist)
repeat with x in userplaylists
try
if (exists parent of x) and (name of parent of x begins with "iTuneMyWalkman" or name of parent of x begins with "iTMW") then
copy name of x to end of plists
end if
if special kind of x ≠ folder and (name of x begins with "iTuneMyWalkman" or name of x begins with "iTMW") then
copy name of x to end of plists
end if
on error msg
log msg
end try
end repeat
end tell
else
set plists to contents of default entry "chosenPlaylists" of user defaults
end if
if plists = {} then
using terms from application "System Events"
display alert (localized string "playlist error") message (localized string "create playlists") buttons {localized string "OK"} default button 1
end using terms from
end if
repeat with i from 1 to count plists
repeat with j from 2 to (count plists) - i + 1
if item (j - 1) of plists > item j of plists then
set tmp to item (j - 1) of plists
set item (j - 1) of plists to item j of plists
set item j of plists to tmp
end if
end repeat
end repeat
tell application "iTunes"
set musicm3u to {}
repeat with plist in plists
set m3u to {}
try
if exists user playlist plist then
if debugging then tell me to log "next playlist"
set playlistname to my strip(name of user playlist plist as string, m3uencoding)
set filetracks to (a reference to (every file track of user playlist plist whose enabled = true))
repeat with x in filetracks
try
copy x to currenttrack
copy location of x to songfile
if songfile ≠ missing value and songfile is not in filelist then
tell application "Finder"
set songname to name of songfile as string
set suffix to "." & name extension of songfile as string
if renamem4btom4a and suffix = ".m4b" then
set songname to text 1 thru (-1 - (count suffix)) of songname
set suffix to ".m4a"
set songname to songname & suffix
end if
end tell
set howto to my whattodo(suffix, bit rate of x)
if howto > -1 then
if howto = 1 then
set filesize to (duration of x) * mybitrate * 125 -- 125 = 1000 / 8
set songname to text 1 thru (-1 - (count suffix)) of songname & mysuffix
set encoded to true
else
set filesize to contents of size of x
set encoded to false
end if
if mysizelimit = 0 or (totalsize + filesize ≤ mysizelimit) then
if myincsync > 0 then
set played count of x to (played count of x) + myincsync
set played date of x to current date
end if
if mydirstruct = 1 then -- artist/album/
set artistname to my strip(album artist of x as string, m3uencoding)
if artistname = "" then set artistname to my strip(artist of x as string, m3uencoding)
if artistname = "" then set artistname to localized string "Unknown Artist"
set albumname to my strip(album of x as string, m3uencoding)
if albumname = "" then set albumname to localized string "Unknown Album"
set discnum to disc number of x
if discnum > 1 then set albumname to albumname & " " & discnum
else if mydirstruct = 2 then -- Music/playlist/
set artistname to "Music"
set albumname to playlistname
else -- iTuneMyWalkman/genre/
set artistname to "Music"
set albumname to my strip(genre of x as string, m3uencoding)
if albumname = "" then set albumname to "Unknown Genre"
end if
if mydirlevel = 2 then -- two levels of folders
copy m3upathprefix & artistname & m3upathseparator & albumname & m3upathseparator to end of m3u
copy musicpath & artistname & "/" & albumname & "/" & songname to end of targetlist
copy musicpath & artistname to end of dirlist
copy musicpath & artistname & "/" & albumname to end of dirlist
else if mydirlevel = 1 then -- one folder level
copy m3upathprefix & albumname & m3upathseparator to end of m3u
copy musicpath & albumname & "/" & songname to end of targetlist
copy musicpath & albumname to end of dirlist
else -- no folders
copy m3upathprefix to end of m3u
copy musicpath & songname to end of targetlist
end if
-- copy songname & return & linefeed to end of m3u
-- the "linefeed" constant came with Leopard and is not available in Tiger, so we use this instead:
copy songname & return & (ASCII character 10) to end of m3u
copy songname to end of songlist
copy songfile to end of filelist
copy encoded to end of encodelist
copy currenttrack to end of tracklist
set totalsize to totalsize + (filesize + blocksize - 1) div blocksize * blocksize
tell window "progress" of application "iTuneMyWalkman"
set content of progress indicator "progressbar" to totalsize
update
end tell
else
if debugging then tell me to log "no space left for " & songfile & " (needs " & filesize & ", only " & mysizelimit - totalsize & " available)"
if mysizelimit - totalsize ≤ 3 * 1024 * 1024 then exit repeat
end if
end if
end if
on error msg
log msg
end try
end repeat
if writem3uplaylists then
tell me
if debugging then tell me to log "adding to Music.m3u"
copy m3u to end of musicm3u
if debugging then tell me to log "Writing " & POSIX file (musicpath & playlistfolder & "/" & playlistname & ".m3u")
set m3u to "#EXTM3U" & return & (ASCII character 10) & m3u as string -- adding the #EXTM3U header
if debugging then tell me to log "converted to string"
set m3u to convertText(m3u, m3uencoding)
if debugging then tell me to log "open"
set fp to open for access (POSIX file (musicpath & playlistfolder & "/" & playlistname & ".m3u")) with write permission
set eof fp to 0
if debugging then tell me to log "write"
if m3uencoding = 1 then
write m3u to fp as «class utf8»
else
write m3u to fp
end if
close access fp
end tell
end if
end if
if debugging then tell me to log "playlist finished"
on error msg
log msg
end try
end repeat
if writem3uplaylists then
tell me
if debugging then tell me to log "Writing Music.3mu"
set musicm3u to "#EXTM3U" & return & (ASCII character 10) & musicm3u as string -- adding the #EXTM3U header
set musicm3u to convertText(musicm3u, m3uencoding)
set fp to open for access (POSIX file (musicpath & playlistfolder & "/Music.m3u")) with write permission
set eof fp to 0
if debugging then tell me to log "write"
if m3uencoding = 1 then
write (musicm3u as string) to fp as «class utf8»
else
write (musicm3u as string) to fp
end if
close access fp
end tell
end if
end tell
end if
if debugging then
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
tell me
log "filelist: " & return & filelist
log "targetlist: " & return & targetlist
log "dirlist: " & return & dirlist
log "getsongs ends"
end tell
set AppleScript's text item delimiters to ASTID
end if
end getsongs
on deleteolds()
if debugging then log "deleteolds begins"
tell window "progress"
set content of text field "status" to "Cleaning up memory card"
set content of progress indicator "progressbar" to 0
set indeterminate of progress indicator "progressbar" to true
update
end tell
global musicpath, dirlist, targetlist, theexcludestring, existinglist
set myexclude to contents of default entry "dontTouch" of user defaults
set excludestring to {}
repeat with x in myexclude
if x as string ≠ "" then copy " -not -iname " & (quoted form of x) & " -not -ipath " & quoted form of ("*/" & x & "/*") to end of excludestring
end repeat
set dirs to shellcmd("/usr/bin/find " & (quoted form of text 1 thru -2 of musicpath) & " -type d" & excludestring)
set counter to 0
tell window "progress"
set maximum value of progress indicator "progressbar" to (count of paragraphs of dirs) * 2
set indeterminate of progress indicator "progressbar" to false
update
end tell
repeat with x in every paragraph of dirs
if x as string = "" then exit repeat
if x is not in dirlist then
if debugging then log "deleting unneeded directory: " & x
shellcmd("/bin/rm -rf " & quoted form of x)
end if
set counter to counter + 1
tell window "progress"
set content of progress indicator "progressbar" to counter
update
end tell
end repeat
tell window "progress"
set content of progress indicator "progressbar" to 0
set indeterminate of progress indicator "progressbar" to true
update