• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Extract Handler-Calls from AppleScript Libraries with AppleScriptObjC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Extract Handler-Calls from AppleScript Libraries with AppleScriptObjC


  • Subject: Extract Handler-Calls from AppleScript Libraries with AppleScriptObjC
  • From: Christopher Stone <email@hidden>
  • Date: Wed, 15 Mar 2017 21:17:03 -0500

Hey Folks,

I've been using various techniques to extract AppleScript-Library source-text since 1997 or so.

If you're interested in reading the full writeup see my post on the LNS AppleScript Forum:

http://forum.latenightsw.com/t/automating-handler-selection-from-script-library-file-ideas/422/18?u=ccstone

The current generation of my personal script uses a combination of AppleScriptObjC and the Satimage.osax and is short and sweet.  It reads Script Debugger's main.recover.rtf file in libraries saved as script-bundles and does so lightning-quick – then it uses the Satimage.osax's regex functions to parse the text just the way I want.

I used to have the handler-calls pop-up in a choose-from list, but now I export them to Typinator and use its Quick Search function.



Now then – today I got a little push to rewrite that script so it would work on any type of AppleScript library (compiled-script or script-bundle) and would NOT require the Satimage.osax.

• It filters the libraries to only the ones I want using a regular _expression_.
    – Currently set to find ALL libraries.

• It extracts the source-text of all found libraries.

• It has regular _expression_ support for filtering the output text.
    – There's currently a filter to output only handler-calls.

The regex and find-file handlers will be useful for other projects.

Enjoy.

--
Best Regards,
Chris

------------------------------------------------------------------------------
# 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:
------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: Rebooting a Mac with a Mail Rule
  • Next by Date: add notes to pdf on local os x webserver
  • Previous by thread: Re: Rebooting a Mac with a Mail Rule
  • Next by thread: add notes to pdf on local os x webserver
  • Index(es):
    • Date
    • Thread