Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

swwack91

macrumors 6502a
Original poster
Jan 28, 2007
736
23
New Jersey
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?
 

Red Menace

macrumors 6502a
May 29, 2011
583
230
Colorado, USA
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.
 

swwack91

macrumors 6502a
Original poster
Jan 28, 2007
736
23
New Jersey
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.

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.
 

Red Menace

macrumors 6502a
May 29, 2011
583
230
Colorado, USA
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.