Mac Mail: Adding a string formatted as a URL

I’ve been looking all over for this and it seems to me that there should be an easy solution.

I’m using text expander to write an applescript snippet which I want to perform a number of actions.

  1. Request a Jira ID via a dialogue
  2. Use System Events to paste the jira ID
  3. Use System Events to select the recently pasted jira ID
  4. Use System Events to do a keystroke “k” & command down to bring up the ‘Insert Link’ dialogue
  5. Use System Events to press return and…

Voila.

One piece of text which is a link to a jira ID.

It bugs me that I can’t do it


display dialog "Enter Jira ID" default answer ""
set jiraID to "ID-" & text returned of result

tell application "System Events.app"
	tell application "Mail"
		keystroke jiraID
		--set cursor_loc to characterOffset of cursor
		select (characters 0 through -8)
		keystroke "k" & command down
		keystroke "https://myjirasite.com/jira/browse/" & jiraID
		
		keystroke return
	end tell
end tell

Hi DemonLlama,

This line:

looks like it should be:
keystroke “k” using command down

editted: also, the usual form of the ui scripting is like this:


tell application "Mail" to activate
tell application "System Events"
	tell process "Mail"
		-- your ui script here
	end tell
end tell

editted: I’m not sure what you’re trying to select, but here’s an example with the insertion location in the body of the outgoing message:


display dialog "Enter Jira ID" default answer ""
set jiraID to "ID-" & text returned of result
tell application "Mail" to activate
tell application "System Events"
	tell process "Mail"
		keystroke jiraID
		key code 123 using {command down, shift down} -- left arrow select
		keystroke "k" using command down
		keystroke return
	end tell
end tell

If you need to select just a few characters, then don’t use ‘command down’, but repeatedly select one character at a time with ‘shift’ and ‘left arrow’. I can’t find selection in the current Mail dictionary. This might be easier than ui scripting the outgoing message window.

kel.

That’s great thanks, any idea as to how to select the last few characters pasted into a mail?

Hi,

If you want to select the kiraID that was just entered:


display dialog "Enter Jira ID" default answer ""
set jiraID to "ID-" & text returned of result
set IDlength to count jiraID
tell application "Mail" to activate
tell application "System Events"
	tell process "Mail"
		keystroke jiraID
		repeat IDlength times
			key code 123 using shift down -- left arrow select one character
		end repeat
		keystroke "k" using command down
	end tell
end tell

After the keystroke “k” line, you would need to script the dialog window to enter the url.

Ok,

Got it. Here it is:


activate
delay 1
display dialog "Enter Jira ID" default answer ""
set jiraID to "PROJ-" & text returned of result
set idLength to count jiraID
tell application "Mail" to activate
tell application "System Events"
	tell process "Mail"
		keystroke jiraID
		repeat idLength times
			key code 123 using shift down -- left arrow select one character
		end repeat
		keystroke "k" using command down
		keystroke "https://myjirasite.net/jira/browse/" & jiraID
		key code 52
		key code 124
		keystroke "]"
		
		repeat (idLength + 1) times
			key code 123 -- left arrow 
		end repeat
		
		
		keystroke "["
		
		repeat (idLength + 1) times
			key code 124 -- right arro 
		end repeat
		
		
	end tell
	
end tell