Hi folks!
I need to blow the dust off a classic. I used the wonderful Dates & Times in AppleScripts posts to build a workflow that enters the Tuesday and Thursday on a product based on a given date regardless of which day of the week it falls on.
I’m having trouble running this in Mojave. It has run for years in other OS’s without issue. Mojave works thru Thursday. Friday & Saturday are moving to the next week. Sunday 12/30/18 thru Thursday 1/3/19 return Week 0. Friday 1/4/19 & Saturday 1/5/19 return Week 1.
I would really appreciate your help.
--Mojave Works Thru Thursday. Friday & Saturday Move To The Next Week.
--Sunday 12/30/18 thru Thursday 1/3/19 return Week 0.
--Friday 1/4/19 & Saturday 1/5/19 return Week 1.
--ACTIVATE ONE OF THESE 3 TEST STRINGS.
--set fmData to "123456|SJL123085|Product|12/31/18" --Text String Example: OK
--set fmData to "123456|SJL123085|Product|01/03/19" --Text String Example: OK
--set fmData to "123456|SJL123085|Product|01/04/19" --Error. Moves to next week.
set TempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "|"
set TIDnum to count text items of fmData
set GPON to first text item of fmData
set VerCode to second text item of fmData
set ProType to third text item of fmData
set IHD to fourth text item of fmData
set AppleScript's text item delimiters to TempTID
--Parse IHD.
set TempTID2 to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set TIDnum2 to count text items of IHD
set ihdMonth to first text item of IHD
set ihdDay to second text item of IHD
set ihdYear to third text item of IHD
set AppleScript's text item delimiters to TempTID2
--Get week number.
date (IHD) as text
set currentWeekNumber to ((getDatesWeekNum((date (IHD)))) - (getDatesWeekNum(date ("1/1/" & ((ihdYear)) as string))))
display dialog currentWeekNumber
--Get Tuesday & Thursday.
getdatesforweeknum((ihdYear), (currentWeekNumber))
set inhomeweek to (getdatesforweeknum((ihdYear), (currentWeekNumber)))
count the items in inhomeweek
get item 1 of inhomeweek
set Tu to (item 1 of inhomeweek)
get item 3 of inhomeweek
set Thu to (item 3 of inhomeweek)
set Tue to Tu as text
date_wo_year(Tue) as text
set Thur to Thu as text
date_with_year(Thur) as text
set PMdate to (date_wo_year(Tue)) & "-" & (date_with_year(Thur)) as text
display dialog PMdate
--Week number calculation.
on getDatesWeekNum(theDate)
set {dDate, dDateNum} to {"1/1/1000", 364878}
return ((theDate - (date dDate)) div days + dDateNum) div 7 + 1
end getDatesWeekNum
--MOJAVE: Choose Sunday instead of Monday.
on getdatesforweeknum(yearNum, weekNum)
set Jan4 to date "Tuesday, January 4, 2000 at 3:30:00 AM"
set Jan4's year to yearNum
set baseSunday to date "Sunday, January 2, 2000 at 3:30:00 AM" --Or any Sunday in the past - MOJAVE MOD.
set week1Start to Jan4 - (Jan4 - baseSunday) mod weeks
set weekNStart to week1Start + (weekNum - 1) * weeks
set datesInWeek to {}
repeat with i from 0 to 6 * days by days
set end of datesInWeek to weekNStart + i as list
end repeat
return datesInWeek
end getdatesforweeknum
--Format text date to number date without year.
to date_wo_year(textDate)
set {year:y, month:m, day:d} to date textDate
tell (y * 10000 + m * 100 + d) as string to text 5 thru 6 & "/" & text 7 thru 8
end date_wo_year
--Format text date to number date with year.
to date_with_year(textDate)
set {year:y, month:m, day:d} to date textDate
tell (y * 10000 + m * 100 + d) as string to text 5 thru 6 & "/" & text 7 thru 8 & "/" & text 1 thru 4
end date_with_year
At one point we were looking to move everything to JXA so I built this one. Sadly, the rest of script is not supported by one developer or another so we had to abandon it.
The first test string contains an error: 1/1-2/3/2019. (Note Jan 1-Feb 3, 2019.) The others work without issue.
Is it still not possible to run JXA inside of AS?
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var sysEvents = Application('System Events');
var finder = Application('Finder');
var indd = 'com.adobe.InDesign';
var inddA = Application('com.Adobe.InDesign');
// File = ([String.path])
var workFldr = Path('/Users/Shared/Work/')
var workFldrIndd = "/Users/Shared/Work/"
var inddDoc = indd.activeDocument;
// ACTIVATE ONE OF THESE 3 TEST STRINGS.
// The first test string contains an error: 1/1-2/3/2019. The others work OK.
// var fmData = "123456|SJL123085|Product|12/31/18"; // ERROR. Note 2nd month.
// var fmData = "123456|SJL123085|Product|01/03/19";
// var fmData = "123456|SJL123085|Product|01/04/19";
var array = fmData.split('|');
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
var currentArrayItem = array[i];
}
var gpon = array[0];
var verCode = array[1];
var proType = array[2];
var ihd = array[3];
// Parse IHD.
var array = ihd.split('/');
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
var currentArrayItem = array[i];
}
var ihdMonth = array[0];
var ihdDay = array[1];
var ihdYear = array[2];
// Remove '/' from IHD for job save.
var docIHD = ihd;
docIHD = docIHD.split('/').join('');
var verCodeIHD = ((verCode) + `-` + (docIHD));
// app.displayDialog(`The new InDesign document will be named \r` + `\'` + (verCodeIHD) + `.\'`);
// Get Tuesday & Thursday of IHW.
var myIHD = new Date(ihd);
var first = myIHD.getDate() - myIHD.getDay();
var third = first + 2; // Get Tuesday.
var fifth = first + 4; // Get Thursday.
var thirdDay = new Date(myIHD.setDate(third)).toLocaleDateString();
var fifthDay = new Date(myIHD.setDate(fifth)).toLocaleDateString();
// Tuesday.
var array = thirdDay.split('/');
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
var currentArrayItem = array[i];
}
var tuesMonth = array[0];
var tuesDay = array[1];
// Thursday.
var array = fifthDay.split('/');
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
var currentArrayItem = array[i];
}
var thurMonth = array[0];
var thurDay = array[1];
var thurYear = array[2];
var pmDates = ((tuesMonth) + `/` + (tuesDay) + `-`+ (thurMonth) + `/` + (thurDay) + `/` + (thurYear));
app.displayDialog(pmDates);