Posted By

zacharia on 02/04/11


Tagged


Versions (?)

Sort movies on disk


 / Published in: AppleScript
 

  1. tell application "iTunes"
  2.  
  3. set numberUpdated to 0
  4. if selection is {} then return
  5. set sel to a reference to selection
  6. repeat with aFile in sel
  7. repeat 1 times
  8. set mediaType to get video kind of aFile
  9. if mediaType is not "none" then
  10.  
  11. #work out the new path and filename
  12. set fileLocation to get location of aFile
  13.  
  14. tell application "Finder"
  15. set theFile to fileLocation
  16. set fileExtension to get name extension of theFile
  17. end tell
  18.  
  19. if mediaType is TV show then
  20. set newLocation to "BigBird:Movies:#TV"
  21. set seasonNumber to get season number of aFile
  22. if seasonNumber is less than 10 then set seasonNumber to ("0" & seasonNumber)
  23. set showName to get show of aFile as string
  24. set showName to my findAndReplace(":", "", showName)
  25. set episodeNumber to get episode number of aFile
  26. if episodeNumber is less than 10 then set episodeNumber to ("0" & episodeNumber)
  27. set episodeName to get name of aFile as string
  28. set episodeName to my findAndReplace(":", "", episodeName)
  29.  
  30. # create the folders if needed
  31. tell application "Finder"
  32.  
  33. set newPath to newLocation
  34.  
  35. set tempPath to newPath & ":" & showName
  36. if not (exists folder tempPath) then
  37. make new folder at newPath with properties {name:showName}
  38. end if
  39. set newPath to tempPath
  40.  
  41. set tempPath to newPath & ":Season " & seasonNumber
  42. if not (exists folder tempPath) then
  43. set tempName to "Season " & seasonNumber
  44. make new folder at newPath with properties {name:tempName}
  45. end if
  46. set newPath to newPath & ":" & "Season " & seasonNumber
  47.  
  48. end tell
  49.  
  50. set newName to episodeNumber & " - " & episodeName & "." & fileExtension as string
  51.  
  52.  
  53. end if
  54. if mediaType is movie then
  55. set newLocation to "BigBird:Movies"
  56. set movieTitle to get name of aFile as string
  57. set movieYear to get year of aFile
  58.  
  59. set movieTitle to my findAndReplace(":", "", movieTitle)
  60.  
  61. set newName to movieTitle & " - " & movieYear & "." & fileExtension
  62. set newPath to newLocation
  63.  
  64. end if
  65.  
  66. #skip if file is already correctly renamed
  67. if fileLocation is equal to newPath & ":" & newName then exit repeat
  68.  
  69. # move the file
  70. tell application "Finder"
  71.  
  72. move theFile to newPath
  73. set name of theFile to newName
  74.  
  75. end tell
  76.  
  77. set numberUpdated to numberUpdated + 1
  78.  
  79. end if
  80. end repeat
  81. end repeat
  82.  
  83. display dialog "Updated and moved " & numberUpdated & " files"
  84.  
  85. end tell
  86.  
  87. on findAndReplace(tofind, toreplace, TheString)
  88.  
  89. set ditd to text item delimiters
  90. set text item delimiters to tofind
  91. set textItems to text items of TheString
  92. set text item delimiters to toreplace
  93. if (class of TheString is string) then
  94. set res to textItems as string
  95. else -- if (class of TheString is Unicode text) then
  96. set res to textItems as Unicode text
  97. end if
  98. set text item delimiters to ditd
  99. return res
  100.  
  101. end findAndReplace

Report this snippet  

You need to login to post a comment.