folder actions not working, or only working intermittently

Hi

I am having intermittent problems with folder actions not working/getting triggered. I have tried restarting my Mac, and on one occasion this worked, but on others not. Because I am experiencing this with a number of folder actions, and they work fine on occasions I don’t think it is anything to do with specific folder actions, but rather something to do with OS X. Is anyone else experiencing these issues, in 10.6.6, and does anyone have any suggestion as to what I might try to get this working?

Thanks

Nick

I’m using 10.4 at work, and I’ve noticed similar behaviour. I’ve spent a lot of time messing around with folder actions, and I think somewhere along the way I corrupted some preferences file or something. Either that or in certain circumstances, one of my actions is causing the System Events app to crash. I’ll mainly notice it when one of my action-attached folders doesn’t’ behave as expected, and sure enough, I have to right click and choose “Enable Folder Actions” again. To get around it, I wrote this script which deletes my folder actions and reattaches them to their proper folders, and I have it set through iCal to run a couple times a day (I had also noticed that one of my other scripts which attaches actions would sometimes fail to properly delete actions, and they’d end up running 3 or 4 times for the same file - particularly after a hard restart). The script forces System Events to start if it is stopped, thus re-enabling folder actions.


tell application "System Events"
	delete every folder action
end tell

try
	set theDesiredFolderPath to "FTP:Incoming:OsseoPressNews:NEWS:Finished News Pages" as alias
	set theDesiredFolderActionScriptPath to "2074:Users:admin:Library:Scripts:Folder Action Scripts:FTP pages.scpt" as alias
	
	tell application "System Events"
		attach action to theDesiredFolderPath using theDesiredFolderActionScriptPath
	end tell
	
	set theDesiredFolderPath to "FTP:Incoming:OsseoPressNews:NEWS:Pages For News" as alias
	set theDesiredFolderActionScriptPath to "2074:Users:admin:Library:Scripts:Folder Action Scripts:FTP pages - wrong folder.scpt" as alias
	
	tell application "System Events"
		attach action to theDesiredFolderPath using theDesiredFolderActionScriptPath
	end tell
	
	set theDesiredFolderPath to "2074:private:var:tmp:folders.501:TemporaryItems:" as alias
	set theDesiredFolderActionScriptPath to "2074:Users:admin:Library:Scripts:Folder Action Scripts:QuarkFullscreen.scpt" as alias
	
	tell application "System Events"
		attach action to theDesiredFolderPath using theDesiredFolderActionScriptPath
	end tell	
	
on error the error_message number the error_number
	set the error_text to "Error: " & the error_number & ". " & the error_message
end try

relishgargler,

the ts posted his question also on dicussion.apple.com and his question is answered there already.

How bout that link?

Here is the solution

I think the TS should be responsible for that.

I don’t mind sharing it, and saving others the effort to find it:
http://discussions.apple.com/thread.jspa?threadID=2770076&tstart=0

Just like I did? you post the same link as me :lol:

Sorry, I didn’t get any email notifications about updates in this thread, so haven’t responded until now. I post the script below in it’s current incarnation that seems to be working consistently for files dropped into a folder, but I’m not yet convinced that there isn’t also an issue with folder actions sometimes not getting triggered.

Anyway, the main use I have for the script is converting files added to a folder on my Mac, that others upload to it via a website. At the moment it is only converting the first file added. The upload of the next file starts, but when it is completed the folder action isn’t getting triggered. Sometimes I get the error: : “this item couldn’t be completed because it is owned by _www”. I wonder if this is related to the next file that shows up having an extension of .part, but this shouldn’t trigger the next folder action.

If anyone has any advice as to getting the script to work with uploaded files I would be grateful,

Thanks,

Nick

on adding folder items to this_folder after receiving added_items
	set this_folderPOSIX to POSIX path of this_folder
	set convertPath to "/opt/local/bin/ffmpeg"
	set converted to false
	
	repeat with aFile in added_items
		set t_ext to name extension of (info for aFile without size)
		if t_ext is not missing value then
			if t_ext is in {"m4a", "ogg", "wav", "flac", "aiff", "mp3", "m4p"} then
				set toconvert to true
				
				if t_ext is "mp3" then
					set toconvert to false
					try
						do shell script convertPath & " -i " & (quoted form of POSIX path of aFile) & "  /dev/null 2>&1 | /usr/bin/grep  -m1 bitrate | /usr/bin/sed 's/.*bitrate://'"
						if (word 1 of the result) as integer > 192 then set toconvert to true
					end try
				end if
				
				if toconvert then
					if t_ext is "mp3" then
						set mp3Name to my removeExtension(aFile) & " 1.mp3"
					else
						set mp3Name to my removeExtension(aFile) & ".mp3"
					end if
					
					do shell script convertPath & " -i " & (quoted form of POSIX path of aFile) ¬
						& " -f mp3 -ar 44100 -ab 192k " & quoted form of (this_folderPOSIX & mp3Name) ¬
						& " -map_meta_data " & (quoted form of POSIX path of aFile) ¬
						& ":" & quoted form of (this_folderPOSIX & mp3Name)
					
					tell application "Finder"
						set x to name of aFile
						delete aFile
						if t_ext is "mp3" then set name of file mp3Name of this_folder to x -- rename as original
					end tell
					set converted to true
				end if
				
			else if t_ext is in {"nfo", "txt"} then
				tell application "Finder" to delete aFile
			end if
		end if
	end repeat
	
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

There were issues with the last script I posted in terms of it working with an upload script that adds files to a folder gradually. Jaques kindly posted the following solution on Apple Discussions:

  1. Create a subdirectory with the name “mp3-192kbs” in “this_folder”

  2. A bash script:

#!/bin/bash
 
convertPath="/opt/local/bin/ffmpeg"
currDir=${1%/*}

for f in "$@";do
   var2=$(/usr/bin/stat -f "%z" "$f");var1=0
   while [ $var2 -gt $var1 ];do
     var1=$var2
     /bin/sleep 12
       ## get file size, if var2 is greater than var1 --> downloading ##
     var2=$(/usr/bin/stat -f "%z" "$f") 
   done
       ## if ffmpeg is running by another script, wait until ffmpeg exit ##
   while /bin/ps -Ax| /usr/bin/grep $convertPath | /usr/bin/grep -v grep; do
      /bin/sleep 15
   done
   filename=${f##*/}; base=${filename%.*}; i=""
       ## add number if the mp3 already exists in subfolder ##
   while [ -e "$currDir/mp3-192kbs/$base$i.mp3" ];do
     ((i++))
   done
       ##convert if no error delete original file ##
   $convertPath -i "$f" -f mp3 -ar 44100 -ab 192k -acodec libmp3lame "$currDir/mp3-192kbs/$base$i.mp3" -map_meta_data "$f":"$currDir/mp3-192kbs/$base$i.mp3" && /bin/rm -f "$f"
done

Saved in the folder “/opt/local/bin/” with the name “ConvertToMp3.sh”

  1. enter the following text in terminal:
chmod u+x /opt/local/bin/ConvertToMp3.sh

and press return

  1. The folder action script:
on adding folder items to this_folder after receiving added_items
	set this_folderPOSIX to POSIX path of this_folder
	set convertPath to "/opt/local/bin/ffmpeg"
	set bashScript to quoted form of "/opt/local/bin/ConvertToMp3.sh"
	set validFiles to ""
	
	repeat with aFile in added_items
		try
			set t_ext to name extension of (info for aFile without size)
		on error
			set t_ext to missing value
		end try
		if t_ext is not missing value then
			if t_ext is in {"m4a", "ogg", "wav", "flac", "aiff", "mp3", "m4p"} then
				set tPath to (quoted form of POSIX path of aFile)
				if t_ext is "mp3" then
					try
						do shell script convertPath & " -i " & tPath & " /dev/null 2>&1 | /usr/bin/grep -m1 bitrate | /usr/bin/sed 's/.*bitrate://'"
						if (word 1 of the result) as integer > 192 then
							set validFiles to validFiles & tPath & " "
						end if
					end try
				else
					set validFiles to validFiles & tPath & " "
				end if
			else if t_ext is in {"nfo", "txt"} then
				try
					tell application "Finder" to delete aFile
				end try
			end if
		end if
	end repeat
	-- run shell in background, this Applescript exit without waiting the shell
	if validFiles is not "" then do shell script bashScript & space & validFiles & " > /dev/null 2>&1 &"
end adding folder items to

This appears to be working fine, but depends on the creation of the folder ‘mp3-192kbs’, which won’t work for my purposes. Here is the process that I am trying to set up:

Files are uploaded by a user to a webpage. This webpage creates a folder for each user (using PHP) in the parent directory ‘songs’, based on their login username, and then uploads the audio files to this folder. I would like the folder action to be run on all ‘name’ subfolders in the parent directory. This method of uploading/converting files raises the following issues:

  1. The script above depends on a subfolder called ‘MP3-192kbs’ in any folder it is attached to. I am wondering if there is a way that the script could be adapted to create (converted) files in the same folder as the script is attached to? This is because the PHP script that generates the playlist depends on the above directory structure.

  2. The ‘name’ folders that are created dynamically have write permissions for the user _www, which is the default Apache user. Because of this a folder action script can’t write any converted files to these folders, as the default user on my Mac, ‘nick’, doesn’t have write permissions. So I presume this means that I need to find a way of running the folder action as the user _www, or perhaps a way of changing the permissions for any folders added to the directory ‘songs’ (with a folder action?)

  3. Because the name folders are created dynamically I need to find a way of either dynamically attaching the folder actions (with a folder action on the ‘songs’ directory?), or amending the above folder action so that it can run on subdirectories too (I am not sure if this can be done with folder actions).

I hope this is clear. I asked another question about the permissions issue on Apple Discussions and it was suggested that I could use launchd, which could possibly be used to run folder actions as _www and dynamically add folder actions, but I’m not sure, and this would mean learning a new language for a website that will be going live on Friday!

If anyone has any thoughts/suggestions on any of the above points I’d be glad to hear them,

Thanks,

Nick