Need help: Attach Images to separate emails biased on similar filename

I have 1000’s :expressionless: of images that are named:
123 Main St New York City NY_1.jpg
123 Main St New York City NY_2.jpg
123 Main St New York City NY_3.jpg
456 South Main St New York City NY_1.jpg
456 South Main St New York City NY_2.jpg
789 North Main St New York City NY_1.jpg
789 North Main St New York City NY_2.jpg
789 North Main St New York City NY_3.jpg

Some houses have 2 photos some have 4 or even 5. The “" could be the the delimiter the numbering will always be after the "” . The address will never use"_" but may contain “-”.

What I am trying to automate is emailing each house with all the images attached and the subject the house name.
I have looked at a bunch of compare scripts but I cant seem to get them to work with my scenario. The emailing part I should be able to handle.

Any help would be great thanks so much!!! :slight_smile:

Am not entirely certain about your query.

But this should get you a list of the Unique Subject Names ie 1 subject name per house.

set thepicFiles to {"123 Main St New York City NY_1.jpg", "123 Main St New York City NY_2.jpg", "123 Main St New York City NY_3.jpg", "456 South Main St New York City NY_1.jpg", "456 South Main St New York City NY_2.jpg", "789 North Main St New York City NY_1.jpg", "789 North Main St New York City NY_2.jpg", "789 North Main St New York City NY_3.jpg"}
set subjectList to {}
repeat with i from 1 to count of items in thepicFiles
	set pic to item i in thepicFiles as string
	set x to offset of "_" in pic
	set sub to text 1 thru (x - 1) in pic as string
	set end of subjectList to sub
end repeat
set uniquesubjectList to {}
repeat with x from 1 to count of items of subjectList
	set N to item x of subjectList
	if N is not in uniquesubjectList then set end of uniquesubjectList to N
end repeat
return uniquesubjectList----------{"123 Main St New York City NY", "456 South Main St New York City NY", "789 North Main St New York City NY"}

Hope it’s of some help.

Val

Thanks for the reply I will play with it at work and let you know how I make out. Thanks Again!!!

This might be a little more helpful to dig out the “unique houses” from the files.

set docfolderpath to path to documents folder as string
set yourfolder to "ABC" --------- the folder with your jpgs and perhaps other the documents
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string
set FNDVAR to ".jpg" --------your "FIND" variable to separate the jpgs from other files
set ppath to quoted form of POSIX path of pathtoyourfolder
set thepicFiles to (do shell script "mdfind -onlyin " & ppath & " 'kMDItemDisplayName == \"*" & FNDVAR & "*\"'")---if you have 1000's you may prefer to use this
set subjectList to {}
set uniquesubjectList to {}
repeat with i from 1 to count of paragraph in thepicFiles
	set theD to paragraph i of thepicFiles as text
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"/"}
	set x to last text item in theD
	set y to offset of "_" in x
	set sub to text 1 thru (y - 1) in x as string
	set end of subjectList to sub
	set AppleScript's text item delimiters to oldDelims
end repeat
repeat with x from 1 to count of items of subjectList
	set N to item x of subjectList
	if N is not in uniquesubjectList then set end of uniquesubjectList to N
end repeat
return uniquesubjectList

Cheers,

Val

set docfolderpath to path to documents folder as string
set yourfolder to "/Users/Stephanie/Desktop/51510" --------- the folder with your jpgs and perhaps other the documents
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string
set FNDVAR to ".jpg" --------your "FIND" variable to separate the jpgs from other files
set ppath to quoted form of POSIX path of pathtoyourfolder
set thepicFiles to (do shell script "mdfind -onlyin " & ppath & " 'kMDItemDisplayName == \"*" & FNDVAR & "*\"'") ---if you have 1000's you may prefer to use this
set subjectList to {}
set uniquesubjectList to {}
repeat with i from 1 to count of paragraph in thepicFiles
	set theD to paragraph i of thepicFiles as text
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"/"}
	set x to last text item in theD
	set y to offset of "_" in x
	set sub to text 1 thru (y - 1) in x as string
	set end of subjectList to sub
	set AppleScript's text item delimiters to oldDelims
end repeat
repeat with x from 1 to count of items of subjectList
	set N to item x of subjectList
	if N is not in uniquesubjectList then set end of uniquesubjectList to N
end repeat
return uniquesubjectList

Doesnt seem to work, am I missing anything?

Try changing

set docfolderpath to path to documents folder as string
set yourfolder to "/Users/Stephanie/Desktop/51510" --------- the folder with your jpgs and perhaps other the documents
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string

to

set docfolderpath to path to desktop folder from user domain as string
set yourfolder to "51510"
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string

Cheers,

Val

Okay I got That to work and I think I got 90% of the email. I just don’t know how to add the photos to the email.

Thanks for all your help here is what I came up with.:rolleyes:

set docfolderpath to path to desktop folder from  user domain as string
set yourfolder to "51510"
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string
set FNDVAR to ".jpg"
set ppath to quoted form of POSIX path of pathtoyourfolder
set thepicFiles to (do shell script "mdfind -onlyin " & ppath & " 'kMDItemDisplayName == \"*" & FNDVAR & "*\"'")
set subjectList to {}
set uniquesubjectList to {}
    repeat with i from 1 to count of paragraph in thepicFiles
       set theD to paragraph i of thepicFiles as text
       set oldDelims to AppleScript's text item delimiters
       set AppleScript's text item delimiters to {"/"}
       set x to last text item in theD
       set y to offset of "_" in x
       set sub to text 1 thru (y - 1) in x as string
       set end of subjectList to sub
       set AppleScript's text item delimiters to oldDelims
    end repeat
    repeat with x from 1 to count of items of subjectList
       set N to item x of subjectList
       if N is not in uniquesubjectList then set end of uniquesubjectList to N

	tell application "Mail"
   		set theMessage to make new outgoing message with properties 
       		{visible:False, subject:N , content:N & " photos are attached."}
  		tell theMessage
        		make new to recipient at end of to recipients with properties 
        		{name:"test", address:"test@gmail.com"}
			make new attachment with properties {file name:theAttachment} at after last paragraph----This is my issue

        	end tell
	end tell
        send theMessage
    end repeat
    return uniquesubjectList

hi bribas,

We’re getting there !

I have tried to add the repeats to complete this as an exercise. It seems to work.

The appropriate multiple jpgs are attached to a different single email based on each unique address.

So there will be 3 jpgs attached to 1 email to 123 Main St New York City, 2 in the email to 456 etc.

Adjust as necessary to suit your requirements.

It is only A solution. Am relatively new to this - so there are, most probably, far more efficient solutions to come from other posters.

It works under the various assumptions outlined below and as per comments in the script, but you can adjust it to suit you.

To test it I had to make some assumptions.

Assumptions.

  1. From the example jpgs I have assumed that everybody has a street address! So
    a) " St " has been used as an offset.
    b) the street address has been used to find the email address.

  2. That the addresses and emails are stored in the Apple Address Book application.

  3. That the addresses and emails used are the first address and the first email in Address Book app.

None of these may apply to you but all of them can be taken out or re-jigged to suit.

The close and save, send and delay at the end of this version of the script are commented out and can be put back in as necessary. If, at first, you are only doing 2 or 3 jpgs to test it I’d leave visible on. You then can confirm that the correct files are attached. Then comment it out when you are sure it works.


sset docfolderpath to path to desktop folder from user domain as string
set yourfolder to "51510" --------- the folder with your jpgs and perhaps other the documents
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string
set FNDVAR to ".jpg" --------your "FIND"  variable for the files to separate the jpgs from other files
set ppath to quoted form of POSIX path of pathtoyourfolder
set thepicFiles to (do shell script "mdfind -onlyin " & ppath & " 'kMDItemDisplayName == \"*" & FNDVAR & "*\"'") ----getting a list of the paths to the .jpg files. Necessary for attachments later.
set subjectList to {}
set uniquesubjectList to {}
set EmList to {}
set AdrList to {}
repeat with i from 1 to count of paragraph in thepicFiles ----- extracting a list of street addresses from the jpg files
	set theD to paragraph i of thepicFiles as text
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"/"}
	set x to last text item in theD
	set y to offset of "_" in x
	set sub to text 1 thru (y - 1) in x as string
	set end of subjectList to sub
	set AppleScript's text item delimiters to oldDelims
end repeat
repeat with x from 1 to count of items of subjectList ------ extracting a list unique street addresses from the list of the street addresses 
	set N to item x of subjectList
	if N is not in uniquesubjectList then set end of uniquesubjectList to N
end repeat
repeat with z from 1 to count of items of uniquesubjectList ---- extracting a list of "just the street part" from the list unique street addresses 
	set a to item z of uniquesubjectList as text
	set b to offset of " St " in a --------assumption 1 that everybody lives on a street
	set c to text 1 thru (b + 2) in a as string
	try ------ a precaution that hopefully excludes those outside the assumptions and allows the script to carry on regardless returning only the items that meet the criteria
		tell application "Address Book" --------- assumption 2  that your email addresses are stored in your Apple AddressBook.
			activate
			if exists first email of (first person whose street of first address is c) then ------assumption 1 that everybody lives on a street
				set theAem to first email of (first person whose street of first address is c) ------assumption 3 that the email and address are the person's first email and first address in their Apple Address Book Entry
				set end of EmList to theAem ------aligning the email list in tandem with the address list/unique subject List
				set end of AdrList to c ------- ditto
			end if
		end tell
	end try
end repeat
repeat with k from 1 to count of items in EmList
	tell application "Address Book"
		set theEm to value of item k of EmList
		set theAdr to item k of AdrList
	end tell
	set theSubject to short date string of (current date) & ("    More Great Photos of " & theAdr) ------ replace with your preference
	set theBody to "Please take a look at these photographs and get back" ----- replace with your preference
	tell application "Mail"
		set theSender to "your email @ yahhoo.com" ------replace with your email address
		set thercpnt to theEm
		set theMessage to make new outgoing message with properties {address:thercpnt, subject:theSubject, content:theBody & return & return}
		tell theMessage
			set visible to true ------optional--handy if just testing a few
			set sender to theSender
			make new to recipient at beginning of to recipients with properties {address:thercpnt}
			repeat with y from 1 to count of paragraphs in thepicFiles ------aligning the appropriate multiple attachment files with a unique address
				if paragraph y of thepicFiles contains item k of AdrList then
					set theAttachment to paragraph y in thepicFiles
					tell content
						make new attachment with properties {file name:theAttachment} at after the last paragraph
					end tell
				end if
			end repeat
		end tell
		----send theMessage ---- left out for testing
		----close theMessage saving yes ---- aternative for testing. Emails will appear in Drafts folder --- eventually. Quit Mail & re-open & they will be there.
		----delay ??? depending on your set-up,file size, speed of connection, etc etc given that there are 1000's of jpgs involved it may be necessary.
	end tell
end repeat


Cheers,

Val

1st off thanks you
2nd: thank you again!
3rd: I wont have time to play with this till tonight. I looked it over quick and looks great!!! I will have to make some modifications, but I will post my version. As for the efficiency all I have to say its its far more efficient then me and my buddy doing it manually. :slight_smile: I’ll let you know how I make out tonight.

I got it to work!!! Again Thanks SOOOO much!!!



set docfolderpath to path to desktop folder from user domain as string
set yourfolder to "51510" --------- the folder with your jpgs and perhaps other the documents
set pathtoyourfolder to docfolderpath & yourfolder & ":" as string
set FNDVAR to ".jpg" --------your "FIND" variable for the files to separate the jpgs from other files
set ppath to quoted form of POSIX path of pathtoyourfolder
set thepicFiles to (do shell script "mdfind -onlyin " & ppath & " 'kMDItemDisplayName == \"*" & FNDVAR & "*\"'") ----getting a list of the paths to the .jpg files. Necessary for attachments later.
set subjectList to {}
set uniquesubjectList to {}
set EmList to {}
set AdrList to {}
set filePath to (path to desktop as string) & "Log.txt"
set newText to ((current date) as string) & tab & return

--Display Dialog and Get Email Input
display dialog "Who are they going to?" default answer "test@gmail.com"
--Get/Set Email Answer
set theEmailAnswer to (text returned of result)

--Display Dialog and Get City Input
--display dialog "What city did you do?" default answer "New York City"
--Get/Set City Answer
--set theCityAnswer to (text returned of result)

repeat with i from 1 to count of paragraph in thepicFiles ----- extracting a list of street addresses from the jpg files
	set theD to paragraph i of thepicFiles as text
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"/"}
	set x to last text item in theD
	set y to offset of "_" in x
	set sub to text 1 thru (y - 1) in x as string
	set end of subjectList to sub
	set AppleScript's text item delimiters to oldDelims
end repeat

repeat with x from 1 to count of items of subjectList ------ extracting a list unique street addresses from the list of the street addresses
	set N to item x of subjectList
	if N is not in uniquesubjectList then set end of uniquesubjectList to N
end repeat

repeat with z from 1 to count of items of uniquesubjectList ---- extracting a list of "just the street part" from the list unique street addresses
	set a to item z of uniquesubjectList as text
	--set b to offset of theCityAnswer in a -------- Replace ST with city because that is the true delimitor and we only do one at a time.
	--set c to text 1 thru (b + 2) in a as string
	--	try ------ a precaution that hopefully excludes those outside the assumptions and allows the script to carry on regardless returning only the items that meet the criteria
	---       tell application "Address Book" --------- assumption 2 that your email addresses are stored in your Apple AddressBook.
	---           activate
	---           if exists first email of (first person whose street of first address is c) then ------assumption 1 that everybody lives on a street
	---               set theAem to first email of (first person whose street of first address is c) ------assumption 3 that the email and address are the person's first email and first address in their Apple Address Book Entry
	-- set end of EmList to theAem ------aligning the email list in tandem with the address list/unique subject List
	set end of AdrList to a ------- ditto
	---           end if
	---       end tell
	--	end try
end repeat

repeat with k from 1 to count of items in uniquesubjectList
	---EmList	
	---   tell application "Address Book"
	---set theEm to value of item k of EmList
	set theAdr to item k of AdrList
	---   end tell
	
	set theSubject to theAdr -----short date string of (current date)replace with your preference
	set theBody to "Here are the photographs of " & theAdr ----- replace with your preference
	set newText to newText & theAdr & return
	tell application "Mail"
		set theSender to "test@gmail.com" ------replace with your email address
		set thercpnt to theEmailAnswer ---use theEm if email address are from addressbook.
		set theMessage to make new outgoing message with properties {address:thercpnt, subject:theSubject, content:theBody & return & return}
		tell theMessage
			set visible to true ------optional--handy if just testing a few
			set sender to theSender
			
			make new to recipient at beginning of to recipients with properties {address:thercpnt}
			repeat with y from 1 to count of paragraphs in thepicFiles ------aligning the appropriate multiple attachment files with a unique address
				if paragraph y of thepicFiles contains item k of AdrList then
					set theAttachment to paragraph y in thepicFiles
					set newText to newText & tab & theAttachment & return
					tell content
						make new attachment with properties {file name:theAttachment} at after the last paragraph
					end tell
				end if
				
			end repeat
		end tell
		
		----send theMessage ---- left out for testing
		----close theMessage saving yes ---- aternative for testing. Emails will appear in Drafts folder --- eventually. Quit Mail & re-open & they will be there.
		----delay ??? depending on your set-up,file size, speed of connection, etc etc given that there are 1000's of jpgs involved it may be necessary.
		
	end tell
end repeat
try
	set newText to newText & ((current date) as string) & return & "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & return & return
	set fileRef to open for access file filePath with write permission
	write newText to fileRef starting at eof
	close access fileRef
on error
	try
		close access file filePath
	end try
end try




I removed the address book functionality of it because the email address is always the same person. I added a dialog for that. I also added a log to write to the desktop.

I only had a little time to play tonight but I will post an update again soon.

Thanks again!!!