Help running a script with Hazel

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

Moderator: Mr_Noodle

Help running a script with Hazel Sun May 17, 2009 9:37 am • by FOOOD
I have this script from a friend. When run on a selected torrent file it pops up a window asking if I want to open the file (on this computer), or if I want to move it to a different computer to be opened there (once an option has been selected it runs 1 of 2 different Automator workflows saved as applications). It works fine if I select the torrent file in my Downloads folder and run the script from Script Editor, but I can't get it to work automatically through Hazel. Here's the script

Code: Select all
set TheChoice to (display dialog "Choose an Action" buttons {"Cancel", "Open Torrent File", "Move File to Power Mac"} cancel button "Cancel")
if button returned of TheChoice is "Open Torrent File" then
   
   tell application "Finder" to open the selection using "Macintosh HD:Users:Jono:Documents:Open the Torrent file.app"
   
else
   if button returned of TheChoice is "Move File to Power Mac" then
      
      tell application "Finder" to open the selection using "Macintosh HD:Users:Jono:Documents:Send Torrent to Power Mac.app"
      
   end if
end if


I tried changing 'to open the selection using' to 'to open theFile using' but it didn't work (I don't really know AppleScript).

Is there any way I can get this working? :)
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Mon May 18, 2009 2:09 pm • by Mr_Noodle
Doing "open theFile using ..." should work in general. I suggest checking the logs ("Info" tab->"View Log") and seeing if there's an error executing the script.

Also, does the dialog show up at all? At what point does it fail?
Mr_Noodle
Site Admin
 
Posts: 11240
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Help running a script with Hazel Thu May 28, 2009 8:49 am • by FOOOD
Sorry for the delay getting back to you on this (My wife went & had a baby :D )

I've tried again but still no luck. Here is the script I'm using to test whether it will work or not from Hazel
Code: Select all
set TheChoice to (display dialog "Choose an Action" buttons {"Cancel", "Option 1", "Option 2"} cancel button "Cancel")
if button returned of TheChoice is "Option 1" then
   
   tell application "Finder" to open theFile using "Macintosh HD:Users:Jono:Documents:Automator Files:Automator Apps:Option1.app"
   
else
   if button returned of TheChoice is "Option 2" then
      
      tell application "Finder" to open theFile using "Macintosh HD:Users:Jono:Documents:Automator Files:Automator Apps:Option2.app"
      
   end if
end if


Here's the info from View Log/Console

Code: Select all
2009-05-28 13:44:39.214 (null)[0] torrent test file.torrent: Rule Open or move torrent files? matched.
2009-05-28 13:44:39.300 hazelfolderwatch[4428] [Error] AppleScript failed: Error executing AppleScript /Users/Jono/Documents/Automator Files/Scripts/Test Scripts & Examples/Select Option Window (Hazel with .apps).scpt on file /Users/Jono/Downloads/torrent test file.torrent.
2009-05-28 13:44:39.300 hazelfolderwatch[4428] AppleScript error: {
    OSAScriptErrorNumber = -1708;


All that happens when the rule runs is I see a growl message saying the AppleScript failed
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Fri May 29, 2009 5:49 pm • by Mr_Noodle
I should've seen this earlier. Sorry it took me so long to notice.

You need to bracket your script with:
Code: Select all
tell application "System Events"
..
end tell


Hazel runs AppleScript in a non-GUI context so you need to tell a GUI app (System Events being a good one) to display the dialog.

There may be other problems with the script but this should at least get you past the first barrier.

Oh, and congrats on the baby.
Mr_Noodle
Site Admin
 
Posts: 11240
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Help running a script with Hazel Mon Jun 01, 2009 6:24 pm • by FOOOD
Thanks so much on this, it now works :)

One last thing, I have a similar thing that uses Automator .workflow files rather than Automator workflows saved as apps
Code: Select all
set TheChoice to (display dialog "Choose an Action" buttons {"Cancel", "Option 1", "Option 2"} cancel button "Cancel")
if button returned of TheChoice is "Option 1" then
   
   tell application "Finder" to set Sel to (selection) as alias list
   repeat with i from 1 to number of items in Sel
      set item i of Sel to (quoted form of (POSIX path of item i of Sel)) & space
      
   end repeat
   
   set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option1.workflow"
   set qtdworkflowpath to quoted form of (POSIX path of workflowpath)
   set command to "/usr/bin/automator -i " & (items of Sel as string) & qtdworkflowpath
   set output to do shell script command
   
else
   if button returned of TheChoice is "Option 2" then
      
      tell application "Finder" to set Sel to (selection) as alias list
      repeat with i from 1 to number of items in Sel
         set item i of Sel to (quoted form of (POSIX path of item i of Sel)) & space
         
      end repeat
      
      set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option2.workflow"
      set qtdworkflowpath to quoted form of (POSIX path of workflowpath)
      set command to "/usr/bin/automator -i " & (items of Sel as string) & qtdworkflowpath
      set output to do shell script command
      
   end if
end if


If I add the brackets
Code: Select all
tell application "System Events"
..
end tell

Hazel won't accept it. It gives the following message

Image

It won't save as a scrip in Script Editor either. Is there anything that can be done to get this to work with Hazel?

Thanks again, and thanks a lot for the congrats :D
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Mon Jun 01, 2009 6:49 pm • by Mr_Noodle
That error should be better. There's a compile error in the script. If you click the hammer button in the lower left, it will highlight the error and show you a cryptic error message.

In your case it appears you can't use "command" as a variable name. Replace it with "aCommand" or whatever. Make sure to find all cases of it (I count 4).

Whether the script works is another thing but at least you can test it now.
Mr_Noodle
Site Admin
 
Posts: 11240
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Help running a script with Hazel Tue Jun 02, 2009 6:58 am • by FOOOD
Almost there I think :)

Here's the script above with 'tell application "System Events"' etc. added, and command changed to aCommand
Code: Select all
tell application "System Events"
   set TheChoice to (display dialog "Choose an Action" buttons {"Cancel", "Option 1", "Option 2"} cancel button "Cancel")
   if button returned of TheChoice is "Option 1" then
      
      tell application "Finder" to set Sel to (selection) as alias list
      repeat with i from 1 to number of items in Sel
         set item i of Sel to (quoted form of (POSIX path of item i of Sel)) & space
         
      end repeat
      
      set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option1.workflow"
      set qtdworkflowpath to quoted form of (POSIX path of workflowpath)
      set aCommand to "/usr/bin/automator -i " & (items of Sel as string) & qtdworkflowpath
      set output to do shell script aCommand
      
   else
      if button returned of TheChoice is "Option 2" then
         
         tell application "Finder" to set Sel to (selection) as alias list
         repeat with i from 1 to number of items in Sel
            set item i of Sel to (quoted form of (POSIX path of item i of Sel)) & space
            
         end repeat
         
         set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option2.workflow"
         set qtdworkflowpath to quoted form of (POSIX path of workflowpath)
         set aCommand to "/usr/bin/automator -i " & (items of Sel as string) & qtdworkflowpath
         set output to do shell script aCommand
         
      end if
   end if
end tell


It compiles fine in Hazel and Script Editor, but if I run it in Hazel nothing happens. If I run it in Script Editor I get the following error message
Image

And see this in the even log

Code: Select all
tell application "System Events"
   display dialog "Choose an Action" buttons {"Cancel", "Option 1", "Option 2"} cancel button "Cancel"
      {button returned:"Option 1"}
end tell
tell application "Finder"
   get selection
      {alias "Macintosh HD:Users:Jono:Downloads:torrent test file.torrent"}
end tell
tell application "System Events"
   get quoted form of POSIX path of alias "Macintosh HD:Users:Jono:Downloads:torrent test file.torrent"
      "System Events got an error: Can’t make quoted form of POSIX path of alias \"Macintosh HD:Users:Jono:Downloads:torrent test file.torrent\" into type reference."
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Thu Jun 04, 2009 2:04 pm • by Mr_Noodle
I think if it's failing in Script Editor, then the problem is a bit outside Hazel support. You might want to do a script isolating the "get quoted form of POSIX path of alias" line and play with that. Unfortunately, I'm a bit busy now so I can't provide much more than that at the moment but maybe another lurking AppleScript person around here can help.
Mr_Noodle
Site Admin
 
Posts: 11240
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Help running a script with Hazel Fri Jun 05, 2009 3:00 am • by alastor933
Hi Jono,

Code: Select all
"System Events got an error: Can’t make quoted form of POSIX path of alias \"Macintosh HD:Users:Jono:Downloads:torrent test file.torrent\" into type reference."

Um, didn't do any testing, but looking at the dictionaries where a "POSIX" term is present it's my guess (bordering on certainty ; ) that you need to use a string, not an alias.
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Re: Help running a script with Hazel Fri Jun 05, 2009 3:40 am • by FOOOD
Mr_Noodle wrote:I think if it's failing in Script Editor, then the problem is a bit outside Hazel support. You might want to do a script isolating the "get quoted form of POSIX path of alias" line and play with that. Unfortunately, I'm a bit busy now so I can't provide much more than that at the moment but maybe another lurking AppleScript person around here can help.

OK, and thanks a lot for the help you've given me. I really appreciate it :)


alastor933 wrote:Hi Jono,
Um, didn't do any testing, but looking at the dictionaries where a "POSIX" term is present it's my guess (bordering on certainty ; ) that you need to use a string, not an alias.

Thanks a lot, but excuse my ignorance. Do you mean change:

tell application "Finder" to set Sel to (selection) as alias list
to
tell application "Finder" to set Sel to (selection) as string list


If I do this and try to save it in Script Editor I get the following error message:

'Expected end of line, etc. but found class name.'
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Fri Jun 05, 2009 9:18 am • by alastor933
Sorry for the brevity.

I played briefly with "selection":
- "selection as alias list" returns references, not aliases.
- same statement throws an error when selection is just one item.
- without "as alias list" it works with one item.

This should work:
Code: Select all
 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
      set anItem to quoted form of POSIX path of anItem & space
      set pSel to pSel & anItem
end repeat
end tell
set pSel to pSel as string

Notes:
- the repeat statement is more compact this way
- the first statement converts a reference to a string
- the second statement turns it into something you can use in "do shell script"
- the 3rd line adds that POSIX path to a list
- at the end the list is coerced to a string

You can now use string "pSel" in the "do shell" command.

To see what happens you can add "log varname" after a statement to see the value of varname in the event log.
This is a great help in debugging.
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Re: Help running a script with Hazel Fri Jun 05, 2009 11:12 am • by FOOOD
OK so I added the code, but I could have placed it wrong or something as it still doesn't work when run (it does compile and save OK though).
Here's my code now
Code: Select all
tell application "System Events"
   set TheChoice to (display dialog "Choose an Action" buttons {"Cancel", "Option 1", "Option 2"} cancel button "Cancel")
   if button returned of TheChoice is "Option 1" then
      
      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
            set anItem to quoted form of POSIX path of anItem & space
            set pSel to pSel & anItem
         end repeat
      end tell
      set pSel to pSel as string
      
      set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option1.workflow"
      set qtdworkflowpath to quoted form of (POSIX path of workflowpath)
      set aCommand to "/usr/bin/automator -i " & (items of Sel as string) & qtdworkflowpath
      set output to do shell script aCommand
      
   else
      if button returned of TheChoice is "Option 2" then
         
         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
               set anItem to quoted form of POSIX path of anItem & space
               set pSel to pSel & anItem
            end repeat
         end tell
         set pSel to pSel as string
         
         set workflowpath to "Macintosh HD:Users:Jono:Documents:Automator Files:Option2.workflow"
         set qtdworkflowpath to quoted form of (POSIX path of workflowpath)
         set aCommand to "/usr/bin/automator -i " & (items of Sel as string) & qtdworkflowpath
         set output to do shell script aCommand
         
      end if
   end if
end tell


Thanks a lot for all the help, much appreciated!
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Fri Jun 05, 2009 2:54 pm • by alastor933
No, it does not work.
You can now use string "pSel" in the "do shell" command.
You omitted the last step.
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Re: Help running a script with Hazel Fri Jun 05, 2009 5:56 pm • by FOOOD
Sorry but I don't know what you mean (I don't know any AppleScript).
FOOOD
 
Posts: 49
Joined: Thu May 31, 2007 4:29 pm
Location: UK

Re: Help running a script with Hazel Sat Jun 06, 2009 7:24 am • by alastor933
Ah, you really meant that. Thought you had tinkered a bit already...

I'll post the entire script later on (don't hold yer breath).
The logic can be improved upon, too: first get Finder selection, then figure out what to do with it.
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Next

Return to Support

cron