AppleScript - Passing Automator input into a handler

Discussion in 'Mac Programming' started by swwack91, Mar 2, 2018.

  1. swwack91 macrumors 6502a

    Joined:
    Jan 28, 2007
    Location:
    New Jersey
    #1
    Hello. I'm new to AppleScript and am trying to incorporate it into an Automator script. I'm trying to trim text using a handler I found online. However, using the run handler to set the input variable (the default AppleScript) code for Automator, causes an error saying that I can't have two run commands.

    Here's the default script for Automator:

    Code:
    on run {input, parameters}
        
        (* Your script goes here *)
        
        return input
    end run
    And here's the handler I'm trying to use:

    Code:
    on trimText(theText, theCharactersToTrim, theTrimDirection)
        set theTrimLength to length of theCharactersToTrim
        if theTrimDirection is in {"beginning", "both"} then
            repeat while theText begins with theCharactersToTrim
                try
                    set theText to characters (theTrimLength + 1) thru -1 of theText as string
                on error
                    -- text contains nothing but trim characters
                    return ""
                end try
            end repeat
        end if
        if theTrimDirection is in {"end", "both"} then
            repeat while theText ends with theCharactersToTrim
                try
                    set theText to characters 1 thru -(theTrimLength + 1) of theText as string
                on error
                    -- text contains nothing but trim characters
                    return ""
                end try
            end repeat
        end if
        return theText
    end trimText
    
    How do I take the input and get it to process using the trimText handler?
     
  2. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    In an AppleScript, the run handler can be either explicitly declared, or statements at the root level of the script are assumed to be a part of it - you can't have both.

    In the Run AppleScript action, the run handler is what is executed, with the input parameter being a list of items passed in from the previous action. If you know the order of the input items, just pass the appropriate list items as the parameters to your trimText handler.
     
  3. swwack91 thread starter macrumors 6502a

    Joined:
    Jan 28, 2007
    Location:
    New Jersey
    #3
    Thanks for the quick reply. Could you give me an example of how to call on the list items? In my particular use case, it’s only one item being passed into this action as input.
     
  4. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #4
    Your trimText handler takes 3 arguments, so I'm guessing there is more to your script? The input parameter to the run handler is a regular AppleScript list, so you can do normal list stuff like setting a variable to item whatever of the input, etc. If you are just getting the text item to trim, you can also use a repeat statement to step through the list.
     

Share This Page

3 March 2, 2018