• 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
Flattening Scripts
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Flattening Scripts


  • Subject: Flattening Scripts
  • From: Christopher Stone <email@hidden>
  • Date: Thu, 13 Feb 2014 20:21:01 -0600

Hey Folks,

Script Debugger has for a very long time had its own method of attaching libraries nicely tucked away in the library panel of the UI.  It performs some behind the scenes magic and attaches them to the script at compile time.

The problem with this scheme is that if you open the script in an Applescript editor other than SD the libraries aren't seen.  That's a bit inconvenient when you want to share work with someone who doesn't have SD, and Mark made it possible to "flatten" a script and incorporate its libraries into the script proper.  The feature's main foible is that every handler in every attached library is included.

This has frustrated me for quite while, but I haven't been willing to futz with it.

Tonight I got really annoyed and spent 30 minutes writing a scanner that will look for handler-calls in the front script, scan my library folder for libraries, find the handler text of only the handlers used in the script and copy them to the clipboard.

Now I can work the way I normally work and yet quickly prepare a script (without all the fat or the effort of carving it off) to share with someone who doesn't have SD.

The script is Satimage.osax-dependent (no surprise) for regex and for reading script resources (much faster than osadecompile).

Of course this won't work neatly with Mavericks' ASObjC Libraries, but the year is young yet.

It's pretty sloppy, but it works.

The Finder bit filters out libraries marked with the Red label.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2014-02-13 : 19:06
# dMod: 2014-02-13 : 19:41
# Appl: Script Debugger, Finder
# Task: Find complete handler code for all handlers in the front script.
#     : Intended to make it easier to "flatten" scripts with libraries.
# Osax: Satimage.osax { http://tinyurl.com/dc3soh }
# Tags: @Applescript, @Script, @Script_Debugger, @Finder, @Handler
-------------------------------------------------------------------------------------------

try


set libFolder to alias ((path to library folder from user domain as text) & "Script Libraries:")
set LF to linefeed
set foundHandlerList to {}
set sep to "-------------------------------------------------------------------------------------------"
-------------------------------------------------------------------------------------------
tell application "Script Debugger"
if document 1 exists then
set scriptSrc to source text of document 1
else
error "No Script Document Exists!"
end if
end tell


if scriptSrc ≠ "" then
set handlerList to fndUsing("\\b([a-z][\\w_]+)\\(.+\\)", "\\1", scriptSrc, 0, 1, 0, 1, 1) of me
set handlerList to sortlist handlerList with remove duplicates
set findList to cng("^(.+)", "(?m)^on \\1\\\\(.+\\\\).+^end \\1$", handlerList) of me


tell application "Finder"
set libList to (files of libFolder whose label index is not 2 and kind is "Compiled OSA Script") as alias list
end tell


repeat with ndx from 1 to (length of libList)
set libFile to item ndx of libList
set libSrc to load resource 2001 type "TEXT" from libFile as «class utf8»
repeat with i in findList
set regEx to contents of i
set _handler to fnd(regEx, libSrc, false, false, true) of me
if _handler ≠ false then
set end of foundHandlerList to _handler
end if
end repeat
end repeat


set foundHandlerList to sep & LF & "--» HANDLERS" & LF & sep & LF & (join foundHandlerList using LF & sep & LF) & LF & sep & LF
set the clipboard to foundHandlerList
else
error "No handlers found!"
end if


on error e number n
stdErr(e, n, true, true) of me
end try
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on cng(findText, changeText, srcData)
change findText into changeText in srcData with regexp without case sensitive
end cng
-------------------------------------------------------------------------------------------
on fnd(_find, _data, _case, _all, strRslt) # Last 3 are all bool
try
find text _find in _data case sensitive _case all occurrences _all string result strRslt with regexp
on error
return false
end try
end fnd
-------------------------------------------------------------------------------------------
on fndUsing(_find, _capture, _data, _case, _regex, _word, _all, strRslt)
try
set findResult to find text _find in _data using _capture case sensitive _case regexp _regex whole word _word ¬
all occurrences _all string result strRslt
on error
false
end try
end fndUsing
-------------------------------------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
set e to e & return & return & "Num: " & n
if beepFlag = true then
beep
end if
if ddFlag = true then
tell me
set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
end tell
if button returned of dDlg = "Copy" then set the clipboard to e
else
return e
end if
end stdErr
-------------------------------------------------------------------------------------------

 _______________________________________________
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

  • Follow-Ups:
    • Re: Flattening Scripts
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Re: Upgrading to Mavericks - is it likely to break EVERYTHING!?
  • Next by Date: Re: Flattening Scripts
  • Previous by thread: Re: Upgrading to Mavericks - is it likely to break EVERYTHING!?
  • Next by thread: Re: Flattening Scripts
  • Index(es):
    • Date
    • Thread