Random File Name Generation with Applescript

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

Moderator: Mr_Noodle

Random File Name Generation with Applescript Mon Nov 05, 2012 10:34 am • by kvl
Hi All.

New to hazel but really enjoying it so far.

This is what I'm trying to do:

Run and embedded applescript that generate a random string so I can pass it as a token and use to rename files.

The applescript code I have is as follows:

Code: Select all
set str to {}
 set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 repeat 12 times
  set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()_+-=[]>?;:'"
 end repeat
 return (str as string)


This code works as I use it in TextExpander all of the time. Returns a 12 character random string. I'm having issues working this into Hazel. I don't have any AppleScript Experience.

This is what I have but I know there is an issue with the end portion where it passes the token:

Code: Select all
set str to {}
 set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 repeat 12 times
  set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()_+-=[]>?;:'"
 end repeat
 return (str as string)
return {hazelExportTokens:{randomname:str}}


Can anyone help?
kvl
 
Posts: 3
Joined: Mon Nov 05, 2012 10:28 am

You have two return statements. The first one is incorrect and the second one won't get executed. Just delete the first one and see if that works. Also, remember in the UI to specify the exported token.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Random File Name Generation with Applescript Mon Nov 05, 2012 2:55 pm • by kvl
Hi Mr. Noodle

I tried the follow already:

Code: Select all
set str to {}
 set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 repeat 12 times
  set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()_+-=[]>?;:'"
 end repeat
return {hazelExportTokens:{randomname:str}}


but it doesn't return anything. The script runs (I'm testing it through a growl notification right now) but nothing is being passed.

I have the exported token 'randomname' specified and am using in the notification but nothing gets returned.
kvl
 
Posts: 3
Joined: Mon Nov 05, 2012 10:28 am

Did you check the logs? See if there's an error there.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Random File Name Generation with Applescript Mon Nov 05, 2012 5:56 pm • by kvl
I checked, no errors

Code: Select all
2012-11-05 16:49:09.040 hazelworker[4523] ViewEStatementDetail.do.pdf: Rule Step 1 matched.
2012-11-05 16:49:09.153 hazelworker[4523] Hazel Alert:
2012-11-05 16:49:09.153 hazelworker[4523] [Custom Message] Hazel Alert:
2012-11-05 16:49:09.176 hazelworker[4523] Done processing folder secret


The token/name should be showing up after the 'Hazel Alert:' but nothing is coming up.

And to note, if I put an actual string like "str" (including quotes) to pass in the token instead of just the variable (str) is passed correctly, gets used and shows up in the alert. So the issue is something to do with the script, the creation of the variable and it being passed into the token.

GOT IT:

Was just playing around and I figured it out:

Code: Select all
set str to {}
set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
repeat 12 times
   set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()_+-=[]>?;:'"
end repeat
return {hazelExportTokens:{randomname:str as string}}


I was missing the 'str as string' portion in the token being passed.

Thanks for your help Mr. Noodle.
kvl
 
Posts: 3
Joined: Mon Nov 05, 2012 10:28 am


Return to Support