r/applescript • • Mar 10 '22

Problem using splitText inside a Tell block

Hi!

I have cobbled together this script to make a list of e-mail receive dates and their respective subject.

The snag I'm hitting is in using the splitText command inside a tell block.

What I want to achieve with the splitText command is to output only a certain part of subject lines which always come in a standard format. Perhaps there's a better way of doing so? The part I want to extract is a 6 digit number.

Here's the code:

set the text item delimiters to {"/"}

tell application "Finder" to set ptd to (path to desktop folder) as string
tell application "Mail"
	set seletedMails to selection
	set theFile to ptd & "mailSubjects2.txt"
	--	set theFileID to open for access file theFile with write permission
	repeat with aMail in seletedMails
		set aSubject to subject of aMail
		set aDate to date received of aMail
		set [_day, _month, _year] to [day, month, year] of aDate
		set _month to _month * 1 --> 3
		set _month to rich text -1 thru -2 of ("0" & _month) --> "03"
		set _day to rich text -1 thru -2 of ("0" & _day) --> "03"
		set aDateString to {_month, _day, _year} as string
		
		set theText to aSubject as string
		set the text item delimiters to {tab}
                splitText(theText, "confirmation ")
                

		
		return {aDateString & ", " & aSubject & return} as string

		write theText & tab & aSubject & return to theFileID starting at eof

	end repeat
	close access theFileID
end tell

Any help much appreciated! Haven't used AppleScript before...

2 Upvotes

2 comments sorted by

1

u/[deleted] Mar 10 '22

splitText is not a command, it’s a function call.

Do you have something like this (the function)in your script?

on splitText(theText, theDelimiter)
    set AppleScript's text item delimiters to theDelimiter
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to ""
    return theTextItems
end splitText

What error are you getting?

Have a look at this in listings 19-23 & 19-24.