Copy full email and paste @ onwards only. Ie: "@company.com"

Hi All,

Fist time posting so let me know if I’m making any mistakes.

RUNNING
OS Sierra 10.12.6
iMac 21.5-inch, Late 2015
2.8 HGz Intel Core i5

AIM
I would like to highlight an email address in my web browser and then run a script to:

  • Copy email
  • Open Apple Mail
  • Select search bar
  • Paste end of email only Ie: “@company.com

USE CASE
This will allow me to search for anyone from that company who has emailed rather than the individual.

FURTHER INFO
I have all the code i need below except the part that removes the characters before the @ sign in the email. Any help with this tricky part would be greatly appreciated. Thanks in advance!

CURRENT CODE

 
on run {input, parameters}
	
	tell application "Google Chrome"
		activate
		delay 0.1
	end tell
	
	tell application "System Events"
		tell process "Google Chrome"
			tell menu bar item "Edit" of menu bar 1
				click menu item "Copy" of menu 1 -- COPY EMAIL ADDRESS
			end tell
		end tell
	end tell
	
	tell application "Mail"
		activate
		delay 0.1
	end tell
	
	tell application "System Events"
		tell process "Mail"
			tell menu bar item "Edit" of menu bar 1
				tell menu item "Find" of menu 1
					click menu item "Mailbox Search" of menu 1 -- FIND
				end tell
			end tell
		end tell
	end tell
	
	tell application "System Events"
		tell process "Mail"
			tell menu bar item "Edit" of menu bar 1
				click menu item "Paste" of menu 1 -- PASTE EMAIL ADDRESS
			end tell
		end tell
	end tell
	
	delay 0.2
	
	tell application "System Events"
		key code 48 -- TAB
		delay 0.2
		key code 48 -- TAB
		delay 0.2
		key code 48 -- TAB
		delay 0.4
		key code 49 -- space bar
	end tell
	
	return input
end run

Model: iMac 21.5-inch, Late 2015
AppleScript: Automator 2.7
Browser: Safari 537.36
Operating System: macOS 10.12

Hi. Welcome to MacScripter.

I don’t know if there are more direct ways of scripting Google Chrome or Mail for your purposes, but assuming what you have already works, you need to insert these two lines immediately above the System Events block which pastes the clipboard contents into Mail:

set theEmailAddress to (the clipboard)
set the clipboard to text (offset of "@" in theEmailAddress) thru end of theEmailAddress

Things that are copied to the clipboard through the proxy of GUI scripting can take a fraction of a second to arrive there. The script gets no feedback about this and goes straight on to the next command. So the above code is best placed immediately before the pasting code rather than immediately after the copying code. Hopefully this will leave enough time for the clipboard contents to change before the editing code reads them. The ‘set the clipboard to’ command is a direct one from the script, so the script will probably wait for it to complete before moving on.

MacScripter has its own [applescript] and [/applescript] BBCode tags which people are encouraged to use when posting AppleScript code here. Code enclosed between them in the posting window appears as above when posted, with a clickable link which opens the code in the clicker’s default editor. There’s a button for these tags just above the text field in each posting window.

Thanks!

That worked perfectly!

And noted re the AppleScript tags.

Not sure if I need to start a new topic for this but it works perfectly in the open Automator window but when I assign a keyboard shortcut and try I see the following error:

The action “Run AppleScript” encountered an error.

It’s not caused by permissions.

Any help or ideas appreciated. :slight_smile:

Xxx

Hi. Sorry. I’ve only just noticed your feedback.

My guess would be that the modifier key(s) you’re using for the shortcut is/are still down when the script runs and may be changing what’s available in the applications’ menus or the behaviour of the key code commands at the end. If it’s not this, I don’t know what else to suggest. :frowning: