My script goes through a sequence and looks to see if each string contains any data… If it does, then it adds the value of those strings containing data to another string and places a comma after each item, and does not place a comma at the end… My question is:
Is there a smarter way to do this than how I am doing it? Thank you so much in advance.
if companybackup ≠"" then
set commentfield to "Company: " & companybackup
if finalgroup ≠"" or birthday ≠"" or finalurl ≠"" or notedata ≠"" then
set commentfield to commentfield & ", "
end if
end if
if finalgroup ≠"" then
set commentfield to commentfield & "Groups: " & finalgroup
if birthday ≠"" or finalurl ≠"" or notedata ≠"" then
set commentfield to commentfield & ", "
end if
end if
if birthday ≠"" then
set commentfield to commentfield & "Birthday: " & birthday
if finalurl ≠"" or notedata ≠"" then
set commentfield to commentfield & ", "
end if
end if
if finalurl ≠"" then
set commentfield to commentfield & finalurl
if notedata ≠"" then
set commentfield to commentfield & ", "
end if
end if
if notedata ≠"" then
set commentfield to commentfield & "Notes: " & notedata
end if
Hi, Patrick. Here are a couple of alternatives:
set commentfield to ""
if companybackup ≠"" then set commentfield to "Company: " & companybackup & ", "
if finalgroup ≠"" then set commentfield to commentfield & "Groups: " & finalgroup & ", "
if birthday ≠"" then set commentfield to commentfield & "Birthday: " & birthday & ", "
if finalurl ≠"" then set commentfield to commentfield & finalurl & ", "
if notedata ≠"" then
set commentfield to commentfield & "Notes: " & notedata
else if commentfield is not "" then
set commentfield to text 1 thru -3 of commentfield
end if
-- Or:
set fieldVars to {companybackup, finalgroup, birthday, finalurl, notedata}
set headings to {"Company: ", "Groups: ", "Birthday: ", "", "Notes: "}
set commentfield to {}
repeat with i from 1 to 5
if (count item i of fieldVars) > 0 then set end of commentfield to item i of headings & item i of fieldVars
end repeat
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set commentfield to commentfield as string
set AppleScript's text item delimiters to astid
You are the best! That (2nd example) is exactly what I was hoping for… I didn’t realize that lists could contain strings… but it makes perfect sense.
thanks so much.