0
votes

So I'm trying to write an apple script that sends emails to a selected block of information in numbers.

Numbers is formatted like this:

Numbers spreadsheet

Here is my apple script:

on run {input, parameters}
if the class of input is not string then set input to input as string

set currentRow to my theSplit(input, "*")

set row to 1

repeat while row <= (count of currentRow)

    set fullInput to {}
    set fullInput to my theSplit(item row of currentRow, "  ")
    set row to row + 1

    set theName to ""
    if item 2 of fullInput is "" then
        set theName to item 1 of fullInput
    else
        set theName to item 2 of fullInput
    end if

    set theContent to "Hello " & theName & ", Email Body"

    tell application "Mail"
        set thisMessage to make new outgoing message with properties {subject:"HTML5 Game Sponsorships", content:theContent}
        tell thisMessage
            make new to recipient with properties {address:(item 3 of fullInput)}
            set visible to true

        end tell
    end tell

end repeat

end run

on theSplit(theString, theDelimiter) set oldDelimiters to AppleScript's text item delimiters set AppleScript's text item delimiters to theDelimiter set theArray to every text item of theString set AppleScript's text item delimiters to oldDelimiters return theArray end theSplit

The script creates the emails properly, formats the information correctly. But the problems lies when rows in numbers do not contain a name. The script is suppose to say Hello 'Company Name', when a name is blank.

It does this, but for some reason it writes:

Hello 'Company Name',

It's not supposed to be on the next line. I assume this is because there is some invisible character that represents a line break that I need to remove somehow. The weird thing is that it only does this when it's the second time it needs to use a company name, it gets the first time right.

Well I hope that made sense, any help is greatly appreciated.

1
Instead of posting a screen shot of your AppleScript code, could you please post the actual code itself so others can copy the code to use it in script editor for testing and solutions to your question? - wch1zpink
There you go! I originally posted this on Apple Communities and when I pasted the code it screwed up all the formatting so I just made it an image. Then just copied the post over here after I didn't get any responses for a while. - user4626554

1 Answers

0
votes

You could try something to trim the carriage return inherrent in your company name with:

set theName to characters 2 thru -1 of (item 1 of fullInput)

This set it to the second through til the last character. (-1 means the last, -2 is the 2nd from last etcetera.)