------------------------------------------------------------------------------
# Auth: Christopher Stone
# Help:
# dCre: 2017/03/12 22:04
# dMod: 2017/03/15 16:52
# Appl: AppleScriptObjC
# Task: Osadecompile from ASObjC - decompile AppleScript Libraries.
# : RegEx filter to select only desired libraries.
# : RegEx filter to output only hander-calls.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @OSAdecompile, @Decompile, @Library, @Libraries
------------------------------------------------------------------------------
use framework "Foundation"
use framework "OSAKit"
use scripting additions
------------------------------------------------------------------------------
set scptLibFldrPath to "~/Library/Script Libraries/"
set scptLibFldrPath to current application's NSString's stringWithString:scptLibFldrPath
set scptLibFldrPath to scptLibFldrPath's stringByExpandingTildeInPath() as string
------------------------------------------------------------------------------
--» Script Library Filter (uses ICU regular expressions)
------------------------------------------------------------------------------
# To find all libraries use this line.
set libFileList to its findFilesWithRegEx:".*" inDir:scptLibFldrPath
------------------------------------------------------------------------------
# Find only library scripts with names containing "Lb" case-sensitive.
# set libFileList to its findFilesWithRegEx:".*Lb.*" inDir:scptLibFldrPath
------------------------------------------------------------------------------
set scriptLibraryText to {}
repeat with libFile in libFileList
set end of scriptLibraryText to linefeed & "••••• LIBRARY SEPARATOR •••••" & linefeed
set end of scriptLibraryText to (its extractScriptSourceFrom:libFile)
end repeat
set AppleScript's text item delimiters to linefeed
set scriptLibraryText to scriptLibraryText as text -- entire text of all libraries
------------------------------------------------------------------------------
# Return Entire Text of all Libraries
------------------------------------------------------------------------------
# return scriptLibraryText -- uncomment to return
------------------------------------------------------------------------------
# Filter library text using a regular _expression_ – extract handler-calls:
------------------------------------------------------------------------------
set handlerList to its regexMatch:"(?m)^on (\\w.+)" fromString:scriptLibraryText captureTemplate:"$1"
set handlerList to handlerList as text
------------------------------------------------------------------------------
return handlerList
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on extractScriptSourceFrom:scriptPath
set aURL to current application's |NSURL|'s fileURLWithPath:scriptPath
set theScript to current application's OSAScript's alloc()'s initWithContentsOfURL:aURL |error|:(missing value)
return theScript's source() as text
end extractScriptSourceFrom:
------------------------------------------------------------------------------
on findFilesWithRegEx:findPattern inDir:srcDirPath
set fileManager to current application's NSFileManager's defaultManager()
set sourceURL to current application's |NSURL|'s fileURLWithPath:srcDirPath
set theURLs to fileManager's contentsOfDirectoryAtURL:sourceURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
set theURLs to theURLs's allObjects()
set foundItemList to current application's NSPredicate's predicateWithFormat_("lastPathComponent matches %@", findPattern)
set foundItemList to theURLs's filteredArrayUsingPredicate:foundItemList
set foundItemList to (foundItemList's valueForKey:"path") as list
end findFilesWithRegEx:inDir:
------------------------------------------------------------------------------
on regexMatch:thePattern fromString:theString captureTemplate:templateStr
set theString to current application's NSString's stringWithString:theString
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:0 |error|:(missing value)
set theFinds to theRegEx's matchesInString:theString options:0 range:{0, theString's |length|()}
set theResult to current application's NSMutableArray's array()
repeat with aFind in theFinds
set foundString to (theRegEx's replacementStringForResult:aFind inString:theString |offset|:0 template:templateStr)
(theResult's addObject:foundString)
end repeat
return theResult as list
end regexMatch:fromString:captureTemplate:
------------------------------------------------------------------------------