Folder Action Script to launch Compressor via Command Line.

Hello,

I have a project that is bending my mind.

I have a folder action script that currently works pretty well to launch a Compressor Droplet. It is this:



on adding folder items to thisFolder after receiving theItems
	repeat with f in theItems
		-- wait for the item to be all there. Since it can take a few minutes for a file to copy over.
		set Was to 0
		set isNow to 1
		repeat while isNow ≠ Was
			set Was to size of (info for f)
			delay 30 -- longer if getting the item is slow
			set isNow to size of (info for f)
		end repeat
		tell application "Finder"
			open theItems using application file "DVCPROHD1080i.app" of folder "Desktop" of folder "anton" of folder "Users" of startup disk
		end tell
	end repeat -- get next item f in thisFolder
end adding folder items to


The DVCPROHD1080i.app in the script above is the Compressor droplet.

So, that’s nice. The Droplet has some handy things built into it, like how I want the file named, where I want it to save the compressed file and what compression Cluster to use.

The drawback of this process is that Compressor is always brought to the foreground each time the droplet is invoked. In my workflow, I have thousands of files I need to compress, and so this script causes Compressor to come to the foreground every 30 seconds, interrupting the work I am doing. I need this to happen in the background.

So question #1: Can a droplet be invoked in the background? Basically hide the whole process?

I don’t know of a way to do that. So, some folks have advised me to look at the command line version of Compressor. So, this does indeed do the work in the background, but it is more complicated and beyond my talents, at least at this point in my AppleScript knowledge.

So, the Command line for a Compressor job goes like this:

./Compressor -clusterid tcp://127.0.0.1:51737 -batchname myBatch -jobpath /Volumes/SourceLocation/ClipToCompress.mov -settingpath /Users/TheUsersName/Library/Application\ Support/Compressor/TheSettingYouWant/setting -destinationpath /Users/DestinationFolder/theCompressedFileName.mov

So, the hitch in this command is:

The clusterid (in this same tcp://127.0.0.1:51737) will change from computer to computer (perhaps cluster to cluster is more accurate). The port is assigned dynamically.

To get to proper cluster id, you need to do this in the terminal. First navigate to the Compressor command line elements inside Compressor.app/Contents/MacOS and then type ./Compressor -show. The result is something like this:

001-739-711:/Applications/Compressor.app/Contents/MacOS anton$ ./Compressor -show
2007-05-10 10:00:01.131 Compressor[2549] in ShowCluster for tcp://127.0.0.1:52645
2007-05-10 10:00:01.131 Compressor[2549] in ShowCluster created URL
2007-05-10 10:00:01.131 Compressor[2549] in ShowCluster created user client
2007-05-10 10:00:01.131 Compressor[2549] in ShowCluster created got accesskey
2007-05-10 10:00:01.133 Compressor[2549] in ShowCluster connected to user client
2007-05-10 10:00:01.134 Compressor[2549]

There may be more than one cluster, you may have a cluster of multiple computers to speed up the compression.

So, how do I get this folder action script working so that the file that gets dropped onto the folder gets sent to the command line version of Compressor, that the script discovers the clusterid and then compressed with the SAME NAME but to a different folder.

Any ideas?

Thanks.

Admittedly, this isn’t properly tested so there might be a bug in it - but still, maybe this helps… ? I assume that “settingPath”, “batchname” and the destination folder are fixed values here. The most important parts are explained briefly through the comments in the script, but feel free to post any further questions in this thread.

Best regards,
danB


property settingPath : "/Users/TheUsersName/Library/Application Support/Compressor/TheSettingYouWant/setting"
property destinationFolder : "/Users/TheUsersName/DestinationFolder/"
property batchname : "myBatch"

on adding folder items to thisFolder after receiving theItems
	repeat with f in theItems
		-- wait for the item to be all there. Since it can take a few minutes for a file to copy over.
		set Was to 0
		set isNow to 1
		repeat while isNow ≠ Was
			set Was to size of (info for f)
			delay 30 -- longer if getting the item is slow
			set isNow to size of (info for f)
		end repeat
		-- POSIX path is the path to the file in "Unixy" style (/path/to/something)
		set pathToClip to POSIX path of f
		(*
		the cluster id is retrieved using grep, i.e. searching for the correct line by some text. The '-m 1' limits the 
		result to the first line with this text. Awk then prints the 7th (and last) word, which happens to be the cluster id
		*)
		set clusterID to (do shell script "/Applications/Compressor.app/Contents/MacOS/Compressor -show | grep -m 1  'in ShowCluster for tcp' | awk -F ' ' '{print $7}'")
		--execute the command line
		do shell script "/Applications/Compressor.app/Contents/MacOS/Compressor -clusterid " & clusterID & " -batchname " & batchname & " -jobpath '" & pathToClip & "' -settingPath '" & settingPath & "' -destinationPath '" & destinationFolder & name of (info for f) & "'"
	end repeat
end adding folder items to

Model: Intel iMac 20"
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

I will try it. Thanks.

Hello,

So I tried the script, but it did not work. And folder action scripts are frustrating to troubleshoot because if they don’t work, it simply doesn’t work.

Below if my version of the script with the settings filled it. Plus, I also added something to it, since you need to invoke Compressor via a ./Compressor command after moving to the folder. So I added:
do shell script “cd /Applications/Compressor.app/Contents/MacOS/; ./Compress -show | grep -m 1 ‘in ShowCluster for tcp’ | awk -F ’ ’ ‘{print $7}’”

The ; to separate the two terminal commands.



property settingPath : "/Users/anton/Library/Application Support/Compressor/iPodVideo-Copy.setting"
property destinationFolder : "/Users/anton/"
property batchname : "myBatch"

on adding folder items to thisFolder after receiving theItems
	repeat with f in theItems
		-- wait for the item to be all there. Since it can take a few minutes for a file to copy over.
		set Was to 0
		set isNow to 1
		repeat while isNow ≠ Was
			set Was to size of (info for f)
			delay 30 -- longer if getting the item is slow
			set isNow to size of (info for f)
		end repeat
		-- POSIX path is the path to the file in "Unixy" style (/path/to/something)
		set pathToClip to POSIX path of f
		(*
       the cluster id is retrieved using grep, i.e. searching for the correct line by some text. The '-m 1' limits the
       result to the first line with this text. Awk then prints the 7th (and last) word, which happens to be the cluster id
       *)
		set clusterID to (do shell script "cd /Applications/Compressor.app/Contents/MacOS/; ./Compress -show | grep -m 1 'in ShowCluster for tcp' | awk -F ' ' '{print $7}'")
		--execute the command line
		do shell script "cd /Applications/Compressor.app/Contents/MacOS/; ./Compressor -clusterid " & clusterID & " -batchname " & batchname & " -jobpath '" & pathToClip & "' -settingPath '" & settingPath & "' -destinationPath '" & destinationFolder & name of (info for f) & "'"
	end repeat
end adding folder items to


Well, for debugging you could use the following script which will inform you about each and every step it takes. The choose file command will pass a so-called AppleScript Alias (sort of a reference to the chosen file) to the handleSingleFile() handler. This the same kind of information that is passed to a folder action in the “after receiving items” parameter.

I’d suggest to run this script from Script Editor with the Event Protocol open (hit Cmd-3 before running the script). This should give you enough information to see where it hangs.

Best regards,
Daniel


property settingPath : "/Users/anton/Library/Application Support/Compressor/iPodVideo-Copy.setting"
property destinationFolder : "/Users/anton/"
property batchname : "myBatch"

on run
	handleSingleFile(choose file)
end run

on handleSingleFile(f)
	set pathToClip to POSIX path of f

	set clusterID to (do shell script "cd /Applications/Compressor.app/Contents/MacOS/; ./Compress -show | grep -m 1 'in ShowCluster for tcp' | awk -F ' ' '{print $7}'")
	display dialog (("I found the following cluster id: " & clusterID) as text)
	
	set shellscript to "cd /Applications/Compressor.app/Contents/MacOS/; ./Compressor -clusterid " & clusterID & " -batchname " & batchname & " -jobpath '" & pathToClip & "' -settingPath '" & settingPath & "' -destinationPath '" & destinationFolder & name of (info for f) & "'"
	display dialog (("Will execute: " & shellscript) as text)
	
	-- increase the timeout value (300 means 300 seconds) if it is too short
	with timeout of 300 seconds
		set res to do shell script "cd /Applications/Compressor.app/Contents/MacOS/; ./Compressor -clusterid " & clusterID & " -batchname " & batchname & " -jobpath '" & pathToClip & "' -settingPath '" & settingPath & "' -destinationPath '" & destinationFolder & name of (info for f) & "'"
	end timeout
	display dialog (("Shell Command returned: " & res) as text)
end handleSingleFile

Model: Intel iMac 20"
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi,

I know, not working folder action scripts are frustrating,
because you get no feedback at all.
I’m testing folder action scripts by commenting out the handler lines
and simulate the handler variables


-- on adding folder items to thisFolder after receiving theItems
set theItems to choose file with multiple selections allowed
tell application "Finder" to set thisFolder to container of (item 1 of theItems) as alias
(*
	here is the script
*)
-- end adding folder items to

Hi Guys,
I was trying the same thing with the Compressor script using terminal:



property settingPath : "/Users/boyd/Library/Application Support/Compressor/TheSettingYouWant/800 Kbs 640x360.setting"
property destinationFolder : "/Users/boyd/Desktop/COMPRESSED"
property batchname : "myBatch"

on run
	handleSingleFile(choose file)
end run

on handleSingleFile(f)
	set pathToClip to POSIX path of f
	
	set clusterID to (do shell script "cd /Applications/Compressor.app/Contents/MacOS/; ./Compress -show | grep -m 1 'in ShowCluster for tcp' | awk -F ' ' '{print $7}'")
	display dialog (("I found the following cluster id: " & clusterID) as text)
	
	set shellscript to "cd /Applications/Compressor.app/Contents/MacOS/; ./Compressor -clusterid " & clusterID & " -jobpath '" & pathToClip & "' -settingPath '" & settingPath & "' -destinationPath '" & destinationFolder & name of (info for f) & "'"
	display dialog (("Will execute: " & shellscript) as text)
	
	-- increase the timeout value (300 means 300 seconds) if it is too short
	with timeout of 300 seconds
		set res to do shell script "cd /Applications/Compressor.app/Contents/MacOS/; ./Compressor -clusterid " & clusterID & " -batchname " & batchname & " -jobpath '" & pathToClip & "' -settingPath '" & settingPath & "' -destinationPath '" & destinationFolder & name of (info for f) & "'"
	end timeout
	display dialog (("Shell Command returned: " & res) as text)
end handleSingleFile


I got the error:
error “Invalid parameter: myBatch” number 255

I have tried to change this batchname, but not quite sure why it won’t read this property. Do I need to save a batch first?

in a command line string you must quote all paths with quoted form of.
Every space character like ./Application Support/. is treated as a parameter delimiter

Hi Stefan,
Does this mean that there is a path to batchname that I need to put in? Or do I need to denote where to save the batchname? Sorry, I have been using a droplet and applescript and it has worked and then it will hang the next time around, so I am trying to run it via terminal.

Thank you

I just noticed that all crucial paths are already escaped by single quotes, so that’s not the error reason.
Unfortunately I’m not familiar with the command line syntax of Compressor

HI Stefan,
After a bit more research, I kind of get what you mean. However, I still have not gotten the terminal command to run, so I will continue trying.

Thank you