I have written the following script to run as as folder action and it works fine when running it through in debug mode in Script Debugger, but as a folder action nothing happens.
I wonder if someone could have a look and spot what the problem is.
Thanks
Nick
on adding folder items to this_folder after receiving added_items
set convertPath to "Macintosh HD:opt:local:bin:ffmpeg"
tell application "Finder"
set inPath to this_folder as alias
set outPath to this_folder as alias
set filesToConvert to (name of every item of inPath whose name extension is "m4a")
end tell
repeat with i from 1 to count of filesToConvert
set fileName to removeExtension(item i of filesToConvert)
do shell script POSIX path of convertPath & " -i " & (quoted form of ((POSIX path of inPath) & item i of filesToConvert)) & " -f mp3 -ar 44100 -ab 192k -acodec libmp3lame " & (quoted form of (POSIX path of outPath)) & quoted form of (fileName & ".mp3") & " -map_meta_data " & (quoted form of ((POSIX path of inPath) & item i of filesToConvert)) & ":" & (quoted form of (POSIX path of outPath)) & quoted form of (fileName & ".mp3")
end repeat
end adding folder items to
to removeExtension(fileToParse) #display dialog fileToParse
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to “.”
return first text item of fileToParse
set text item delimiters of AppleScript to prevTIDs
end removeExtension
is the script saved in (~)/Library/Scripts/Folder Action Scripts?
By the way: I recommend to use a different folder for the output files, because the action handler will be triggered again when the converted file has been added. Although the script will work fine because of the .m4a filter, but it is a waste of time and resources.
cosmetic suggestions:
¢ this_folder is always an alias so any coercion to alias is not needed.
¢ The Finder is not needed to coerce a string path or file specifier to alias.
¢ Use a POSIX path for convertPath
¢ The removeExtension() handler is not reliable, if the filename contains more than one dot.
¢ The reset text item delimiters line in the removeExtension() handler will never be executed
I have made adjustment according to your suggestions, but it’s still not working. i think the issue may be with the triggering. it seems intermittent. i made a simple script:
on adding folder items to this_folder after receiving added_items
display dialog "1"
end adding folder items to
but i am not always getting a dialog when a add a file to the folder with the script attached.
the files need to go into the same folder as the original for it to suit my purposes (audio files are uploaded to this folder, which is used to build a playlist on a website). I want .m4a files (and possibly) other formats converted to mp3, but mp3 files to be skipped.
is there a more reliable way of getting just the filename without the extension?
below is the script in its current form:
on adding folder items to this_folder after receiving added_items
set convertPath to POSIX path of "Macintosh HD:opt:local:bin:ffmpeg"
set inPath to this_folder
set outPath to this_folder
tell application "Finder"
set filesToConvert to (name of every item of inPath whose name extension is "m4a")
end tell
repeat with i from 1 to count of filesToConvert
set fileName to removeExtension(item i of filesToConvert)
do shell script convertPath & " -i " & (quoted form of ((POSIX path of inPath) & item i of filesToConvert)) & " -f mp3 -ar 44100 -ab 192k -acodec libmp3lame " & (quoted form of (POSIX path of outPath)) & quoted form of (fileName & ".mp3") & " -map_meta_data " & (quoted form of ((POSIX path of inPath) & item i of filesToConvert)) & ":" & (quoted form of (POSIX path of outPath)) & quoted form of (fileName & ".mp3")
end repeat
end adding folder items to
to removeExtension(fileToParse)
set text item delimiters of AppleScript to "."
return first text item of fileToParse
end removeExtension
Somehow our last two posts got lost. Thanks for the updated script which is working well at the moment. I am trying to make a couple of additions to it though, and haven’t been able to work out how to do it:
Add a line checking for mp3s that are over a bitrate of 192kbps and adding these to the conversion queue
Deleting the original file that has been converted. The line I have added to do this is not working.
Would you be able to help with this?
Thanks
Nick
on adding folder items to this_folder after receiving added_items
set convertPath to "/opt/local/bin/ffmpeg"
set this_folderPOSIX to POSIX path of this_folder
tell application "Finder" to set filesToConvert to every item of this_folder whose (name extension is "m4a") or (name extension is "ogg") or (name extension is "wav") or (name extension is "flac") or (name extension is "aiff")
repeat with i from 1 to count of filesToConvert
set aFile to item i of filesToConvert as alias
set mp3Name to removeExtension(aFile) & ".mp3"
do shell script convertPath & " -i " & (quoted form of POSIX path of aFile) ¬
& " -f mp3 -ar 44100 -ab 192k -acodec libmp3lame " & quoted form of (this_folderPOSIX & mp3Name) ¬
& " -map_meta_data " & (quoted form of POSIX path of aFile) ¬
& ":" & quoted form of (this_folderPOSIX & mp3Name)
delete (every file of this_folder whose (name extension is "wav") or (name extension is "m4a") or (name extension is "aiff") or (name extension is "flac") or (name extension is "ogg") or (name extension is "nfo") or (name extension is "txt"))
end repeat
display dialog "new tracks on monte"
end adding folder items to
on removeExtension(fileAlias)
set {name:Nm, name extension:Ex} to info for fileAlias
if Ex is missing value then return Nm
return text 1 thru ((count Nm) - (count Ex) - 1) of Nm
end removeExtension