Reputation: 9
Goal:
I am trying to write a script that I can tie into a folder action to move files from a download folder to the proper location. The folder will see music, movies and TV shows, or sports. There will be individual files as well as folders of multiple items. I have the skeleton of the script done, but I'm struggling with a few things that I know how to do in other languages.
Parameters:
TV: S%%E%% for individual files & S%% for folders
Movies: Folders with a single video file(mkv, mp4, m4v) and no S%%E%% in name, or folders without S%%
Audio: File type (mp3, aac, flac, wav) for individual files, folders will be tough seeing the contents
Issues:
How do I change the file selection to include folders and files for testing? I think this will work fine when I am giving the script input from a folder action in Automator, but not entirely sure yet.
It seems there are not wildcards available in Applescript to use in my comparisons.
My main issue is not knowing how to open a folder item and loop through the contents looking for certain extensions or names.
GitHub:https://github.com/wiebs2334/DownloadManager
Any suggestions or advice would be very much appreciated!
Upvotes: 0
Views: 109
Reputation: 285064
Vanilla AppleScript is not able to handle wildcards for text search but there is a free Scripting Addition Satimage.osax
which adds powerful functions for example find text with Regular Expression
You can find the OSAX on the Smile Companion OSAX site. Download Satimage osax 3.7.0 (build 411)
and put the file into /Library/ScriptingAdditions
(top level Library
!)
This is your optimized script using the Scripting Addition, the basic regex patterns are
"\\.S\\d{2}E"
: Look for a dot follows by capital S
followed by two digits and a captital E
"(nhl|nfl|ncaa)"
: Text contains "nhl" or "nfl" or "ncaa"If the text doesn't match the pattern an error is thrown.
The major changes are
run
handler shows a dialog to select files or foldersopen
handler is added to use the script as dropletif/end if - if/end if
expressions are replaced with if/else if/end if
If your are going to use the script as folder action both files and folders are considered anyway
property audioKeywords : {"mp3", "aac", "flac", "wav"}
property videoExtensions : {"mkv", "m4v", "mov", "mp4"}
on run
set {button returned:buttonReturned} to display dialog "Choose files or folders" buttons {"Cancel", "Choose Files", "Choose Folders"}
if buttonReturned is "Choose Files" then
classifyItems for (choose file with multiple selections allowed)
else
classifyItems for (choose folder with multiple selections allowed)
end if
end run
on open theItems
classifyItems for theItems
end open
to classifyItems for myFiles
repeat with anItem in myFiles
set anItem to anItem as text
set isVideo to false
set isTV to false
set isMovie to false
set isAudio to false
set isSports to false
tell application "System Events" to set {theClass, theName, theExt, theContainer} to {class, name, name extension, container} of disk item anItem --set variables
set className to theClass as text
(* &&&&&&&&&&&&&&&&&&&&&&&&&&&& Files &&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
if className is "file" then
########### Video ############
if theExt is in videoExtensions then --search through all video extensions
set isVideo to true
########### TV ############
try
find text "\\.S\\d{2}E" in theName with regexp
set isTV to true
on error
########### Sports ############
try
find text "(nhl|nfl|ncaa)" in theName with regexp
set isSports to true
on error
########### Movies ############
set isMovie to true
end try
end try
########### Audio ############
else if theExt is in audioKeywords then
set isAudio to true
end if
(* &&&&&&&&&&&&&&&&&&&&&&&&&&&& Folders &&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
else if className is "folder" then
########### TV ############
try
find text "\\.S\\d{2}\\." in theName with regexp
set isTV to true
on error
try
########### Audio ############
find text "(mp3|aac|flac|wav)" in theName with regexp
set isAudio to true
on error
########### Movie ############
set isMovie to true
end try
end try
end if
display dialog ("Type:" & theClass & " || Ext:" & theExt & " || Video:" & isVideo & "
" & theName & "
Movie:" & isMovie & " || TV:" & isTV & " || Sports:" & isSports & "
Audio:" & isAudio)
end repeat
end classifyItems
Upvotes: 1