Embedded Applescript Error Runs fine outside of Hazel

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

I embedded the following script in a Hazel rule (conditions of which are just label color):
Code: Select all
set myString to do shell script " /bin/ps -ax"
set allWords to (every word of myString)
set wordcount to 0
repeat with i in my allWords
   if i contains "HandBrakeCLI" then set wordcount to wordcount + 1
end repeat

if wordcount < 2 then
   activate application "Batch_Encode_Movies"
else
   error number -128
end if


And it throws up the following error:
Code: Select all
2010-12-01 08:03:07.662 hazelfolderwatch[15472] [Error] AppleScript failed: Error executing AppleScript on file /Volumes/Hackintosh Companion/Movie Process/20.0 New AnyDVDHD Rips/You Don't Mess With The Zohan (2008).
2010-12-01 08:03:08.116 hazelfolderwatch[15472] AppleScript error: {
    NSLocalizedDescription = "allWords of \U00abscript\U00bb doesn\U2019t understand the count message.";
    NSLocalizedFailureReason = "allWords of \U00abscript\U00bb doesn\U2019t understand the count message.";
    OSAScriptErrorAppAddressKey = <NSAppleEventDescriptor: 'psn '("hazelfolderwatch")>;
    OSAScriptErrorAppNameKey = hazelfolderwatch;
    OSAScriptErrorBriefMessageKey = "allWords of \U00abscript\U00bb doesn\U2019t understand the count message.";
    OSAScriptErrorMessageKey = "allWords of \U00abscript\U00bb doesn\U2019t understand the count message.";
    OSAScriptErrorNumberKey = -1708;
    OSAScriptErrorOffendingObjectKey = <NSAppleEventDescriptor: 'obj '{ 'form':'usrp', 'want':'prop', 'seld':'utxt'("allWords"), 'from':'null'() }>;
  OSAScriptErrorRangeKey = NSRange: {0, 0};
}


This script runs fine in applescript editor program.

The script is meant to kick of a batch encode movies workflow i downloaded. The reason for the script is because the encode workflow kicks on a handbrake encode and I dont want more than 2 instances of handbrake running at the same time.

I am a super applescript noob so not sure what could be wrong since it works in applescript editor. Is it the nested shell script?
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

I'm not sure why the AppleScript is bombing but here's a more concise way of doing it:

Code: Select all
set processCount to do shell script "/bin/ps -ax | grep -c HandBrakeCLI"

if processCount = "0" then
   activate application "Batch_Encode_Movies"
else
   error number -128
end if


Not sure if it will work around the problem but it's worth a shot.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:I'm not sure why the AppleScript is bombing but here's a more concise way of doing it:

Code: Select all
set processCount to do shell script "/bin/ps -ax | grep -c HandBrakeCLI"

if processCount = "0" then
   activate application "Batch_Encode_Movies"
else
   error number -128
end if


Not sure if it will work around the problem but it's worth a shot.


Much more eloquent but it seems to work inconsistently. If I run the do shell script with the grep command from applescript, it returns a value of 2 sometimes and other times a value of 1 (both if handbrake isnt running). This causes an inconsistency when i integrate this command into my script. Not sure why it does this....its so odd....i wish i understood this a little better.

Not a big deal at all but im sure as you know, always want to get to 100%
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

I don't know if it'll resolve your problem, but I'd replace the "ps" options with "-acx" to limit its output that's input for "grep" to only include the executable name (HandBrakeCLI) instead of the full command line.
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

Yeah, that would be a good idea. What I overlooked is that when you do a 'ps' with a 'grep' is that it sometimes picks up the grep command.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I sent dhy8386 PM with a longer explanation and example of "ps -c …" usage.
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

Works swimmingly. I did have to edit my script because that command with an -acx will error out if nothing is running and you grep for it. Here is the final applescript I use. Its amateur i am sure but it works. I am using an automator script which takes my blu rays and converts to MKV then uses Handbrake to Encode. There are actually 4 CLI apps that run at any time depending on where the process is at and I never want more than 2 running - just too processor intensive.

Much thanks again to both of you.

Code: Select all
try
   set processcountMKV to do shell script "/bin/ps -acx | grep -c makemkvcon"
on error
   set processcountMKV to 0
end try

try
   set processcountMM to do shell script "/bin/ps -acx | grep -c mkvmerge"
on error
   set processcountMM to 0
end try

try
   set processcountME to do shell script "/bin/ps -acx | grep -c mkvextract"
on error
   set processcountME to 0
end try

try
   set processcountH to do shell script "/bin/ps -acx | grep -c handbrakecli"
on error
   set processcountH to 0
end try

try
   set origFilepath to quoted form of POSIX path of (theFile as alias)
   set shellCommand to "automator -i " & origFilepath & " '/Users/dhy/Library/Services/Batch Rip • Batch Encode (Finder)(Movie).workflow' ;"
   
   delay 5
   
   tell application "Finder"
      if label index of theFile = 6 then
         error number -128
      else if processcountMKV + processcountME + processcountMM + processcountH ≥ "2" then
         error number -128
      else
         do shell script shellCommand
      end if
   end tell
end try
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm


Return to Support