Do Shell Script has me stumped!

I am trying to do a shell script command and it’s driving me crazy. I’ve done a display dialog of the shell script from Applescript to make sure the syntax is right. I have then copied that exact output straight into terminal and the process is completely successful. But when I try to run it from Applescript, it does nothing. I have no idea with the problem is. I would appreciate any insight you can provide. Here it is:


on process_item(this_item)
	set {name:Nm, name extension:Ex} to info for this_item
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	set myFile to Nm & "." & Ex
	with timeout of 36000 seconds -- nine hour per movie time limit
		set thePackageUrl to do shell script "pcl publish --username=me@myemail.com --password=myPass --pandofile=" & myFile & ".pando --sender=\"VMX\" --title=\"" & Nm & "\" " & myFile & ""
	end timeout
	--delay 20
	display dialog thePackageUrl buttons {"OK"} default button 1
end process_item


When I have it display a dialog of what the shell script is, it is this:

pcl publish --usernameoo=me@myemail.com --password=myPass --pandofile=thisfile.mp4.pando --sender=“VMX” --title=“thisfile” thisfile.mp4

And when I run that directly in the shell, it works perfectly. What is the issue?

Thanks,
sw

Try including the complete path to pcl (which I don’t have on my machine, apparently). Your shell may not be the same as AppleScript’s.

Excellent point…I did that. But it still doesn’t work. I just don’t understand this. I put in the real path:

/Applications/Utilities/pcl/pcl

And then did another display dialog and copied that directly into shell without a problem. Works great. Try to run the Applescript and nothing happens. My script now looks like this:



on process_item(this_item)
   set {name:Nm, name extension:Ex} to info for this_item
   if Ex is missing value then set Ex to ""
   if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
   set myFile to Nm & "." & Ex
   with timeout of 36000 seconds -- nine hour per movie time limit
       set thePackageUrl to do shell script "/Applications/Utilities/pcl/pcl publish --username=me@myemail.com --password=myPass --pandofile=" & myFile & ".pando --sender=\"VMX\" --title=\"" & Nm & "\" " & myFile & ""
   end timeout
   --delay 20
   display dialog thePackageUrl buttons {"OK"} default button 1
end process_item

Trying to do a little more debugging here. So I copied the shell script part to a new script so that I could run it directly and see errors. So here is the script I created:


set thePackageUrl to do shell script "/Applications/Utilities/pcl/pcl publish --username=myuser --password=mypass --pandofile=photo12.jpg.pando --sender=\"VMX\" --title=\"VMX\" Users/vtaccess/test/Photo12.jpg"
--end timeout
--delay 20
display dialog thePackageUrl buttons {"OK"} default button 1

And I get this error:

“Upload failed: PD_ERR_NO_TOKEN”

I’m confused because again, this exact command works fine in terminal. Watching top, it is trying to start pcl. So does this mean it’s something with the application?

Can anyone tell me what that means?

Much appreciated.

Given the context, I assume that pcl is HP’s printer control language for producing a postscript file given an input file. Since you’re giving usernames and passwords in your script, perhaps you need to add to your do shell script “bunchOfStuff” with administrator privileges. That’s a stab in the dark. AppleScript uses the bash shell, by the way.

OK. I got it working on its own applescript outside the realm of my folder action. It was an issue with the application. I figured it out in terms of the flags. So now I got back to my regular script and plug it in exactly. Watching top, I can see that pcl isn’t even getting called. So I now must also have an issue with my script in general. I don’t understand because this is the same folder action script I use for other things, and the only thing I’m changing is the shell call. So I can’t figure out what’s happening to keep the pcl application from even getting called. Any ideas? Here is the full script:


property type_list : {"MooV"} -- the list of file types which will be processed
-- since file types are optional in Mac OS X,
-- check the name extension if there is no file type
-- NOTE: do not use periods (.) with the items in the name extensions list
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"}
property extension_list : {"mov", "mpg", "mpeg", "avi", "wmv", "mp4", "mpeg", "flv"}
property example_property_A : "On"
property example_property_B : "On"

on adding folder items to this_folder after receiving these_items
	-- insert this command after the cpu-eating command
	checkStableSize(these_items)
	
	repeat with i from 1 to the count of these_items
		
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) and ¬
			((the file type of the item_info is in the type_list) or ¬
				the name extension of the item_info is in the extension_list) then
			process_item(this_item)
		end if
	end repeat
end adding folder items to

-- this sub-routine processes folders
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) and ¬
			((the file type of the item_info is in the type_list) or ¬
				the name extension of the item_info is in the extension_list) then
			process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine processes files
on process_item(this_item)
	set {name:Nm, name extension:Ex} to info for this_item
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	set myFile to Nm & "." & Ex
	with timeout of 36000 seconds -- nine hour per movie time limit
		set thePackageUrl to do shell script "/Applications/Utilties/pcl/pcl publish --username=me --password=mypass --pandofile=" & myFile & ".pando --sender=\"VMX\" --title=\"" & Nm & "\" " & myFile & ""
	end timeout
	display dialog thePackageUrl buttons {"OK"} default button 1
end process_item

-- Handler to wait until a file is fully loaded.
on checkStableSize(theItem)
	set F to quoted form of POSIX path of theItem
	set sizeThen to first word of (do shell script "du -d 0 " & F) as integer --coercing to integer fixes the quote problem
	repeat
		try
			delay 1 -- seconds (set to longer if needed)
			set sizeNow to first word of (do shell script "du -d 0 " & F) as integer --coercing to integer fixes the quote problem
			if sizeNow - sizeThen = 0 then exit repeat
			set sizeThen to sizeNow
		on error
			exit repeat
		end try
	end repeat
end checkStableSize

Try posting dialogs from each of your handlers to see whether you are getting to them. Also, since you’re calling your handlers from within a handler, “my” in front of them is a good idea. Finally, after resaving your altered folder action, did you check that it was still properly attached?

Alright, now I think I’m going insane. I REALLY don’t understand. To debug, I’ve been setting up display dialogs everywhere. So I wanted to make sure that it was accessing the pcl application. And if so, then I wanted to send it a command without sufficient flags to see if it would bring up the error properly (thus still accessing the pcl app).So here’s what I did:


on process_item(this_item)
	set thePackageUrl to do shell script "/Applications/Utilities/pcl/pcl version"
	display dialog "hi" default answer thePackageUrl buttons {"OK"} default button 1
	
set thisPackage to do shell script "/Applications/Utilities/pcl/pcl publish"
	display dialog "hi" default answer thisPackage buttons {"OK"} default button 1
end process_item

It runs the first command and displays it properly so I know it is running the app. But it doesn’t run the second command.
But then, when I run the second command in other applescripts and in shell, it returns “No files specified”. But here no dialog will come up. Why??? And why does it work in other scripts???

Finally I’ve solved something myself. Another hour of smashing my head against the computer and I finally got it, but I still don’t understand why. So in the folder action script, I had to use the destinationFile path. The reason I didn’t htink of this was because for the other scripts I was running it wasn’t erroring out on that. Ugh. Another lesson learned in the newbie life of applescript.


on process_item(this_item)
   set {name:Nm, name extension:Ex} to info for this_item
   if Ex is missing value then set Ex to ""
   if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
   set myFile to Nm & "." & Ex
  set destinationFile to the POSIX path of (this_item)
   with timeout of 36000 seconds -- nine hour per movie time limit
       set thePackageUrl to do shell script "/Applications/Utilties/pcl/pcl publish --username=me --password=mypass --pandofile=" & myFile & ".pando --sender=\"VMX\" --title=\"" & Nm & "\" " & destinationFile& ""
   end timeout
   display dialog thePackageUrl buttons {"OK"} default button 1
end process_item

I think folder actions aren’t the best at handling errors. It looks like your problem was that you weren’t passing the full path to the pcl application.

Also, I would add quoted form to your script to handle spaces or other characters that could cause problems in the shell:

set thePackageUrl to do shell script "/Applications/Utilties/pcl/pcl publish --username=me --password=mypass --pandofile=" & myFile & ".pando --sender=\"VMX\" --title=\"" & Nm & "\" " & quoted form of destinationFile