ASOC and arc4random

Hello all,
I would like to use the arc4random number generator in my ASOC code. Can someone post an example of how I can call it from within ASOC? I’m only about 50% of the way to translating from C definitions to ASOC equivalents.

Thank you!

Model: MBP
AppleScript: Xcode 4
Browser: Safari 536.28.10
Operating System: Mac OS X (10.8)

Hello,

You can try

Regards,

Hi,

use a Objective-C class.

In Xcode press ⌘N, choose Objective-C class, press return, type Random, make sure it’s a subclass of NSObject, press return twice

replace the .h file with


#import <Foundation/Foundation.h>

@interface Random : NSObject

+ (NSString *)generatePasswordWithLength:(UInt16 )length;
+ (NSUInteger)randomNumber;

@end


replace the .m file with


#import "Random.h"

@implementation Random

+ (NSString *)generatePasswordWithLength:(UInt16 )length
{
    NSString *alphabet  = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
    NSMutableString *string = [NSMutableString stringWithCapacity:length];
    for (NSUInteger i = 0U; i < length; i++) {
        u_int32_t r = arc4random() % [alphabet length];
        unichar c = [alphabet characterAtIndex:r];
        [string appendFormat:@"%C", c];
    }
    return string;
}

+ (NSUInteger)randomNumber
{
    return (NSUInteger) arc4random();
}

@end


In ASOC write


property Random : class "Random"

-- .

set generatedPassword to Random's generatePasswordWithLength_(10) as text
log generatedPassword
set randomNumber to Random's randomNumber() as integer
log randomNumber

the generatePasswordWithLength method is a simple alphanumerical password generator