Sunday, July 22, 2018

#1 2013-04-04 11:58:03 am

Stephen Magladry
Member
Registered: 2009-02-04
Posts: 23

Automator weirdness

I am trying set up a text service that will break up the input into a set of sentences. This handle breaks things up like I want:

Applescript:


on extractSentences(parameter)
   local domainList, currentDelim, textItemCount, theTextItem, theFirstThreeLetters, newItem
   set domainList to {"com", "net", "org"}
   set currentDelim to AppleScript's text item delimiters
   set AppleScript's text item delimiters to "."
   set parameter to text items of parameter
   set AppleScript's text item delimiters to currentDelim
   set textItemCount to the count of parameter
   repeat while textItemCount > 1
       set theTextItem to item textItemCount of parameter
       if the length of theTextItem ≥ 3 then
           -- this is so blah.com and other selected domains don't cause a sentence break
           set theFirstThreeLetters to text 1 thru 3 of theTextItem
           if the theFirstThreeLetters is in domainList then
               set newItem to item (textItemCount - 1) of parameter & "." & theTextItem
               set item (textItemCount - 1) of parameter to newItem
               set parameter to items 1 thru (textItemCount - 1) of parameter & items (textItemCount + 1) thru -1 of parameter
               set textItemCount to textItemCount - 1
           end if
       end if
       set textItemCount to textItemCount - 1
       
   end repeat
   return parameter
end extractSentences

when I do a Run Applescript in Automator,

Applescript:



on run {input, parameters}
   set sentenceList to extractSentences(parameters)
   --note: i'm not worried about returning the results, I'll work on stuff here. excluded for brevity.
   return input
end run

on extractSentences(parameter)
   local currentDelim, textItemCount, theTextItem, theFirstThreeLetters, newItem, domainList
   set domainList to {"com", "net", "org"}
   set currentDelim to AppleScript's text item delimiters
   set AppleScript's text item delimiters to "."
   set parameter to text items of parameter
   set AppleScript's text item delimiters to currentDelim
   set textItemCount to the count of parameter
   repeat while textItemCount > 1
       set theTextItem to item textItemCount of parameter
       if the length of theTextItem ≥ 3 then
           set theFirstThreeLetters to text 1 thru 3 of theTextItem
           if the theFirstThreeLetters is in domainList then
               set newItem to item (textItemCount - 1) of parameter & "." & theTextItem
               set item (textItemCount - 1) of parameter to newItem
               set parameter to items 1 thru (textItemCount - 1) of parameter & items (textItemCount + 1) thru -1 of parameter
               set textItemCount to textItemCount - 1
           end if
       end if
       set textItemCount to textItemCount - 1
       
   end repeat
   return parameter
end extractSentences

I get a syntax error, "Can’t get every text item." on the line "set parameter to text items of parameter"
changing the line to something like "set blah to text items of parameter" doesn't fix the problem.

anyone see why that would happen?

Last edited by Stephen Magladry (2013-04-04 11:59:35 am)


Filed under: Automator, Text Service

Offline

 

#2 2013-04-04 02:30:34 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11499
Website

Re: Automator weirdness

Hi,

as the plural implies, parameters are multiple items aka a list.
Even if you pass only one item you have to dereference the list, e.g.

Applescript:

on extractSentences(theItems)
if count theItems = 0 then return
set parameter to item 1 of theItems
…


regards

Stefan

Offline

 

#3 2013-04-04 02:40:11 pm

Stephen Magladry
Member
Registered: 2009-02-04
Posts: 23

Re: Automator weirdness

Thanks,

that did the trick.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)