Hi all!
I guess you’ve all had it at some point. A lot of almost identical “if else” codes after each other.
Is it possible to make this into a short, smart other code? I don’t have a clue how to do it, but i guess you should use repeat with x in y.
Any way. The script i have moves, based on the file extension, to one or another folder. at this moment i have all these variables called Ext (VideoExt, MusicExt) which all are a list of file types that are respectively A video and a music file.
Example:
if ext is in VideoExt then
wTf_("Detected file type: Video")
do shell script "mv " & quoted form of i & space & quoted form of VideoPath
wTf_(fileName & " moved to Video")
userLog_("Detected a " & ext & " file, move to " & VideoPath & " succesful")
else if (MusicExt contains ext) = true then
wTf_("Detected file type: Music")
do shell script "mv " & quoted form of i & space & quoted form of MusicPath
wTf_(fileName & " moved to Music")
userLog_("Detected a " & ext & " file, move to " & MusicPath & " succesful")
else if (DocumentExt contains ext) = true then
wTf_("Detected file type: Document")
do shell script "mv " & quoted form of i & space & quoted form of DocumentPath
wTf_(fileName & " moved to Documents")
userLog_("Detected a " & ext & " file, move to " & DocumentPath & " succesful")
else if ext is in TorrentExt then
wTf_("Detected file type: Torrent")
do shell script "mv " & quoted form of i & space & quoted form of TorrentPath
wTf_(fileName & " moved to Torrents")
userLog_("Detected a " & ext & " file, move to " & TorrentPath & " succesful")
else if (PictureExt contains ext) = true then
wTf_("Detected file type: Picture")
do shell script "mv " & quoted form of i & space & quoted form of PicturePath
wTf_(fileName & " moved to Pictures")
userLog_("Detected a " & ext & " file, move to " & PicturePath & " succesful")
else
wTf_("Detected file type: Other")
tell application "System Events" to set filesize to (size of ii)
if filesize is greater than "524288000" then
wTf_("File is larger than 500mb (" & filesize & " bytes)")
do shell script "mv " & quoted form of i & space & quoted form of LargeDataPath
wTf_(fileName & " moved to Large Files")
userLog_("Detected a large file (" & filesize & " bytes), move to " & LargeDataPath & " succesful")
else
wTf_("File is small")
do shell script "mv " & quoted form of i & space & quoted form of OtherPath
wTf_(fileName & " moved to Other files")
userLog_("Detected a " & ext & " file, move to " & OtherPath & " succesful")
end if
end if
Is it possible to slim this down to just a few lines?
Also, for when needed, here is the rest of the script:
on adding folder items to this_folder after receiving added_items
wTf_("----------------")
wTf_("Beginning of debug log file")
wTf_("$t")
userLog_("===========")
userLog_("Beginning of user log file")
try
display notification "New file(s) detected!"
set VideoPath to POSIX path of (POSIX file "/Volumes/Macintosh HD/Movies")
set VideoExt to {"mp4", "avi", "mkv"}
set DocumentPath to POSIX path of (POSIX file "/Volumes/Macintosh HD/Documents/")
set DocumentExt to {"doc", "docx", "xlsx", "xls", "pages"}
set MusicPath to POSIX path of (POSIX file "/Volumes/Macintosh HD/Music/")
set MusicExt to {"mp3", "aac", "AIFF", "m4a", "ogg", "wav"}
set PicturePath to POSIX path of (POSIX file "/Volumes/Macintosh HD/Pictures/")
set PictureExt to {"jpg", "bmp", "tiff", "png"}
set LargeDataPath to (POSIX file "/Volumes/Macintosh HD/Downloads/Large/")
set OtherPath to POSIX path of (POSIX file "/Volumes/Macintosh HD/Downloads/Others/")
set TorrentPath to POSIX path of (POSIX file "/Volumes/Macintosh HD/Torrents/")
set TorrentExt to {"torrent", "TORRENT"}
set y to {"VideoExt", "MusicExt", "TorrentExt"}
set Filecount to 0
repeat with i in added_items
userLog_("Added file: " & i)
end repeat
repeat with i in added_items
set Filecount to Filecount + 1
wTf_(("File with: " & i) as text)
set filepath to i as text
tell application "Finder"
set fileName to name of i
set ext to name extension of i
set ext1 to ("File Extension: " & (ext as text)) as text
end tell
wTf_(ext1)
set ii to i
set i to POSIX path of i
if ext is in VideoExt then
wTf_("Detected file type: Video")
do shell script "mv " & quoted form of i & space & quoted form of VideoPath
wTf_(fileName & " moved to Video")
userLog_("Detected a " & ext & " file, move to " & VideoPath & " succesful")
else if (MusicExt contains ext) = true then
wTf_("Detected file type: Music")
do shell script "mv " & quoted form of i & space & quoted form of MusicPath
wTf_(fileName & " moved to Music")
userLog_("Detected a " & ext & " file, move to " & MusicPath & " succesful")
else if (DocumentExt contains ext) = true then
wTf_("Detected file type: Document")
do shell script "mv " & quoted form of i & space & quoted form of DocumentPath
wTf_(fileName & " moved to Documents")
userLog_("Detected a " & ext & " file, move to " & DocumentPath & " succesful")
else if ext is in TorrentExt then
wTf_("Detected file type: Torrent")
do shell script "mv " & quoted form of i & space & quoted form of TorrentPath
wTf_(fileName & " moved to Torrents")
userLog_("Detected a " & ext & " file, move to " & TorrentPath & " succesful")
else if (PictureExt contains ext) = true then
wTf_("Detected file type: Picture")
do shell script "mv " & quoted form of i & space & quoted form of PicturePath
wTf_(fileName & " moved to Pictures")
userLog_("Detected a " & ext & " file, move to " & PicturePath & " succesful")
else
wTf_("Detected file type: Other")
tell application "System Events" to set filesize to (size of ii)
if filesize is greater than "524288000" then
wTf_("File is larger than 500mb (" & filesize & " bytes)")
do shell script "mv " & quoted form of i & space & quoted form of LargeDataPath
wTf_(fileName & " moved to Large Files")
userLog_("Detected a large file (" & filesize & " bytes), move to " & LargeDataPath & " succesful")
else
wTf_("File is small")
do shell script "mv " & quoted form of i & space & quoted form of OtherPath
wTf_(fileName & " moved to Other files")
userLog_("Detected a " & ext & " file, move to " & OtherPath & " succesful")
end if
end if
end repeat
on error x
set logsynctime to (current date) as text
wTf_("[" & (current date) & "]" & " - Error: " & x)
userLog_("One or more files were not moved succesfully, open debuglog.txt and search for: \"" & logsynctime & "\"")
display notification x with title "File not moved:"
end try
userLog_()
wTf_("End of log")
end adding folder items to
on userLog:x
set filepath to (path to desktop as string) & "Log.txt"
try
set fileRef to open for access file filepath with write permission
write (x & return) to fileRef starting at eof
close access fileRef
on error
try
close access file filepath
end try
end try
end userLog:
on wTf:(theActionExecuted)
if theActionExecuted contains "moved to" then
display notification theActionExecuted with title "File Move Complete!"
end if
if theActionExecuted = "$t" then
set theActionExecuted to "[" & (current date) & "]"
end if
set filepath to (path to desktop as string) & "debugLog.txt"
try
set fileRef to open for access file filepath with write permission
write (theActionExecuted & return) to fileRef starting at eof
close access fileRef
on error
try
close access file filepath
end try
end try
end wTf:
Model: MacbookPro9.1
AppleScript: 10.9
Browser: Safari 537.71
Operating System: Mac OS X (10.8)