Ever since the tragic end announced last October of Bastiaan “DJ Bazzie Wazzie” Boertien’s magical sparkly Applescript Toolbox (https://astoolbox.wordpress.com/2018/10/02/the-end-of-applescript-toolbox/), I have stubbornly refused to upgrade any of my Macs to Mojave. Apple’s decision to disallow all 3rd party scripting additions, even when properly signed by known developers, is at best myopic and at worst contemptuous of AS developers.
And with no real compelling reasons to upgrade to Mojave — Dark Mode is fun, but it can in no way compensate for the loss of essential functionality embedded into scores of Applescripts I use every day — I’ve been very content to continue life at High Sierra ranch this past year. But now that we’re learning more about Marzipan, and the ability to one day hopefully see beloved IOS apps ported for Mac, it was time to begin figuring out a replacement for AST Toolbox, beyond disabling System Integrity Protection.
Below you’ll find:
-
My first draft of a script library to replace AST’s commands, including 7 of the 20 AST commands I use the most:
• AST date with format
• AST format date
• AST find regex
• AST guess language for
• AST mouse point location
• AST URL decode
• AST URL encode
2)Just like AST, all functions were built for fast execution speed. You’ll also note that my implementation of AST find regex is very bare bones, with none of the Toolbox’s shiny bells and whistles.
-
Examples demonstrating how the script library’s syntax compares to AST’s are at the end of this post. I hewed as closely as possible to the original, even throwing in an AST prefix in recognition of Toolbox’s legacy, just like the NS prefix in cocoa pays tribute to NeXTSTEP. I’ve also included a couple of explanations where necessary, but please see Applescript Toolbox’s dictionary for far more helpful and detailed instructions.
-
For a basic primer on script libraries, see https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/UseScriptLibraries.html.
Please note:
-
The script library should be saved as “AST.scpt”, in your script folder path.
-
Scripts invoking the script library will need to include the following statement in their header, and should also declare a global AST variable:
set AST to load script “Macintosh HD:Your Script Folder Path:AST.scpt”) as alias
global AST -
Due to an insanely busy day job, I won’t be able to participate in any active discussions about these functions, or respond to private messages, but I’ll try and pop in every few weeks or so.
-
My apologies in advance for any and all errors. These functions are up and running on my Macs, and I hope they’ll run fine on yours, too.
Feel free to suggest corrections / additions, etc. I can only hope DJ Bazzie Wazzie knows how truly wuly thankful I am for all his tremendous efforts.
AST.scpt:
use framework "Foundation"
#AST mouse point location
on ASTmousePointLocation()
return {item 1 of (current application's NSEvent's mouseLocation() as list), (current application's NSHeight((current application's class "NSScreen"'s mainScreen())'s visibleFrame())) - (item 2 of (current application's NSEvent's mouseLocation() as list))}
end ASTmousePointLocation
#AST guess language for
on ASTguessLanguageFor(theText)
return (current application's NSLinguisticTagger's dominantLanguageForString:theText) as text
end ASTguessLanguageFor
#AST URL encode
on ASTURLEncode(theText)
return ((current application's NSString's stringWithString:(theText))'s stringByAddingPercentEscapesUsingEncoding:(current application's NSASCIIStringEncoding)) as text
end ASTURLEncode
#AST URL decode
on ASTURLDecode(theText)
return ((current application's NSString's stringWithString:(theText))'s stringByReplacingPercentEscapesUsingEncoding:(current application's NSASCIIStringEncoding)) as text
end ASTURLDecode
#AST date with format (Returns date created with the given format. For information on formats, please see http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns)
on ASTDateWithFormat(theFormat, theDate, theLocale)
set dateFormatter to current application's NSDateFormatter's alloc()'s init()
set dateFormatter's locale to current application's NSLocale's localeWithLocaleIdentifier:theLocale
set dateFormatter's dateFormat to theFormat
set theDate to (dateFormatter's dateFromString:theDate)
set theDate to theDate as date
return theDate
end ASTDateWithFormat
#AST format date (Returns string representation of a date in the given format. For information on formats, please see http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns)
on ASTformatDate(theFormat, theDate, theLocale)
set {theYear, theMonth, theDay, theSeconds} to theDate's {year, month, day, time}
if theYear < 0 then
set theYear to -theYear
set theEra to 0
else
set theEra to 1
end if
set theCalendar to current application's NSCalendar's currentCalendar()
set theDate to theCalendar's dateWithEra:theEra |year|:theYear |month|:(theMonth as integer) |day|:theDay hour:0 minute:0 |second|:theSeconds nanosecond:0
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:theLocale)
theFormatter's setDateFormat:theFormat
set theString to theFormatter's stringFromDate:theDate
return theString as text
end ASTformatDate
#AST find regex (Returns a list of regex matches in a string. Variable 'theGroup' is an integer referencing the parts of the matches which correspond to a particular group in the regex. Group 1 references the whole regex, group 2 is the first parenthesised subgroup, and so on, tracking the parenthesis openings from left to right.)
on ASTfindRegex(theRegex, theText, theGroup)
set theText to current application's NSString's stringWithString:theText #encoding:(current application's NSUTF8StringEncoding)
set theRegex to current application's NSRegularExpression's regularExpressionWithPattern:(theRegex) options:(current application's NSRegularExpressionAnchorsMatchLines) |error|:(missing value)
set theFinds to theRegex's matchesInString:theText options:0 range:{0, theText's |length|()}
set theResults to current application's NSMutableArray's array()
repeat with aFind in theFinds
if theGroup is 1 then
(theResults's addObject:(theText's substringWithRange:(aFind's range())))
else if theGroup > 1 then
(theResults's addObject:(theText's substringWithRange:(aFind's rangeAtIndex:(theGroup - 1))))
end if
end repeat
return theResults as list
end ASTfindRegex
Examples: Applescript Toolbox vs. AST Script Library
#IMPORTANT: BE SURE TO UPDATE THE LINE BELOW WITH YOUR SCRIPT FOLDER PATH
set AST to load script "Macintosh HD:Your Script Folder Path:AST.scpt") as alias
global AST
set {theX1, theY1} to AST mouse point location
tell AST to set {theX2, theY2} to ASTmousePointLocation()
set theLanguage1 to AST guess language for ("eins, zwei, drei, vier")
tell AST to set theLanguage2 to ASTguessLanguageFor("eins, zwei, drei, vier")
set theEncodedText1 to AST URL encode "u / \\ doing & why?"
tell AST to set theEncodedText2 to ASTURLEncode("u / \\ doing & why?")
set theDecodedText1 to AST URL decode "u%20/%20%5C%20do%20&%20why?"
tell AST to set theDecodedText2 to ASTURLDecode("u%20/%20%5C%20do%20&%20why?")
set theRegex1 to AST find regex "(\\d)(\\.)(\\d)" in string "3.4, 3.9 and 4.1" regex group 4
tell AST to set theRegex2 to ASTfindRegex("(\\d)(\\.)(\\d)", "3.4, 3.9 and 4.1", 4)
set theDateString1 to AST format date "EEEE d MMMM yyyy" of date current date using locale "en_US"
tell AST to set theDateString2 to ASTformatDate("EEEE d MMMM yyyy", current date, "en_US")
set theDate1 to AST date with format "EEEE d MMMM yyyy HH:mm" date string "Saturday 20 April 2019 15:30" using locale "en_US"
tell AST to set theDate2 to ASTDateWithFormat("EEEE d MMMM yyyy HH:mm", "Saturday 20 April 2019 15:30", "en_US")