Can't run AppleScript from Hazel

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

Moderator: Mr_Noodle

Can't run AppleScript from Hazel Mon May 20, 2013 12:29 pm • by mediteran
Hi,

I've got an AppleScript which is working fine from the AppleScript Editor. Now, I tried to get the script running out of Hazel. For this, I added the handler
"on hazelProcessFile(theFile)" at the top of my script
and "end hazelProcessFile" at the end.

When compiling or saving the scxript, I'm getting the error "Expected “end” but found “on”."

The same happens when I try to embed the script in Hazel (of course, without the handlers in the code).

I'm getting the error on this line of my script:
on renameFiles(finalMatchesArray, fileNameAddition)

The script can be downloaded here: http://rivab2b.com/hazel/renameFiles.scpt.zip

Does anybody have an idea what is bothering?

Thanks & regards
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

Re: Can't run AppleScript from Hazel Mon May 20, 2013 1:06 pm • by a_freyer
This is because you have other subroutines in this script. Move the:

Code: Select all
 --Hazel END
end hazelProcessFile


Before the:

Code: Select all
--Umbenennen der Biddateien. Argumente: Liste mit Namen von Bilddateien, Namenszusatz


Comment, and you should be golden.

I also anticipate you're going to want to change the CSV file which is hardcoded to the variable theFile
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Can't run AppleScript from Hazel Mon May 20, 2013 1:40 pm • by mediteran
Hi a_freyer,

thanks for your quick answer. I changed the position of end hazelProcessFile and don't get the error anymore.

But the script doesn't work from Hazel. I'm not sure if I understand this right:
I also anticipate you're going to want to change the CSV file which is hardcoded to the variable theFile


Does this mean that I should change this:
on hazelProcessFile(theFile)
into:
on hazelProcessFile("Ralf System:Users:rhobirk:Desktop:hr_coast.csv")

I did this, but with no effect.
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

Re: Can't run AppleScript from Hazel Mon May 20, 2013 1:44 pm • by a_freyer
I'll take another look, however, I'm guessing that you want hazel to take CSV files to process with this workflow, yes?

You'll need to change Ralf System:Users:rhobirk:Desktop:hr_coast.csv to theFile
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Can't run AppleScript from Hazel Mon May 20, 2013 1:46 pm • by a_freyer
Update - my comment above works for me. Are you sure that you uncommented the on hazelProcessFile(theFile) and the end hazelProcessFile
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Can't run AppleScript from Hazel Mon May 20, 2013 2:10 pm • by mediteran
Yes, they are uncommented. And I returned it at top to (theFile).
It doesn't work, Growl is giving an error notice "Error executing AppleScript…".

The CSV file is needed by the AppleScript only to rename the files.
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

Re: Can't run AppleScript from Hazel Mon May 20, 2013 2:17 pm • by Mr_Noodle
Check the logs for the error and post it here.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Can't run AppleScript from Hazel Mon May 20, 2013 2:48 pm • by mediteran
Here's the log from my latest trial:

Code: Select all
2013-05-20 20:14:33.864 hazelworker[76285] Processing folder RenameTest
2013-05-20 20:14:35.925 hazelworker[76285] baska_007.jpg: Rule Rename matched.
2013-05-20 20:14:36.597 hazelworker[76285] [Error] AppleScript failed: Error executing AppleScript /Users/rhobirk/Desktop/renameFiles.scpt on file /Users/rhobirk/Desktop/RenameTest/baska_007.jpg.
2013-05-20 20:14:36.597 hazelworker[76285] AppleScript error: {
    NSLocalizedDescription = "The variable sourceFolderPath is not defined.";
    NSLocalizedFailureReason = "The variable sourceFolderPath is not defined.";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: 'null'()>";
    OSAScriptErrorBriefMessageKey = "The variable sourceFolderPath is not defined.";
    OSAScriptErrorMessageKey = "The variable sourceFolderPath is not defined.";
    OSAScriptErrorNumberKey = "-2753";
    OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'utxt'(\"sourceFolderPath\")>";
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
2013-05-20 20:14:36.600 hazelworker[76285] Done processing folder RenameTest
 


Short explanation:
In my rule, I'm first running a rename which changes everything to lowercase and replaces spaces with underscores. After this, the AppleScript should act.
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

Re: Can't run AppleScript from Hazel Mon May 20, 2013 3:03 pm • by Mr_Noodle
I think it's having a problem with declaring the sourceFolderPath variable global. Also, I see other handlers in here which means you probably can't run this as an embedded script.

Looking at the commented out "--end hazelProcessFile" line, it seems like you were trying to put the whole script into a handler, which won't work since you have other handlers. Try putting that "end" line before the "on renameFiles(finalMatchesArray, fileNameAddition)" instead.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Can't run AppleScript from Hazel Mon May 20, 2013 3:24 pm • by mediteran
I think it's having a problem with declaring the sourceFolderPath variable global.

What can I do on the sourceFolderPath variable? Honestly, my AppleScript knowledge is next to zero. A friend wrote this script according to my demands, and it's doing its job from the AppleScript Editor properly.

you probably can't run this as an embedded script.

I don't intend to run it as an embedded script, but as an external one.

Try putting that "end" line before the "on renameFiles(finalMatchesArray, fileNameAddition)" instead.

I did this already before, as told by a_freyer.

I uploaded the updated version of the script: http://rivab2b.com/hazel/renameFiles.scpt.zip
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

Re: Can't run AppleScript from Hazel Mon May 20, 2013 3:29 pm • by a_freyer
Move the global definition outside of hazelProcess file:


Code: Select all
--Hazel START

global sourceFolderPath

on hazelProcessFile(theFile)
   
   --Setzen der Variable als Global, damit sie im Handler verwendet werden kann
....



That should address your problem.

But I am curious - what is this trying to do? It appears to just rename a series of files ...
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Can't run AppleScript from Hazel Mon May 20, 2013 3:37 pm • by mediteran
Perfect!
It's working like it should do. You saved my day.
Many thanks!
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm


Return to Support