Applescript: How do I create custom data types?

Discussion in 'Mac Programming' started by moonman239, Jan 5, 2013.

  1. moonman239, Jan 5, 2013
    Last edited: Jan 5, 2013

    moonman239 macrumors 68000

    Mar 27, 2009
    Maybe the title was badly worded, but you'll understand what I mean after I explain what I want to do.

    I want to make a script that creates multiple random, unique ID's. To accomplish this, I could do something like this:

    on randomID()
    --ID generation code
    end randomID
    set theid to randomID()
    but I thought I could do something like this:

    to randomID
    --ID generation code
    end randomID
    set theID to randomID
    Notice the lack of parentheses after "set theID to".

    Somebody on a different forum was able to construct a method that would return a string with something in the string replaced with the value of another string. Here's his code:

    to switchText from t to r instead of s
    	set d to text item delimiters
    	set text item delimiters to s
    	set t to t's text items
    	set text item delimiters to r
    	tell t to set t to item 1 & ({""} & rest)
    	set text item delimiters to d
    end switchText
    When I tried the second block of code, AppleScript told me it expected something to follow "to randomID".

    Any help would be greatly appreciated!
  2. Red Menace macrumors 6502

    Red Menace

    May 29, 2011
    Littleton, Colorado, USA
    When defining and calling handlers, you must include its parameters, even if there aren't any (in that case, you include a pair of empty parentheses after the handler name). The second form just uses labeled parameters instead of positional ones - see the Handler Basics section of the AppleScript Language Guide.

Share This Page