Show me your scripts!

I am interested in seeing everyone’s scripts (in any supported language) they use for TextExpander.

1 Like

I start! Here is the code formy “next weekday scripts”. I often say in emails let’s meet “next Monday”, “next Tuesday”, etc. and to make it clearer I add the date. I have written a script that is doing that for me.

The shortcuts are : nxmon, nxtue, etc.

Here is one for Monday. You can easily adapt it to different weekdays. Shoutout to Doug Stephen for providing the core function (below in the code).

# AppleScript for TextExpander
# Helmut Hauser
# http://www.worksmartandberemarkable.com

# For different weekdays you simply have to change the this line
# e.g., set dateOfNextWeekday to getDateOfNextWeekWeekday(Sunday) -- Sunday
set dateOfNextWeekday to getDateOfNextWeekday(Monday) -- Monday

set myWeekDay to weekday of dateOfNextWeekday
set myMonth to month of dateOfNextWeekday
set myDay to day of dateOfNextWeekday
set myYear to year of dateOfNextWeekday

# Here we put the final output together
# if you want a different format, you will have to change this line
# simply combine variables, like myMonth or my Day), with other textpiece like ")" 
# together with &
#set fullOutput to "next " & myWeekDay & " (" & myMonth & " " & myDay & ", " & myYear & ")"
set fullOutput to "" & myWeekDay & " (" & myMonth & " " & myDay & ")"

return fullOutput
# ----------------------------------------
# subroutine from Doug Stephen
# see http://canadian-fury.com/2012/08/31/performing-fixed-point-date-arithmetic-in-textexpander-using-applescript/
# adapted by Helmut Hauser


# eneric script that allows you to get the date information of a specific upcoming weekday
# e.g., to get the date of next Monday
# getDateOfNextWeekday(Monday)
# Note that the scripts caculates the next upcoming weekday 
# If today is Monday, getDateOfNextWeekday(Monday) will return the date for Monday next week
# If today is Monday, getDateOfNextWeekday(Tuesday) will return the date of tomorrow
on getDateOfNextWeekday(nextWeekdayToFind)
	set returnDate to current date
	if returnDate's weekday is nextWeekdayToFind then return (current date) + (7 * days)
	repeat until returnDate's weekday is nextWeekdayToFind
		set returnDate to returnDate + days
	end repeat
	
	return returnDate
end getDateOfNextWeekday
#----------------------------------------











2 Likes

I also have version which returns the date within a track, the corresponding abbreviation are (nxmon, (nxtue, (nxwed, etc.

Here is the code

# AppleScript for TextExpander
# Helmut Hauser
# http://www.worksmartandberemarkable.com




# For different weekdays you simply have to change the this line
# e.g., set dateOfNextWeekday to getDateOfNextWeekWeekday(Sunday) -- Sunday
set dateOfNextWeekday to getDateOfNextWeekday(Monday) -- Monday






set myWeekDay to weekday of dateOfNextWeekday
set myMonth to month of dateOfNextWeekday
set myDay to day of dateOfNextWeekday
set myYear to year of dateOfNextWeekday


# Here we put the final output together
# if you want a different format, you will have to change this line
# simply combine variables, like myMonth or my Day), with other textpiece like ")" 
# together with &
#set fullOutput to " (" & myMonth & " " & myDay & ", " & myYear & ")"
set fullOutput to " (" & myMonth & " " & myDay & ")"


return fullOutput








# ----------------------------------------
# subroutine from Doug Stephen
# see http://canadian-fury.com/2012/08/31/performing-fixed-point-date-arithmetic-in-textexpander-using-applescript/
# adapted by Helmut Hauser


# eneric script that allows you to get the date information of a specific upcoming weekday
# e.g., to get the date of next Monday
# getDateOfNextWeekday(Monday)
# Note that the scripts caculates the next upcoming weekday 
# If today is Monday, getDateOfNextWeekday(Monday) will return the date for Monday next week
# If today is Monday, getDateOfNextWeekday(Tuesday) will return the date of tomorrow
on getDateOfNextWeekday(nextWeekdayToFind)
	set returnDate to current date
	if returnDate's weekday is nextWeekdayToFind then return (current date) + (7 * days)
	repeat until returnDate's weekday is nextWeekdayToFind
		set returnDate to returnDate + days
	end repeat
	
	return returnDate
end getDateOfNextWeekday
#----------------------------------------











3 Likes

This is great stuff @HelmutHauser! It makes me so happy to see inline comments! :smiley:

2 Likes

This is great, @HelmutHauser! I’ve added the first one to our Community Snippets Public Group with the abbreviation com.nxmon.

1 Like

Here’s the same code in JavaScript so it works outside of the Mac:

var targetWeekday = 1; // 0=Sunday, 1=Monday, ..., 6=Saturday
var now = new Date();
var daysAhead = (targetWeekday - now.getDay() + 7) % 7 || 7; // Skip to next week if today matches
var nextDate = new Date(now.setDate(now.getDate() + daysAhead));
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var weekday = weekdays[nextDate.getDay()];
var month = months[nextDate.getMonth()];
var day = nextDate.getDate();
weekday + " (" + month + " " + day + ")";

1 Like