Delete certain pieces of text?

Hello all. I need some help. I have a mailing list for my business and the way text is entered is like this:

“email”,“date”,“ip”

So a quick example would be:

bob@bob.com”,“1268080664”,“XX.XXX.XXX.XXX”

Except I want to use a piece of software for Mac instead of using the online tool. I set up automator to download the text file the info is stored in, but then I need it to delete some necessary functions. Before you ask, “Why not just make your PHP file remove the extra stuff?” and my answer is that I want to keep it there incase I use it in the future. So basically, I am wondering how in applescript, I can have it remove everything but the email address. The email is always laying between the first two quotes, so that might make it easier. I also posted this in the Automator forum just in case. Thanks!

Anyone? I can’t find any help :frowning:

Hi,

there are no answers, because the description of your problem is not very detailed
You can extract the email address with


set a to "\"bob@bob.com\",\"1268080664\",\"XX.XXX.XXX.XXX\""
set {TID, text item delimiters} to {text item delimiters, quote}
set emailAddress to text item 2 of a
set text item delimiters to TID
emailAddress

Hi,

I once wrote a script to extract eMail addresses from messages in Apple Mail.

The “magic” is done by a small Python script, which parses text files for eMail addresses using a regex expression and returns the found ones to AppleScript.

For your task, the usage would be as follows (just an example):


property mytitle : "mailosearch"

on run
	-- let the user choose a text file
	set txtfile to (choose file with prompt "Please choose a text file containing eMail addresses:" without multiple selections allowed)
	set qtdtxtfilepath to quoted form of POSIX path of txtfile
	
	-- extracting the eMail addresses from the text file using a Python script
	set pyscriptpath to (((path to desktop) as text) & "findmails.py")
	set qtdpyscriptpath to quoted form of POSIX path of pyscriptpath
	set command to "/usr/bin/python " & qtdpyscriptpath & " " & qtdtxtfilepath
	set mailaddrs to paragraphs of (do shell script command)
	if mailaddrs is {} then
		tell me
			activate
			display dialog "Sorry, we could not discover any eMail addresses in the file." buttons {"OK"} default button 1 with icon caution with title mytitle
		end tell
	else
		choose from list mailaddrs with prompt "Found eMail addresses:"
	end if
end run

Might be of some use. Also works with PDF files.

Best regards from Berlin,

Martin

I got this help from another board:

set theData to paragraphs of (read file "path:to:your.txt")
set emailAddresses to {} -- somewhere to store the addresses when we're done
set {old_delims, my text item delimiters} to {my text item delimiters, "\""} -- setup the TIDs
repeat with eachLine in theData
  copy text item 2 of eachLine to end of emailAddresses -- extract the email address
end repeat
set my text item delimiters to old_delims -- clean up when we're done

How would I then have it save it as a second text file, named email2.txt?

Something like this (built on what you have):

set theData to paragraphs of (read file "path:to:your.txt")
set emailAddresses to {} -- somewhere to store the addresses when we're done
set {old_delims, my text item delimiters} to {my text item delimiters, "\""} -- setup the TIDs
repeat with eachLine in theData
	copy text item 2 of eachLine to end of emailAddresses -- extract the email address
end repeat
set text item delimiters to return
set emailText to emailAddresses as text -- now have paragraphs
set my text item delimiters to old_delims -- clean up when we're done
-- Write to the file
set newFile to open for access (path to desktop as text) & "email2.txt" with write permission
set eof of newFile to 0 -- erases the file
try
	write emailText to newFile
	close access newFile
on error
	close access newFile -- make sure it's not left hanging if something goes wrong
end try

Two handlers I just made:


set newFile to writeToNonExistingFile((path to desktop), "Hello World.txt", "Hello World!")
do shell script "open " & quoted form of (POSIX path of newFile)

on writeToExistingFile(anAlias, dataToWrite)
	set OA to open for access anAlias with write permission
	write dataToWrite to OA starting at 0
	close access OA
end writeToExistingFile

on writeToNonExistingFile(aContainer, aName, dataToWrite)
	tell application "Finder" to set fileAlreadyExists to (exists file aName of aContainer)
	if fileAlreadyExists is true then
		set dialRes to (display dialog "The file already exists" buttons {"Cancel Save", "Overwrite", "Other Name"} cancel button 1 default button 3)
		if (button returned of dialRes) is "Cancel Save" then
			return missing value
			
		else if (button returned of dialRes) is "Overwrite" then
			tell application "Finder" to set fileToWrite to (file aName of aContainer) as alias
			set OA to open for access fileToWrite with write permission
			write dataToWrite to OA starting at 0
			close access OA
			
		else if (button returned of dialRes) is "Other Name" then
			set newName to text returned of (display dialog "Please give up a name for the file." & return & return & "Will be saved at: " & POSIX path of aContainer default answer "")
			repeat
				tell application "Finder" to set fileAlreadyExists to (exists file newName of aContainer)
				if fileAlreadyExists is false then exit repeat
				set newName to text returned of (display dialog "Name already in use. Please chose an other one." default answer "")
			end repeat
			
			tell application "Finder" to set fileToWrite to (make new file at aContainer with properties {name:newName})
			set fileToWrite to fileToWrite as alias
			
			set OA to open for access fileToWrite with write permission
			write dataToWrite to OA starting at 0
			close access OA
		end if
	else
		
		log "making file"
		tell application "Finder" to set fileToWrite to (make new file at aContainer with properties {name:aName})
		set fileToWrite to fileToWrite as alias
		
		set OA to open for access fileToWrite with write permission
		write dataToWrite to OA starting at 0
		close access OA
	end if
	
	
	return fileToWrite
end writeToNonExistingFile

Hope it helps,
ief2

I tried that using this code:

set theData to paragraphs of (read file "users:************:desktop:email.txt")
set emailAddresses to {} -- somewhere to store the addresses when we're done
set {old_delims, my text item delimiters} to {my text item delimiters, "\""} -- setup the TIDs
repeat with eachLine in theData
	copy text item 2 of eachLine to end of emailAddresses -- extract the email address
end repeat
set text item delimiters to return
set emailText to emailAddresses as text -- now have paragraphs
set my text item delimiters to old_delims -- clean up when we're done
-- Write to the file
set newFile to open for access (path to desktop as text) & "email2.txt" with write permission
set eof of newFile to 0 -- erases the file
try
	write emailText to newFile
	close access newFile
on error
	close access newFile -- make sure it's not left hanging if something goes wrong
end try

Which links correctly to the text file which contains these words:

test@email.com”,“1266774086”,“XX.XX.XXX.XXX”
test2@email.com”,“1266875977”,“XX.XX.XXX.XXX”

And I get this error in Script Editor:

There is probably an empty line somewhere. Insert a try block to ignore empty lines

repeat with eachLine in theData
	try
		copy text item 2 of eachLine to end of emailAddresses -- extract the email address
	end try
end repeat

@Adam
“"” = quote :wink:

Works perfect now! Thank you! If I have any more questions, I will let you guys know! Thank you too adam!