PDF File Management

You've a year digit missing in the first -- slash-dates work fine for me with 4-digit years, two-digit dates also work but there is an assumption that, for today (26 Jan), 50 and higher are in the 1900s while 49 and lower are the 2000s (I'm guessing this cutoff increments every year. Moral -- don't use two-digit years in your dates :wink:

YYYYMMDD works for me too, so I'm not sure what's happening for you. Basically, if the Data Detector can determine a (sensible) date in the string it will. But the script will error if there is more than one date in the string, 1 Jan 2025 today for example, but that's because I'm not smart enough to handle an ObjC array :frowning:

Just use one -- pass the number of days to subtract as a parameter, use that number as the days multiplier. For a KM variable Local_daysToSubtract:

use framework "Foundation"
use framework "AppKit"
use scripting additions

set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set dateString to getvariable "Local_PDFDate" instance inst
	set daysToSubtract to (getvariable "Local_daysToSubtract" instance inst) as number
end tell

set anNSString to current application's NSString's stringWithString:dateString
set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeDate) |error|:(missing value)
set resultsArray to theNSDataDetector's matchesInString:dateString options:0 range:{0, anNSString's |length|()}
set theDate to (resultsArray's valueForKey:"date") as date
set previousDate to theDate - daysToSubtract * days

set outText to "" & year of previousDate & (text -2 thru -1 of ("0" & word 2 of short date string of previousDate)) & (text -2 thru -1 of ("0" & day of previousDate))

return outText

The only trick is that KM variables are strings so we have to coerce it to a number (done as part of getting the value, in line 7).

Works fine here -- at least, my version does. But I've had to create it from scratch so it may not be the same as yours.

You really do need to post a minimal macro that shows the problem, otherwise we're just guessing.

While I do like the "Substrings" Action, one "Search Using Regular Expression" Action can replace all three:

1 Like