Shortcut to open gmail and enter subject

Hi I have created an apple script that creates job folders and such depending upon job number and job name. I would also like the same script to place a hyperlink in each of the created folders so that when I click on it it will start my gmail with the subject pre populated. Does anyone have any thoughts on how this may be possible. This is my script thus far: Thank you in advance

global jobNum
global newJobFolder
set jobNum to text returned of (display dialog “Enter a job number:” default answer “”)
set jobName to text returned of (display dialog “Enter a job name:” default answer “”)
set folderpath to (choose folder with prompt “Select client folder”)
set newJobFolder to my newFold(jobNum, jobName, folderpath)
on newFold(theNumber, theName, thefolder)
set subNameList to {“Designs”, “Documents”, “Received”}
set itemCount to count of subNameList
tell application “Finder”
set newJobFolder to (make new folder at thefolder with properties ¬
{name:theNumber & " " & theName})
repeat with i from 1 to itemCount
make new folder at newJobFolder with properties ¬
{name:jobNum & " " & item i of subNameList}
end repeat
duplicate file “Users:ace:Dropbox:Company:0000-1_E.xlsx” of startup disk to folder (jobNum & " Documents") of newJobFolder
set name of the result to jobNum & " E.xlsx"
end tell
end newFold

How do you access gmail? Do you use a client like Mail, Thunderbird, Outlook? Or if you use a browser, what browser?

It seems like the easiest way to handle the link would to be use OSX’s .webloc format that saves a hyperlink as a file, but that doesn’t appear to support “mailto:” as the protocol.

Other options would be to save an applescript as a .app or save a shell script as a .sh that open the mailto, but Gatekeeper might make you go through a “are you sure you want to run this” every time you open one, if you’re generating a new executable for each one.

Ah, it can be handled with a hypertext link file, the extension just has to be .mailloc

I’ll take a look at that.

Ok, here you go. I don’t know what you want for subject / email body, but you can modify those variables. Presumably something involving the job name/number.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

global jobNum
global newJobFolder
set jobNum to text returned of (display dialog "Enter a job number:" default answer "")
set jobName to text returned of (display dialog "Enter a job name:" default answer "")
set folderpath to (choose folder with prompt "Select client folder")
set newJobFolder to my newFold(jobNum, jobName, folderpath)


on newFold(theNumber, theName, thefolder)
	set emailAddress to "someone@somewhere.com"
	set emailSubject to "Insert Subject Line Here"
	set bodyText to "Insert email body text here"
	set emailLinkFileName to "Send Email"
	
	set subNameList to {"Designs", "Documents", "Received"}
	set itemCount to count of subNameList
	set linkBody to "mailto:" & emailAddress & "?subject=" & my replace_chars(emailSubject, " ", "%20") & "&body=" & my replace_chars(bodyText, " ", "%20")
	tell application "Finder"
		set newJobFolder to (make new folder at thefolder with properties ¬
			{name:theNumber & " " & theName})
		repeat with i from 1 to itemCount
			make new folder at newJobFolder with properties ¬
				{name:jobNum & " " & item i of subNameList}
		end repeat
		duplicate file "Users:ace:Dropbox:Company:0000-1_E.xlsx" of startup disk to folder (jobNum & " Documents") of newJobFolder
		set name of the result to jobNum & " E.xlsx"
		make new internet location file to linkBody at newJobFolder with properties {name:emailLinkFileName, name extension:"mailloc"}
		-- This shouldn't be necessary, but for some reason the Finder always creates this file with the invalid file extension ".mailtoloc" instead of ".mailoc", even when using the explicit extension declaration. So I'm just changing the extension after making it so it will work.
		set the name extension of file (((newJobFolder as text) & emailLinkFileName) & ".mailtoloc" as alias) to "mailloc"
	end tell
end newFold

-- Generic find and replace functionality
on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Anybody want to comment on Applescript/Finder always creating the wrong file extension here? Did I find a bug?

Oh yeah, and if you don’t have gmail set up to handle mailto’s:

https://www.howtogeek.com/210119/how-to-set-gmail-as-your-default-mail-client-in-os-x/

thanks so much ill try that

Note o tried the link above it does open a gmail to start a new email but is there a way for it to pre populat the emails subject with jobNum -jobName ? Thank you

In your original post you just said “with the subject pre populated,” you didn’t say what you wanted it populated with.

So when I posted my script, I just used “Insert Subject Line Here” as the variable contents and said “I don’t know what you want for subject / email body, but you can modify those variables. Presumably something involving the job name/number.”

Here it is with the subject line you asked for inserted there.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

global jobNum
global newJobFolder
set jobNum to text returned of (display dialog "Enter a job number:" default answer "")
set jobName to text returned of (display dialog "Enter a job name:" default answer "")
set folderpath to (choose folder with prompt "Select client folder")
set newJobFolder to my newFold(jobNum, jobName, folderpath)


on newFold(theNumber, theName, thefolder)
	set emailAddress to "someone@somewhere.com"
	set emailSubject to theNumber & " -" & theName
	set bodyText to "Insert email body text here"
	set emailLinkFileName to "Send Email"
	
	set subNameList to {"Designs", "Documents", "Received"}
	set itemCount to count of subNameList
	set linkBody to "mailto:" & emailAddress & "?subject=" & my replace_chars(emailSubject, " ", "%20") & "&body=" & my replace_chars(bodyText, " ", "%20")
	tell application "Finder"
		set newJobFolder to (make new folder at thefolder with properties ¬
			{name:theNumber & " " & theName})
		repeat with i from 1 to itemCount
			make new folder at newJobFolder with properties ¬
				{name:jobNum & " " & item i of subNameList}
		end repeat
		duplicate file "Users:ace:Dropbox:Company:0000-1_E.xlsx" of startup disk to folder (jobNum & " Documents") of newJobFolder
		set name of the result to jobNum & " E.xlsx"
		make new internet location file to linkBody at newJobFolder with properties {name:emailLinkFileName, name extension:"mailloc"}
		-- This shouldn't be necessary, but for some reason the Finder always creates this file with the invalid file extension ".mailtoloc" instead of ".mailoc", even when using the explicit extension declaration. So I'm just changing the extension after making it so it will work.
		set the name extension of file (((newJobFolder as text) & emailLinkFileName) & ".mailtoloc" as alias) to "mailloc"
	end tell
end newFold

-- Generic find and replace functionality
on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

thank you so much It works exactly as needed and saves a lot of time

You’re welcome. If you need it, note that the email body text can also be generated and filled in by the script, although I think there’s some (not super long) character limit for a mailto.

The CC field can also be included in the mailto link if needed.

  • Tom.

Thanks Tom.

one other item. If i want to collect an account manager email at start so it can populate the email to field what syntax would be used. I tried this without success: (refer lines
set jobMgr to text returned of (display dialog “Enter account manager email:” default answer “”)
and
set emailAddress to jobMgr

global jobNum
global newJobFolder
set jobNum to text returned of (display dialog “Enter a job number:” default answer “”)
set jobName to text returned of (display dialog “Enter a job name:” default answer “”)
set jobMgr to text returned of (display dialog “Enter account manager email:” default answer “”)
set folderpath to (choose folder with prompt “Select client folder”)
set newJobFolder to my newFold(jobNum, jobName, folderpath)

on newFold(theNumber, theName, thefolder)
set emailAddress to jobMgr
set emailSubject to theNumber & " -" & theName
set bodyText to “”
set emailLinkFileName to theNumber & " -" & theName

set subNameList to {"Designs", "Documents", "Received"}
set itemCount to count of subNameList
set linkBody to "mailto:" & emailAddress & "?subject=" & my replace_chars(emailSubject, " ", "%20") & "&body=" & my replace_chars(bodyText, " ", "%20")
tell application "Finder"
	set newJobFolder to (make new folder at thefolder with properties ¬
		{name:theNumber & " " & theName})
	repeat with i from 1 to itemCount
		make new folder at newJobFolder with properties ¬
			{name:jobNum & " " & item i of subNameList}
	end repeat
	duplicate file "Users:ace:Dropbox:Company:0000-1_E.xlsx" of startup disk to folder (jobNum & " Documents") of newJobFolder
	set name of the result to jobNum & " E.xlsx"
	make new internet location file to linkBody at newJobFolder with properties {name:emailLinkFileName, name extension:"mailloc"}
	-- This shouldn't be necessary, but for some reason the Finder always creates this file with the invalid file extension ".mailtoloc" instead of ".mailoc", even when using the explicit extension declaration. So I'm just changing the extension after making it so it will work.
	set the name extension of file (((newJobFolder as text) & emailLinkFileName) & ".mailtoloc" as alias) to "mailloc"
end tell

end newFold

– Generic find and replace functionality
on replace_chars(this_text, search_string, replacement_string)
set AppleScript’s text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript’s text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript’s text item delimiters to “”
return this_text
end replace_chars