Help running a script with Hazel

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

Moderator: Mr_Noodle

Re: Help running a script with Hazel Sat Jun 06, 2009 8:32 am • by FOOOD
Yea, I did :lol:

But I'm going to learn how to use it properly from scratch. It's so useful.
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Sat Jun 06, 2009 2:24 pm • by alastor933
Here you go:
Code: Select all
tell application "System Events"
   
   -- first get Finder selection
   -- we always need that
   tell application "Finder"
      set Sel to selection -- a list of references - always!
      set pSel to {} -- list to take POSIX strings
      repeat with anItem in Sel
         set anItem to anItem as alias as string -- coerce reference to alias, then string
         set anItem to quoted form of POSIX path of anItem & space -- change to POSIX notation
         set pSel to pSel & anItem -- add to list
      end repeat
   end tell
   set pSel to pSel as string
   
   -- now to find out what they want...
   set TheChoice to (display dialog "Choose an Action" buttons {"Cancel", "Option 1", "Option 2"} cancel button "Cancel")
   
   --... and do it
   if button returned of TheChoice is "Option 1" then
      set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option1.workflow"
      my doshell(pSel, workflowpath) -- do this
   else if button returned of TheChoice is "Option 2" then
      set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option2.workflow"
      my doshell(pSel, workflowpath) -- do that
   end if
end tell

-- and a short subroutine to save even more lines
-- makes main script more legible
on doshell(theFiles, workflowpath)
   set qtdworkflowpath to quoted form of POSIX path of workflowpath
   set theCommand to "/usr/bin/automator -i " & theFiles & qtdworkflowpath
   set output to do shell script theCommand
end doshell

Let us know how it goes.
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Re: Help running a script with Hazel Sat Jun 06, 2009 4:27 pm • by FOOOD
Thanks a lot. This works fine if I run it from Script Editor, but Hazel won't accept it. If I embed the code in Hazel it shows an error message saying:

'At least one of the actions has an empty field or an error. Please fill in the field, correct the error or remove the action.'

It highlights on from the following as the problem.

-- and a short subroutine to save even more lines
-- makes main script more legible
on doshell(theFiles, workflowpath)


It won't work from Hazel if I link to the script file rather than embedding it either :|

What I'm hoping to happen is that Hazel is watching my Downloads folder for certain file types. If it see one of these file types added to the folder it should run the script which will ask me if I want to open the file (Automator workflow option 1), or move the file to a different computer (Automator workflow option 2).

(Sorry for all this hassle!)
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Sun Jun 07, 2009 4:42 am • by alastor933
Oh dear.

I'm guessing Hazel does not like subroutines - my own Hazel scripts don't use them, so I really don't know.

Try this:
- copy the code of the subroutine, that's the lines between "on doshell.." end "end doshell".
- paste into the script to replace the "my doshell..." lines.
- delete the entire subroutine.

You've now brought the subroutine code 'inline'.
Let's see what happens.
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Re: Help running a script with Hazel Sun Jun 07, 2009 9:40 am • by FOOOD
I tried this:
Code: Select all
tell application "System Events"
   
   -- first get Finder selection
   -- we always need that
   tell application "Finder"
      set Sel to selection -- a list of references - always!
      set pSel to {} -- list to take POSIX strings
      repeat with anItem in Sel
         set anItem to anItem as alias as string -- coerce reference to alias, then string
         set anItem to quoted form of POSIX path of anItem & space -- change to POSIX notation
         set pSel to pSel & anItem -- add to list
      end repeat
   end tell
   set pSel to pSel as string
   
   -- now to find out what they want...
   set TheChoice to (display dialog "Choose an Action" buttons {"Cancel", "Option 1", "Option 2"} cancel button "Cancel")
   
   --... and do it
   if button returned of TheChoice is "Option 1" then
      set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option1.workflow"
on doshell(theFiles, workflowpath)
   set qtdworkflowpath to quoted form of POSIX path of workflowpath
   set theCommand to "/usr/bin/automator -i " & theFiles & qtdworkflowpath
   set output to do shell script theCommand
end doshell
   
   else if button returned of TheChoice is "Option 2" then
      set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option2.workflow"
on doshell(theFiles, workflowpath)
   set qtdworkflowpath to quoted form of POSIX path of workflowpath
   set theCommand to "/usr/bin/automator -i " & theFiles & qtdworkflowpath
   set output to do shell script theCommand
end doshell
   end if
end tell


But Hazel and Script Editor both won't accept them I'm afraid :(
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Sun Jun 07, 2009 3:59 pm • by alastor933
I said:
- copy the code of the subroutine, that's the lines between "on doshell.." end "end doshell".

That's not what you did...
Or is it my understanding of English?
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Re: Help running a script with Hazel Sun Jun 07, 2009 4:24 pm • by FOOOD
Your English is perfect :D
I didn't realise you meant just the code between those parts (thought you meant use all of it).

Do you mean like this?
Code: Select all
tell application "System Events"
   
   -- first get Finder selection
   -- we always need that
   tell application "Finder"
      set Sel to selection -- a list of references - always!
      set pSel to {} -- list to take POSIX strings
      repeat with anItem in Sel
         set anItem to anItem as alias as string -- coerce reference to alias, then string
         set anItem to quoted form of POSIX path of anItem & space -- change to POSIX notation
         set pSel to pSel & anItem -- add to list
      end repeat
   end tell
   set pSel to pSel as string
   
   -- now to find out what they want...
   set TheChoice to (display dialog "Choose an Action" buttons {"Cancel", "Option 1", "Option 2"} cancel button "Cancel")
   
   --... and do it
   if button returned of TheChoice is "Option 1" then
      set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option1.workflow"
      set qtdworkflowpath to quoted form of POSIX path of workflowpath
      set theCommand to "/usr/bin/automator -i " & theFiles & qtdworkflowpath
      set output to do shell script theCommand
   else if button returned of TheChoice is "Option 2" then
      set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option2.workflow"
      set qtdworkflowpath to quoted form of POSIX path of workflowpath
      set theCommand to "/usr/bin/automator -i " & theFiles & qtdworkflowpath
      set output to do shell script theCommand
   end if
end tell

Hazel accepts this, and shows the window when a appropriate file type is added, but then displays an error message in Growl after clicking on one of the buttons saying 'Error executing AppleScript'.
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Mon Jun 08, 2009 5:46 am • by alastor933
Yes, that's what I meant.
Hmm.. I'm close to being stuck!

The script does not actually return something to Hazel, it seems.
So you could change this
Code: Select all
      set output to do shell script theCommand
to this
Code: Select all
      do shell script theCommand

But I really don't think that will solve it.
In that case it would return to being a Hazel problem.
In the mean time you could take a peek at Hazel's log, see if anything relevant shows up (button in Hazel's info panel).

Edit:
I'm also not sure about doing dialogs in a Hazel script. From your description I gather that the error occurs AFTER the dialog closes. Right?
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Re: Help running a script with Hazel Tue Jun 09, 2009 3:29 am • by Mr_Noodle
alastor933: Many thanks for helping out here. I apologize that I haven't been more helpful but things have been hectic here at WWDC. Among other things, my baggage took an extra trip to JFK before coming back here today.

But yes, digging up the error in the logs will help pinpoint the problem. Also, you already fixed it, but if you are doing an embedded script, you can't add extra handlers.

I'll try and check back in during the week but please excuse my relative absence during this time.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Help running a script with Hazel Tue Jun 09, 2009 6:00 am • by FOOOD
Oh well, not to worry. Thanks again for all the help so far! :)


alastor933 wrote:I'm also not sure about doing dialogs in a Hazel script. From your description I gather that the error occurs AFTER the dialog closes. Right?

Yes, that's correct


Mr_Noodle wrote:I'll try and check back in during the week but please excuse my relative absence during this time.

No problem at all, there's no hurry :)
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Previous

Return to Support

cron